Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding ignore files to git repo retroactively

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 of 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?

like image 477
devoured elysium Avatar asked Apr 19 '11 14:04

devoured elysium


2 Answers

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.

like image 130
sehe Avatar answered Sep 24 '22 01:09

sehe


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

like image 42
Jared Avatar answered Sep 24 '22 01:09

Jared