Int*_*idd 275 git ignore gitignore
我的存储库中有一些文件应该被忽略,我将它们添加到.gitignore中,当然,它们不会从我的存储库中删除.
所以我的问题是,是否有一个使用filter-branch的魔术命令或脚本可以重写我的历史记录并轻松删除所有这些文件?或者只是一个命令,它将创建一个将删除它们的提交?
Sam*_*ane 377
You can remove them from the repository manually:
git rm --cached file1 file2 dir/file3
Run Code Online (Sandbox Code Playgroud)
Or, if you have a lot of files:
git rm --cached `git ls-files -i --exclude-from=.gitignore`
Run Code Online (Sandbox Code Playgroud)
But this doesn't seem to work in Git Bash on Windows. It produces an error message. The following works better:
git ls-files -i --exclude-from=.gitignore | xargs git rm --cached
Run Code Online (Sandbox Code Playgroud)
关于在没有这些文件的情况下重写整个历史,我非常怀疑有一种自动的方法.
而且我们都知道改写历史很糟糕,不是吗?:)
gta*_*atr 299
可以在任何操作系统上运行的更简单方法
git rm -r --cached .
git add .
git commit -m "Removing all files in .gitignore"
Run Code Online (Sandbox Code Playgroud)
你基本上读取了所有文件,除了.gitignore中的文件
小智 126
由于未跟踪.gitignore中的文件,因此可以使用git clean命令以递归方式删除不受版本控制的文件.
使用git clean -xdn进行试运行,看看将被删除.
然后git clean -xdf用来执行它.
基本上,git clean -h或man git-clean(在unix中)会给你帮助.
请注意,此命令还将删除不在暂存区域中的新文件.
希望能帮助到你.
我通过使用 sed 操作 .gitignore 语句的输出做了一个非常简单的解决方案:
cat .gitignore | sed '/^#.*/ d' | sed '/^\s*$/ d' | sed 's/^/git rm -r /' | bash
解释:
小智 6
git rm --cached -r .
递归删除所有缓存
git add .
添加 .gitignore 中未包含的所有文件
您将必须提交一些已删除的文件,这些文件在文件系统上并未真正删除
使用单个命令将是
git rm --cached -r . && git add .