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


当前回答

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 .

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

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

更新您的.gitignore 文件 - 例如,添加一个文件夹,你不想跟踪到.gitignore. git rm -r --cached. - 删除所有跟踪的文件,包括所需和不需要的. 您的代码将是安全的,只要你保存了本地. git 添加. - 所有文件将返回,除了那些在.gitignore.


请点击 @AkiraYamamoto 指向我们正确的方向。

使用 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 历史。

例如,在使用 CVS 时,这个问题不存在, CVS 将信息存储为基于文件的变更列表。

这两篇文章对我很有帮助:

git assume-unchanged vs skip-worktree 以及如何用 Git 忽略跟踪文件的变化

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

从此,此文件中的所有本地变更将被忽视,不会去远程。如果文件在远程中被更改,冲突将发生,当 git pull. Stash 不会工作。

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

文件内容将被远程内容取代. 将您的更改从安全地点调整到文件,然后再次执行:

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