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

发布于 2024-08-02 09:28:59 字数 359 浏览 2 评论 0原文

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

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

git add /path/to/file 无效。

git add -f /path/to/file 无效。

git status /path/to/file 显示“要提交的更改”存储桶中的文件。

为了确定起见,我删除了 .gitignore 文件。上述任何行为均不会改变。

我已经完成了 git reset --hard ,重新进行了更改。上述任何行为均不会改变。

这里可能发生了什么?

I make an arbitrary change to a file within my git working directory.

git status does not recognized that the file has changed.

git add /path/to/file has no effect.

git add -f /path/to/file has no effect.

git status /path/to/file shows the file as in the 'changes to be committed' bucket.

I removed my .gitignore file, just to be sure. No change to any of the above behaviors.

I have done git reset --hard, re-made my change. No change to any of the above behaviors.

What could be going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

第七度阳光i 2024-08-09 09:28:59

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

git update-index --assume-unchanged path/to/file

尽管听起来很愚蠢,但我这样做了,然后几天后对文件进行了更改,但无法弄清楚为什么git 没有跟踪它。我尝试了上述所有建议,但一直在抓狂,因为更改的文件未在任何 .gitignore 或 except 文件中列出。

如果你告诉 git 假设文件未更改,你将不得不:

git update-index --no-assume-unchanged path/to/file

或者只是重新克隆存储库,就像我在记起自己所做的事情之前所做的那样......

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.

If you've told git to assume the file is unchanged, you will have to:

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...

信仰 2024-08-09 09:28:59

结果我在我的文件中添加了 skip-worktree [1]

git update-index --skip-worktree path/to/file

然后忘记了。您可以使用以下命令撤消此操作:

git update-index --no-skip-worktree path/to/file

ls-files-v 在我的案例中显示了文件的状态:

git ls-files -v | grep path/to/file
S path/to/file

字母 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

识别您的问题

该文件是否被 git 忽略?

git check-ignore * **/* | grep path/to/file
git ls-files --exclude-standard --ignore

如果列出了该文件,则说明某处有一条忽略规则将其排除在外。[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

该文件是否标记为 assume-unchanged

git ls-files -v | grep path/to/file

如果文件以小写字母为前缀,则会使用 assume-unchanged 进行标记。修复方法:

git update-index --no-assume-unchanged path/to/file

文件是否标记为 skip-worktree

git ls-files -v | grep path/to/file

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

git update-index --no-skip-worktree path/to/file

Turns out I added skip-worktree on my file [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:

git ls-files -v | grep path/to/file
S path/to/file

The letters git ls-files -v will prefix with are and mean:

# 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 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:

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

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:

git update-index --no-assume-unchanged path/to/file

Is the file flagged 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:

git update-index --no-skip-worktree path/to/file
混浊又暗下来 2024-08-09 09:28:59

Git 忽略文件的一般原因有两个: gitignore子模块

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

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

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

无法跟踪文件在 Git 子模块内

There are two general reasons why Git will ignore a file: gitignore and submodules.

To be more specific, the following conditions will cause Git to ignore a file when 'git add' is invoked:

  1. The file matches a pattern in $GIT_DIR/exclude.
  2. The file matches a pattern in a .gitignore file inside the repo.
  3. The file matches a pattern in a user-specific .gitignore file (specified by 'git config --global core.excludesfile').
  4. The file is part of a submodule.

More info can be found in another SO question:

Unable to track files within Git submodules

尴尬癌患者 2024-08-09 09:28:59

检查使用

$ git ls-files --exclude-standard --ignore

文件是否确实未被排除(除了

Check using

$ git ls-files --exclude-standard --ignore

if the file is truly not excluded (there are other exclude files than only .gitignore).

给妤﹃绝世温柔 2024-08-09 09:28:59

你检查你的全局 git 忽略文件吗?

git config --global --get-all core.excludesfile
git config --system --get-all core.excludesfile

如果其中任何一个返回一个文件作为其值,请查看该文件。

You check your global git ignore file?

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.

等风来 2024-08-09 09:28:59

如果您的文件位于“要提交的更改”存储桶中,则 git 已经识别该更改并将提交它!它已经在索引中。否则,它将位于“已更改但未更新”存储桶中。

:)

希望这有帮助/

if your file is in the 'Changes to be committed' bucket then git already recognized the change and is going to commit it! Its in the index already. Otherwise it would be in the 'Changed but not updated' bucket.

:)

Hope this helps/

很酷不放纵 2024-08-09 09:28:59

您确定您的文件没有被父目录中的某些 .gitignore 文件排除吗?

Are you sure that your file is not excluded by some of the .gitignore files in parent directories?

筑梦 2024-08-09 09:28:59

完成 git add 后,您是否进行了提交,以便它实际上位于存储库中并且可以与(更改的)工作副本进行比较?

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?

£烟消云散 2024-08-09 09:28:59

检查从相关文件到项目根目录的每个父目录中是否有 .gitignore 文件。

某些项目使用多个 .gitignore 文件,每个文件都位于自己的目录中,而不是根目录中的单个 .gitignore 文件。

Check each parent directory from the file in question to the project root for .gitignore files.

Some projects use several .gitignore files, each in its own directory, instead of a single .gitignore in the root.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文