So I had this project where at first I did a
git add .
so I could just get working and not get bothered with which files were going to be version-controlled or what.
That, of course, got painful, as while between each commit I usually just change 3-5 files, all the build files (.class, .html, .xml, etc) are completely regenerated, meaning that doing a
git add .
will add hundreds o开发者_StackOverflowf files instead of the 3-5 files that I really changed.
I know I can add all the unwanted files to a .gitignore
file. What I'd like to do, is if there is a way to do this retroactively. I'd like to get rid of all files that shouldn't have been in commits in the first place.
Is it possible to do this in git? If yes, how?
Yes, you can use git filter-branch
.
The 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).
So as an example:
git filter-branch \
--index-filter 'git rm --cached --ignore-unmatch filename' \
--tag-name-filter cat \
-- --all
The tag-name-filter
is there to rewrite your tags as well, so you won't lose them.
Disclaimer
Be careful rewriting public history. You might make it impossible/difficult for others to keep up-to-date or contribute.
http://help.github.com/removing-sensitive-data/
Not removing sensitive data but it seems this is what you're looking for.
git filter-branch --index-filter 'git rm --cached --ignore-unmatch *.<filetype>' HEAD
As far as I know git won't do this retroactively. You need to git rm
or git rm --cached
them.
精彩评论