Undoing a 'git push'

前端 未结 12 985
遥遥无期
遥遥无期 2020-11-22 13:45

Here\'s what I did on my supposed-to-be-stable branch...

% git rebase master
First, rewinding head to repla         


        
相关标签:
12条回答
  • 2020-11-22 14:11

    git revert is less dangerous than some of the approaches suggested here:

    prompt> git revert 35f6af6f77f116ef922e3d75bc80a4a466f92650
    [master 71738a9] Revert "Issue #482 - Fixed bug."
     4 files changed, 30 insertions(+), 42 deletions(-)
    prompt> git status
    # On branch master
    # Your branch is ahead of 'origin/master' by 1 commit.
    #
    nothing to commit (working directory clean)
    prompt>
    

    Replace 35f6af6f77f116ef922e3d75bc80a4a466f92650 with your own commit.

    0 讨论(0)
  • 2020-11-22 14:11

    you can use the command reset

    git reset --soft HEAD^1
    

    then:

    git reset <files>
    git commit --amend
    

    and

    git push -f
    
    0 讨论(0)
  • 2020-11-22 14:13

    Undo multiple commits git reset --hard 0ad5a7a6 (Just provide commit SHA1 hash)

    Undo last commit

    git reset --hard HEAD~1 (changes to last commit will be removed ) git reset --soft HEAD~1 (changes to last commit will be available as uncommited local modifications)

    0 讨论(0)
  • 2020-11-22 14:17

    The existing answers are good and correct, however what if you need to undo the push but:

    1. You want to keep the commits locally or you want to keep uncommitted changes
    2. You don't know how many commits you just pushed

    Use this command to revert the change to the ref:

    git push -f origin refs/remotes/origin/<branch>@{1}:<branch>
    
    0 讨论(0)
  • 2020-11-22 14:19
    git push origin +7f6d03:master
    

    This will revert your repo to mentioned commit number

    0 讨论(0)
  • 2020-11-22 14:21

    If you want to ignore the last commit that you have just pushed in the remote branch: this will not remove the commit but just ignoring it by moving the git pointer to the commit one earlier, refered by HEAD^ or HEAD^1

    git push origin +HEAD^:branch
    

    But if you have already pushed this commit, and others have pulled the branch. In this case, rewriting your branch's history is undesirable and you should instead revert this commit:

    git revert <SHA-1>
    git push origin branch
    
    0 讨论(0)
提交回复
热议问题