为什么 git 忽略我更改的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1329361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is git ignoring my changed file?
提问by joshwa
I make an arbitrary change to a file within my git working directory.
我对 git 工作目录中的文件进行了任意更改。
git status
does not recognized that the file has changed.
git status
无法识别文件已更改。
git add /path/to/file
has no effect.
git add /path/to/file
没有效果。
git add -f /path/to/file
has no effect.
git add -f /path/to/file
没有效果。
git status /path/to/file
shows the file as in the 'changes to be committed' bucket.
git status /path/to/file
将文件显示为“要提交的更改”存储桶中的文件。
I removed my .gitignore file, just to be sure. No change to any of the above behaviors.
我删除了我的 .gitignore 文件,只是为了确定。没有改变上述任何行为。
I have done git reset --hard
, re-made my change. No change to any of the above behaviors.
我已经完成了git reset --hard
,重新进行了更改。没有改变上述任何行为。
What could be going on here?
这里会发生什么?
采纳答案by Tim Henigan
There are two general reasons why Git will ignore a file: gitignore
and submodules
.
Git 忽略文件的一般原因有两个:gitignore
和submodules
.
To be more specific, the following conditions will cause Git to ignore a file when 'git add
' is invoked:
更具体地说,以下条件将导致 Git 在git add
调用' '时忽略文件:
- The file matches a pattern in
$GIT_DIR/exclude
. - The file matches a pattern in a
.gitignore
file inside the repo. - The file matches a pattern in a user-specific
.gitignore
file (specified by 'git config --global core.excludesfile
'). - The file is part of a submodule.
- 该文件与
$GIT_DIR/exclude
. - 该文件与
.gitignore
存储库内文件中的模式匹配。 - 该文件与用户特定
.gitignore
文件(由“git config --global core.excludesfile
”指定)中的模式匹配。 - 该文件是子模块的一部分。
More info can be found in another SO question:
更多信息可以在另一个 SO 问题中找到:
回答by Tom Wayson
Also make sure that you have not manually updated the index to assume that the changed file(s) are unchanged like so:
还要确保您没有手动更新索引以假设更改的文件未更改,如下所示:
git update-index --assume-unchanged path/to/file
As dumb as it sounds I did that, then a few days later made changes to the file and could not figure out why git was not tracking it. I tried all of the above suggestions, and kept pulling my hair out b/c the changed file was not listed in any .gitignore
or exclude
files.
听起来很愚蠢,我这样做了,然后几天后对文件进行了更改,但无法弄清楚为什么 git 没有跟踪它。我尝试了所有的上述建议,并保持/拉我的头发B C更改的文件并没有任何上市.gitignore
或exclude
文件。
If you've told git to assume the file is unchanged, you will have to:
如果你告诉 git 假设文件没有改变,你将不得不:
git update-index --no-assume-unchanged path/to/file
or just re-clone the repo like I ended up doing before I remembered what I had done...
或者只是像我最后做的那样重新克隆 repo,然后我才想起我做了什么......
回答by Nate
Turns out I added skip-worktree
on my file [1]
原来我skip-worktree
在我的文件中添加了[1]
git update-index --skip-worktree path/to/file
and forgot. You can undo this with:
忘记了。您可以通过以下方式撤消此操作:
git update-index --no-skip-worktree path/to/file
ls-files
with -v
revealed the status of the file in my case:
ls-files
与-v
显示在我的情况下,文件的状态:
git ls-files -v | grep path/to/file
S path/to/file
The letters git ls-files -v
will prefix with are and mean:
这些字母git ls-files -v
将以 are 为前缀,意思是:
# H: cachead
# S: skip-worktree
# M: unmerged
# R: removed/deleted
# C: modified/changed
# K: to be killed
# ?: other
# lowercase letter: assume-unchanged
To identify your issue
确定您的问题
Is the file ignored by git?
该文件是否被 git 忽略?
git check-ignore * **/* | grep path/to/file
git ls-files --exclude-standard --ignore
If the file is listed, there is an ignore rule somewhere that is excluding it.[2] [3]Find it in one of these files:
如果列出了该文件,则某处有一个忽略规则将其排除在外。[2] [3]在以下文件之一中找到它:
less .gitignore
less ~/.gitignore
less %USERPROFILE%\.gitignore # <- Probably the same file as the above one
less $( git config --global core.excludesfile )
less $( git config --system core.excludesfile )
less ${GIT_DIR:-.git}/exclude
Is the file flagged assume-unchanged
文件是否被标记 assume-unchanged
git ls-files -v | grep path/to/file
If the file is prefixed with a lower case letter, it is flagged with assume-unchanged
. Fix it with:
如果文件以小写字母为前缀,则标记为assume-unchanged
. 修复它:
git update-index --no-assume-unchanged path/to/file
Is the file flagged skip-worktree
文件是否被标记 skip-worktree
git ls-files -v | grep path/to/file
If the file is prefixed with S
, it is flagged with skip-worktree
. Fix it with:
如果文件以 为前缀S
,则标记为skip-worktree
。修复它:
git update-index --no-skip-worktree path/to/file
回答by Jakub Nar?bski
Check using
检查使用
$ git ls-files --exclude-standard --ignore
if the file is truly not excluded (there are other exclude files than only .gitignore).
如果确实没有排除文件(除了.gitignore之外还有其他排除文件)。
回答by davetron5000
You check your global git ignore file?
你检查你的全局 git ignore 文件?
git config --global --get-all core.excludesfile
git config --system --get-all core.excludesfile
If either of those return a file as their value, look in that file.
如果其中任何一个返回文件作为其值,请查看该文件。
回答by Vitaly Kushner
if your file is in the 'Changes to be committed' bucket then git already recognizedthe change and is going to commit it! Its in the index already. Otherwise it would be in the 'Changed but not updated' bucket.
如果您的文件在“要提交的更改”存储桶中,那么 git已经识别出更改并将提交它!它已经在索引中了。否则它将在“已更改但未更新”存储桶中。
:)
:)
Hope this helps/
希望这可以帮助/
回答by hlovdal
Are you sure that your file is not excluded by some of the .gitignore files in parent directories?
您确定您的文件没有被父目录中的某些 .gitignore 文件排除在外吗?
回答by kajaco
After you did git add, did you do a commit so it's actually in the repo and can be compared to the (changed) working copy?
在你做了 git add 之后,你是否做了一个提交,所以它实际上在 repo 中并且可以与(更改的)工作副本进行比较?
回答by nostromo
Check each parent directory from the file in question to the project root for .gitignore files.
检查从相关文件到 .gitignore 文件的项目根目录的每个父目录。
Some projects use several .gitignore files, each in its own directory, instead of a single .gitignore in the root.
一些项目使用多个 .gitignore 文件,每个文件都在自己的目录中,而不是根目录中的单个 .gitignore 。