Undoing a 'git push'

前端 未结 12 984
遥遥无期
遥遥无期 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 13:56

    I believe that you can also do this:

    git checkout alpha-0.3.0
    git reset --hard cc4b63bebb6
    git push origin +alpha-0.3.0
    

    This is very similar to the last method, except you don't have to muck around in the remote repo.

    0 讨论(0)
  • 2020-11-22 13:57

    You need to make sure that no other users of this repository are fetching the incorrect changes or trying to build on top of the commits that you want removed because you are about to rewind history.

    Then you need to 'force' push the old reference.

    git push -f origin last_known_good_commit:branch_name
    

    or in your case

    git push -f origin cc4b63bebb6:alpha-0.3.0
    

    You may have receive.denyNonFastForwards set on the remote repository. If this is the case, then you will get an error which includes the phrase [remote rejected].

    In this scenario, you will have to delete and recreate the branch.

    git push origin :alpha-0.3.0
    git push origin cc4b63bebb6:refs/heads/alpha-0.3.0
    

    If this doesn't work - perhaps because you have receive.denyDeletes set, then you have to have direct access to the repository. In the remote repository, you then have to do something like the following plumbing command.

    git update-ref refs/heads/alpha-0.3.0 cc4b63bebb6 83c9191dea8
    
    0 讨论(0)
  • 2020-11-22 14:00

    A way to do it without losing the changes you wanted:

    git reset cc4b63b 
    git stash
    git push -f origin alpha-0.3.0
    git stash pop
    

    Then you can choose the files you meant to push

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

    The accepted solution (from @charles bailey) is highly dangerous if you are working in a shared repo.

    As a best practice, all commits pushed to a remote repo that is shared should be considered 'immutable'. Use 'git revert' instead: http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#fixing-mistakes

    https://git-scm.com/book/be/v2/Git-Basics-Undoing-Things

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

    Scenario 1: If you want to undo the last commit say 8123b7e04b3, below is the command(this worked for me):

    git push origin +8123b7e04b3^:<branch_name>
    

    Output looks like below:

    Total 0 (delta 0), reused 0 (delta 0)
    To https://testlocation/code.git
     + 8123b7e...92bc500 8123b7e04b3^ -> master (forced update)
    

    Additional info: Scenario 2: In some situation, you may want to revert back what you just undo'ed (basically undo the undo) through the previous command, then use the below command:

    git reset --hard 8123b7e04b3
    

    Output:

    HEAD is now at cc6206c Comment_that_was_entered_for_commit
    

    More info here: https://github.com/blog/2019-how-to-undo-almost-anything-with-git

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

    Another way to do this:

    1. create another branch
    2. checkout the previous commit on that branch using "git checkout"
    3. push the new branch.
    4. delete the old branch & push the delete (use git push origin --delete <branch_name>)
    5. rename the new branch into the old branch
    6. push again.
    0 讨论(0)
提交回复
热议问题