How do you fix a bad merge, and replay your good commits onto a fixed merge?

后端 未结 12 1197
时光取名叫无心
时光取名叫无心 2020-11-21 10:25

I accidentally committed an unwanted file (filename.orig while resolving a merge) to my repository several commits ago, without me noticing it until now. I want

12条回答
  •  天命终不由人
    2020-11-21 10:48

    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:

    $ 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)

    $ 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.

    $ 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.

    $ 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 ../large_dot_git and ../tmpdir directories (maybe in a couple weeks or month from now, just in case...)

提交回复
热议问题