git push after removing large file

后端 未结 2 2001
礼貌的吻别
礼貌的吻别 2021-02-04 18:43

I accidentally commited log/test.log but have never pushed it. I have since done a git rm to get rid of it. But when I try to push, I\'m still getting a huge amount of data at

2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-04 19:25

    Don't revert the commit and then push because the huge file will still be carried around in the history.

    Given that you haven't yet pushed it and that the commit you want to redo is the most recent, remove that commit from your history:

    $ git reset HEAD^

    This will return your index to the state it was in on the parent commit (HEAD^). Now you have a mulligan: add and commit the way you meant to the first time around.

    If you've made other subsequent commits, you'll need to git rebase -i where is the SHA-1 of the bad commit's parent.

    For example (and note the SHA-1 will be different in your repo)

    $ git rebase -i 57d0b28

    will drop you in an editor that resembles

    pick 366eca1 This has a huge file
    pick d975b30 delete foo
    pick 121802a delete bar
    
    # Rebase 57d0b28..121802a onto 57d0b28
    #
    # Commands:
    #  p, pick = use commit
    #  r, reword = use commit, but edit the commit message
    #  e, edit = use commit, but stop for amending
    #  s, squash = use commit, but meld into previous commit
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    # However, if you remove everything, the rebase will be aborted.
    #

    Replace pick with edit on the line with the heavy commit

    edit 366eca1 This has a huge file
    pick d975b30 delete foo
    pick 121802a delete bar

    Save and quit your editor to return to your shell, where you'll see a message of the form

    Stopped at 366eca1... This has a huge file
    You can amend the commit now, with
    
        git commit --amend
    
    Once you are satisfied with your changes, run
    
        git rebase --continue

    From there, delete the offending file (--cached removes the file from the index only)

    $ git rm --cached big-nasty-file
    rm 'big-nasty-file'

    amend the commit

    $ git commit --amend

    and finish the rebase

    $ git rebase --continue

提交回复
热议问题