How can I undo a `git commit` locally and on a remote after `git push`

后端 未结 7 1097
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 03:28

I have performed git commit followed by a git push. How can I revert that change on both local and remote repositories?

$ git log
         


        
相关标签:
7条回答
  • 2020-12-02 03:58

    Alternatively:

    git push origin +364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8^:master
    

    Force the master branch of the origin remote repository to the parent of last commit

    0 讨论(0)
  • 2020-12-02 04:02

    git reset HEAD~1 if you don't want your changes to be gone(unstaged changes). Change, commit and push again git push -f [origin] [branch]

    0 讨论(0)
  • 2020-12-02 04:02

    Try using

    git reset --hard <commit id> 
    

    Please Note : Here commit id will the id of the commit you want to go to but not the id you want to reset. this was the only point where i also got stucked.

    then push

    git push -f <remote> <branch>
    
    0 讨论(0)
  • 2020-12-02 04:07
    git reset --hard HEAD~1
    git push -f <remote> <branch>
    

    (Example push: git push -f origin bugfix/bug123)

    This will undo the last commit and push the updated history to the remote. You need to pass the -f because you're replacing upstream history in the remote.

    0 讨论(0)
  • 2020-12-02 04:07

    You can do an interactive rebase:

    git rebase -i <commit>
    

    This will bring up your default editor. Just delete the line containing the commit you want to remove to delete that commit.

    You will, of course, need access to the remote repository to apply this change there too.

    See this question: Git: removing selected commits from repository

    0 讨论(0)
  • 2020-12-02 04:13

    First of all, Relax.

    "Nothing is under our control. Our control is mere illusion.", "To err is human"

    I get that you've unintentionally pushed your code to remote-master. THIS is going to be alright.

    1. At first, get the SHA-1 value of the commit you are trying to return, e.g. commit to master branch. run this:

    git log
    

    you'll see bunch of 'f650a9e398ad9ca606b25513bd4af9fe...' like strings along with each of the commits. copy that number from the commit that you want to return back.

    2. Now, type in below command:

    git reset --hard your_that_copied_string_but_without_quote_mark
    

    you should see message like "HEAD is now at ". you are on clear. What it just have done is to reflect that change locally.

    3. Now, type in below command:

    git push -f
    

    you should see like

    "warning: push.default is unset; its implicit value has changed in..... ... Total 0 (delta 0), reused 0 (delta 0) ... ...your_branch_name -> master (forced update)."

    Now, you are all clear. Check the master with "git log" again, your fixed_destination_commit should be on top of the list.

    You are welcome (in advance ;))

    UPDATE:

    Now, the changes you had made before all these began, are now gone. If you want to bring those hard-works back again, it's possible. Thanks to git reflog, and git cherry-pick commands.

    For that, i would suggest to please follow this blog or this post.

    0 讨论(0)
提交回复
热议问题