所以我有这个项目,起初我做了一个
git add .
这样我就可以开始工作,而不必为哪些文件将要进行版本控制或其他什么而烦恼。
当然,这很痛苦,因为在每次提交之间我通常只更改 3-5 个文件,所有构建文件(.class、.html、.xml 等)都完全重新生成,这意味着做一个
git add .
将添加数百个文件,而不是我真正更改的 3-5 个文件。
我知道我可以将所有不需要的文件添加到 .gitignore
文件中。我想做的是,是否有办法追溯执行此操作。我想摆脱所有不应该首先提交的文件。
是否可以在 git 中执行此操作?如果是,如何?
最佳答案
是的,你可以使用 git filter-branch
。
Pro Git Book has a nice section on removing objects from history
--index-filter <command>
This is the filter for rewriting the index. It is similar to the tree filter but does not check out the tree, which makes it much faster. Frequently used with
git rm --cached --ignore-unmatch …
, see EXAMPLES below. For hairy cases, see git-update-index(1).
举个例子:
git filter-branch \
--index-filter 'git rm --cached --ignore-unmatch filename' \
--tag-name-filter cat \
-- --all
tag-name-filter
也可以重写您的标签,因此您不会丢失它们。
免责声明
重写公共(public)历史时要小心。您可能会让其他人无法/难以了解最新信息或做出贡献。
关于git - 将忽略文件追溯添加到 git repo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5717976/