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


当前回答

特别是对于基于 IDE 的文件,我使用以下文件:

例如,对于 slnx.sqlite 文件,我刚刚完全解除它如下:

git rm {PATH_OF_THE_FILE}/slnx.sqlite -f
git commit -m "remove slnx.sqlite"

请记住,其中一些文件存储一些本地用户设置和项目偏好(类似于您打开的文件)。所以每次您浏览或在您的 IDE 中进行一些更改,该文件会被更改,因此它会检查并显示为未订购的更改。

其他回答

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>

如果你不想使用 CLI 并在 Windows 上工作,一个非常简单的解决方案是使用 TortoiseGit. 它在菜单中有“删除(保持本地)”操作,它工作得很好。

我知道这个答案已经迟到了很多年,但是我决定我想在注意到可能有不同的答案后做出贡献,取决于用户想要的结果。

如何强迫Git完全忘记这一点?

这取决于下列哪个是所需的结果:

停止跟踪文件并将其从存储库中删除,或停止跟踪文件,而不会从存储库中删除(可能其他合作伙伴可以下载)

因为(一),解决方案就像上面的马特的答案一样;因为(二),请参见康斯坦丁的答案。

来源: https://stackabuse.com/git-stop-tracking-file-after-adding-to-gitignore/

如果有人在Windows上有艰难的时间,你想忽略整个文件夹,请在文件探测器上转到所需的“文件夹”,右键单击并进行“Git Bash Here”(Git for Windows 应该安装)。

执行此命令:

git ls-files -z | xargs -0 git update-index --assume-unchanged

此方法需要使用 /.git/info/exclude 文件(偏好)或预先存在的.gitignore 在所有有文件要被忽视/忘记的任务中。

执行Git的所有方法都忽略了后事实行为,有效地重写历史,从而对任何公共/共享/合作的存储库产生了显著的分裂,这些存储库可能在这个过程之后被撤回。

#Commit up-to-date .gitignore (if not already existing)
#This command must be run on each branch

git add .gitignore
git commit -m "Create .gitignore"

#Apply standard Git ignore behavior only to the current index, not the working directory (--cached)
#If this command returns nothing, ensure /.git/info/exclude AND/OR .gitignore exist
#This command must be run on each branch

git ls-files -z --ignored --exclude-standard | xargs -0 git rm --cached

#Commit to prevent working directory data loss!
#This commit will be automatically deleted by the --prune-empty flag in the following command
#This command must be run on each branch

git commit -m "ignored index"

#Apply standard git ignore behavior RETROACTIVELY to all commits from all branches (--all)
#This step WILL delete ignored files from working directory UNLESS they have been dereferenced from the index by the commit above
#This step will also delete any "empty" commits.  If deliberate "empty" commits should be kept, remove --prune-empty and instead run git reset HEAD^ immediately after this command

git filter-branch --tree-filter 'git ls-files -z --ignored --exclude-standard | xargs -0 git rm -f --ignore-unmatch' --prune-empty --tag-name-filter cat -- --all

#List all still-existing files that are now ignored properly
#If this command returns nothing, it's time to restore from backup and start over
#This command must be run on each branch

git ls-files --other --ignored --exclude-standard

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

其他从已修改的远程存储库中提取的开发人员应该进行备份,然后:

#fetch modified remote

git fetch --all

#"Pull" changes WITHOUT deleting newly-ignored files from working directory
#This will overwrite local tracked files with remote - ensure any local modifications are backed-up/stashed

git reset FETCH_HEAD

脚印

告诉你的合作伙伴放弃,不要合并,他们创建的任何分支从你的旧(封闭)存储库历史。 一个合并承诺可以重新引入一些或所有的封闭的历史,你刚刚走到清理问题。