为什么git忽略我更改的文件?

jos*_*hwa 15 git gitignore

我对我的git工作目录中的文件进行了任意更改.

git status 无法识别文件已更改.

git add /path/to/file 没有效果.

git add -f /path/to/file 没有效果.

git status /path/to/file 将文件显示为"要提交的更改"存储区中的文件.

我删除了我的.gitignore文件,只是为了确定.上述任何行为均无变化.

我做了git reset --hard,重新做了我的改变.上述任何行为均无变化.

这可能会发生什么?

Tom*_*son 27

还要确保您没有手动更新索引以假设更改的文件未更改,如下所示:

git update-index --assume-unchanged path/to/file
Run Code Online (Sandbox Code Playgroud)

虽然我听起来很愚蠢,但几天之后对文件进行了更改,并且无法弄清楚为什么git没有跟踪它.我尝试了以上所有的建议,并继续拉我的头发b/c更改的文件没有在任何.gitignoreexclude文件中列出.

如果你告诉git假设文件没有变化,你必须:

git update-index --no-assume-unchanged path/to/file
Run Code Online (Sandbox Code Playgroud)

或者只是重新克隆回购,就像我记得我做过之前做的那样...

  • 这只是拯救了我的培根.需要更多的赞成. (3认同)
  • 接受的答案没有帮助,但确实如此.谢谢@tomwayson (2认同)

Nat*_*ate 18

事实证明我添加skip-worktree了我的文件[1]

git update-index --skip-worktree path/to/file
Run Code Online (Sandbox Code Playgroud)

并忘记了.你可以撤消这个:

git update-index --no-skip-worktree path/to/file
Run Code Online (Sandbox Code Playgroud)

ls-files-v显示在我的情况下,文件的状态:

git ls-files -v | grep path/to/file
S path/to/file
Run Code Online (Sandbox Code Playgroud)

字母git ls-files -v前缀为are和mean:

# H: cachead
# S: skip-worktree
# M: unmerged
# R: removed/deleted
# C: modified/changed
# K: to be killed
# ?: other
# lowercase letter: assume-unchanged
Run Code Online (Sandbox Code Playgroud)

识别您的问题

该文件被git忽略了吗?

git check-ignore * **/* | grep path/to/file
git ls-files --exclude-standard --ignore
Run Code Online (Sandbox Code Playgroud)

如果列出了该文件,则会在某处将忽略规则排除在外.[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
Run Code Online (Sandbox Code Playgroud)

该文件是否已标记 assume-unchanged

git ls-files -v | grep path/to/file
Run Code Online (Sandbox Code Playgroud)

如果文件的前缀是小写字母,则会标记为assume-unchanged.修复它:

git update-index --no-assume-unchanged path/to/file
Run Code Online (Sandbox Code Playgroud)

该文件是否已标记 skip-worktree

git ls-files -v | grep path/to/file
Run Code Online (Sandbox Code Playgroud)

如果文件带有前缀S,则标记为skip-worktree.修复它:

git update-index --no-skip-worktree path/to/file
Run Code Online (Sandbox Code Playgroud)


Tim*_*gan 9

Git忽略文件有两个一般原因:gitignoresubmodules.

更具体地说,以下条件将导致Git在git add调用' ' 时忽略文件:

  1. 该文件与中的模式匹配$GIT_DIR/exclude.
  2. 该文件与.gitignorerepo内文件中的模式匹配.
  3. 该文件与用户特定文件中的模式匹配.gitignore(由' git config --global core.excludesfile' 指定).
  4. 该文件是子模块的一部分.

更多信息可以在另一个SO问题中找到:

无法跟踪Git子模块中的文件


Jak*_*ski 6

检查使用

$ git ls-files --exclude-standard --ignore
Run Code Online (Sandbox Code Playgroud)

如果文件确实没有被排除(除了.gitignore之外还有其他排除文件).


归档时间:

查看次数:

26256 次

最近记录:

10 年,5 月 前