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


当前回答

在我的案例中,我在几个目录中有几个.lock 文件,我需要删除。

git rm -r --cached **/*.lock

这样做,在我所在的“根”下进入每个文件夹,并排除所有符合模式的文件。

其他回答

复制/复制(单线)的答案是:

git rm --cached -r .; git add .; git status; git commit -m "Ignore unwanted files"

这个命令不会改变.gitignore 文件的内容. 它只会忽略已承诺到 Git 存储库的文件,但现在我们已经添加到.gitignore。

命令 git 状态; 只需审查更改,可以下载。

最终,它将立即通过“不要错过不需要的文件”的消息进行更改。

如果您不希望承诺更改,请放下命令的最后一部分(git commit -m “不要错过不需要的文件”)

这就是我如何解决我的问题:

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD git push

在此,我们基本上正在试图在以前的任务中重写该特定文件的历史。

有关更多信息,您可以在此处参阅过滤器分支的男性页面。

来源:从存储库中删除敏感数据 - 使用过滤器分支

来源:Git:如何删除错误的大文件

在已承诺的DS_Store的情况下:

find . -name .DS_Store -print0 | xargs -0 git rm --ignore-unmatch

忽略它们:

echo ".DS_Store" >> ~/.gitignore_global
echo "._.DS_Store" >> ~/.gitignore_global
echo "**/.DS_Store" >> ~/.gitignore_global
echo "**/._.DS_Store" >> ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global

最后,做一个承诺!

此方法需要使用 /.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

脚印

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

做下列步骤严肃,你会好。

从目录/存储中删除错误添加的文件. 您可以使用“rm -r”命令(为Linux)或通过浏览目录删除它们. 或者将它们移动到您的计算机上的另一个位置. (您可能需要关闭 IDE 如果运行移动/删除.) 现在将文件 / 目录添加到.gitignore 文件并保存它. 现在通过使用这些命令从 Git 存储中删除它们(如果有)