Git: erase files from repository from previous commits
I am on github. I set up a .gitig开发者_如何学Cnore file in which I specify files not to be added to future commits since I'm trying to lighten the cloning. Now I need to delete all those .class files for example from all the previous commits to share only the necessary sources, otherwise no matter my efforts it will always weight 50mb more or less. I had a look on http://cheat.errtheblog.com/s/git I think I might need a composition of those command but I really don't wanna s***w up my work. thanks
There is an example in the git-filter-branch docs which matches your use case.
git clone project project.tmp
cd project.tmp
git filter-branch --prune-empty --index-filter 'git rm --cached --ignore-unmatch *.class' HEAD
cd ..
git clone project.tmp project.clean
rm -rf project.tmp
cd project.clean
project.clean
should then be ready to be pushed upstream.
PS. If you are nervous about how this operation might go, it never hurts to experiment on a clone and github test branch.
Before attempting this it is of course a good idea to switch to a testing branch:
git branch testing; git checkout testing
What I did was just to find all the class files in the tree of committed files and then deleted them from git:
- git ls-files |grep *.class
- for file in
git ls-files |grep *.class
; do echo $file; done; This is just checking whether my for loop will have all the files that I want before rm - for file in
git ls-files |grep *.class
; do git rm --cached $file; done;
Then you commit this to your git repo and put this into your .gitignore:
syntax: regexp
*.class
Once you are done with this you can choose to merge this into your master branch and push or you just repeat the same on the master branch.
精彩评论