Git常见问题


Git常见问题


文件权限修改取消追踪

在一次开发中,Linux的项目拉取到Windows的项目中,发现文件权限被修改了,如 755 变成了 644, 导致文件被追踪,而提交时会报错。

起因,一个项目直接从另一台电脑复制过来,结果git diff时提示:

warning: in the working copy of 'app/admin/controller/Index.php', LF will be replaced by CRLF the next time Git touches it

这里需要说一下,LF 被 CRLF 替换,如果大家都在同一个电脑版本环境下开发,可以使用以下命令:

# 告诉 Git 忽略行尾风格
git config core.autocrlf false

既然复制过来出现了这个问题,那就在原来电脑全部提交,然后在这台电脑中拉取,就可以解决这个问题了,包括项目的 vendor 目录。结果在添加 vendor 时又提示了这个:

dministrator@PC-20260803TFLF MINGW64 /d/develop/www/xian (master)
$ git diff
diff --git a/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock b/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock
--- a/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock
+++ b/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock
@@ -1 +1 @@
-Subproject commit 90a04bcbf03784066f16038e87e23a0a83cee3c2
+Subproject commit 90a04bcbf03784066f16038e87e23a0a83cee3c2-dirty

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian (master)
$ git add .

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian (master)
$ git commit -m 'vendor 提交补充'
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  (commit or discard the untracked or modified content in submodules)
        modified:   vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock (modified content)

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian (master)
$ git diff
diff --git a/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock b/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock
--- a/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock
+++ b/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock
@@ -1 +1 @@
-Subproject commit 90a04bcbf03784066f16038e87e23a0a83cee3c2
+Subproject commit 90a04bcbf03784066f16038e87e23a0a83cee3c2-dirty

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian (master)

commit时未添加成功。

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian (master)
$ cd vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock ((90a04bc...))
$ git status
HEAD detached at 90a04bc
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   tests/coverage-checker.php

no changes added to commit (use "git add" and/or "git commit -a")

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock ((90a04bc...))
$ git diff
warning: in the working copy of 'tests/coverage-checker.php', LF will be replaced by CRLF the next t
ime Git touches it
diff --git a/tests/coverage-checker.php b/tests/coverage-checker.php
old mode 100755
new mode 100644

Administrator@PC-20260803TFLF MINGW64 /d/develop/www/xian/vendor/alipaysdk/easysdk/vendor/phpdocumentor/reflection-docblock ((90a04bc...))
$ 

看到是文件的权限被修改了 100755 改成了 100644, 导致文件被追踪,而提交时因为文件内容未修改所以无法添加提交。

在这里忽略文件权限修改追踪就可以了:

# 告诉 Git 忽略文件权限变化
git config core.filemode false

另外说一下,完全抛弃本地修改,回退到上一个版本, 并且删除本地缓存,使用下面的命令:

git reset --hard HEAD






参考资料


返回