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


当前回答

下面的命令系列将从 Git 索引中删除所有项目(不是工作目录或本地存储库),然后更新 Git 索引,同时遵守 Git 忽略。

首先:

git rm -r --cached .
git add .

然后:

git commit -am "Remove ignored files"

或者作为一个单线:

git rm -r --cached . && git add . && git commit -am "Remove ignored files"

其他回答

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

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

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

脚印

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

.gitignore 将防止未跟踪的文件添加(没有添加 -f)到 Git 跟踪的文件集,但是, Git 将继续跟踪已跟踪的任何文件。

要停止跟踪文件,我们必须从指数中删除文件:

git rm --cached <file>

要重新删除文件夹和文件中的所有文件:

git rm -r --cached <folder>

将文件从主审查中删除,将在下一个任务中进行。

警告:虽然这不会从您的本地机器中删除物理文件,但它会在下一个 git 拖动时将从其他开发人员的机器中删除文件。

BFG 专门用于从 Git 存储库中删除不需要的数据,如大文件或密码,因此它有一个简单的旗帜,将删除任何大历史文件(不属于您的当前文件):‘--strip-blobs-bigger-than’

java -jar bfg.jar --strip-blobs-bigger-than 100M

如果你想按名称指定文件,你也可以这样做:

java -jar bfg.jar --delete-files *.mp4

BFG 比 git 过滤器行业快 10 至 1000 倍,通常更容易使用 - 查看完整使用指示和示例,以获取更多详细信息。

来源:库存大小减少

我喜欢JonBrave的答案,但我有足够的工作目录,承诺 - 一个让我有点害怕,所以这里是我做的事情:

git config --global alias.exclude-ignored '!git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached &&  git ls-files -z --ignored --exclude-standard | xargs -0 git stage &&  git stage .gitignore && git commit -m "new gitignore and remove ignored files from index"'

打破它:

git ls-files -z --ignored --exclude-standard | xargs -0 git rm -r --cached
git ls-files -z --ignored --exclude-standard | xargs -0 git stage
git stage .gitignore
git commit -m "new gitignore and remove ignored files from index"

删除被忽略的文件从索引阶段.gitignore 和您刚刚删除的文件承诺