我把以前由 Git 跟踪的文件放在.gitignore 列表中. 但是,文件在编辑后仍然显示在 git 状态。


当前回答

使用 git rm --cached 命令不会回答原始问题:

你怎样要强迫吉特完全忘记(一个文件)?

事实上,这个解决方案会导致文件在运行 git pull 时在存储库的每个其他例子中被删除!

强迫 Git 忘记文件的正确方式是由 GitHub 在这里记录的。

我推荐阅读文档,但基本上:

git fetch --all
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch full/path/to/file' --prune-empty --tag-name-filter cat -- --all
git push origin --force --all
git push origin --force --tags
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

只需用文件的完整路径取代完整/路径/到/文件,请确保您已将文件添加到.gitignore 文件中。

您还需要(暂时)允许您的存储库不快推进,因为您正在改变您的 Git 历史。

其他回答

把它移动出来,承诺,然后把它移动回来。

这对我来说在过去已经工作了,但可能有更“迷人的”方式来实现这一点。

要总结一下:

您的应用程序正在寻找一个被忽略的文件 config-overide.ini 并使用它在承诺的文件 config.ini (或替代,寻找 ~/.config/myapp.ini,或 $MYCONFIGFILE) 承诺文件 config-sample.ini 并忽略文件 config.ini,有脚本或类似的复制文件如有必要。

git update-index 为我做工作:

git update-index --assume-unchanged <file>

注意: 这个解决方案实际上是独立的.gitignore 因为 gitignore 仅适用于未追踪的文件。


更新,更好的选择

因为这个答案被发布,一个新的选项已经创建,应该是优先的. 你应该使用 --skip-worktree 这是为修改的跟踪文件,用户不想再承诺,并保持 --assume-unchanged 性能,以防止 git 检查大跟踪文件的状态. 查看 https://stackoverflow.com/a/13631525/717372 更多详细信息...

git update-index --skip-worktree <file>

取消

git update-index --no-skip-worktree <file>

答案来自Matt Frear是最有效的IMHO. 下面只是一个PowerShell脚本给那些在Windows只删除文件从他们的Git存储库,符合他们的排除列表。

# Get files matching exclusionsfrom .gitignore
# Excluding comments and empty lines
$ignoreFiles =  gc .gitignore | ?{$_ -notmatch  "#"} |  ?{$_ -match  "\S"} | % {
                    $ignore = "*" + $_ + "*"
                    (gci -r -i $ignore).FullName
                }
$ignoreFiles = $ignoreFiles| ?{$_ -match  "\S"}

# Remove each of these file from Git
$ignoreFiles | % { git rm $_}

git add .
git ls-files -c --ignored --exclude-standard -z | xargs -0 git rm --cached
git commit -am "Remove ignored files"

这取消了被忽略的文件列表,从指数中删除它们,并执行了变更。