如何从Git中移除不小心提交的文件?

stackoverflow看到的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# create and check out a temporary branch at the location of the bad merge
git checkout -b tmpfix <sha1-of-merge>

# remove the incorrectly added file
git rm somefile.orig

# commit the amended merge
git commit --amend

# go back to the master branch
git checkout master

# replant the master branch onto the corrected merge
git rebase tmpfix

# delete the temporary branch
git branch -d tmpfix

下面还有一种方法,不过使用的命令比上面的复杂:

This is the best way:

http://github.com/guides/completely-remove-a-file-from-all-revisions

Just be sure to backup the copies of the files first.

EDIT

The edit by Neon got unfortunately rejected during review.
See Neons post below, it might contain useful information!

E.g. to remove all *.gz files accidentally committed into git repository:

1
2
3
4
5
6
7
$ du -sh .git ==> e.g. 100M
$ git filter-branch --index-filter 'git rm --cached --ignore-unmatch *.gz' HEAD
$ git push origin master --force
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now
$ git gc --aggressive --prune=now

That still didn’t work for me? (I am currently at git version 1.7.6.1)

1
$ du -sh .git ==> e.g. 100M

Not sure why, since I only had ONE master branch. Anyways, I finally got my git repo truely cleaned up by pushing into a new empty and bare git repository, e.g.

1
2
3
$ git init --bare /path/to/newcleanrepo.git
$ git push /path/to/newcleanrepo.git master
$ du -sh /path/to/newcleanrepo.git ==> e.g. 5M

(yes!)

Then I clone that to a new directory and moved over it’s .git folder into this one. e.g.

1
2
3
4
$ mv .git ../large_dot_git
$ git clone /path/to/newcleanrepo.git ../tmpdir
$ mv ../tmpdir/.git .
$ du -sh .git ==> e.g. 5M

(yeah! finally cleaned up!)

After verifying that all is well, then you can delete the ../largedotgit and ../tmpdir directories (maybe in a couple weeks or month from now, just in case…)

Posted via UltraBlog.vim.

更多
No Responses Post a comment

Leave a Reply

Note: You may use basic HTML in your comments. Your email address will not be published.

Subscribe to this comment feed via RSS