How can I rollback a github repository to a specific commit?

后端 未结 6 1923
孤街浪徒
孤街浪徒 2020-11-28 16:44

My github has 100 commits in it right now. I need to rollback the repository to commit 80, and remove all the subsequent ones.

Why? This repo is supposed to be fo

相关标签:
6条回答
  • 2020-11-28 17:23

    Another way:

    Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree, I right-clicked on the and selected "Reset BRANCHNAME to this commit".

    Then navigate to your repository's local directory and run this command:

    git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f -- tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME 
    

    This will erase all commits after the current one in your local repository but only for that one branch.

    0 讨论(0)
  • 2020-11-28 17:25

    Most suggestions are assuming that you need to somehow destroy the last 20 commits, which is why it means "rewriting history", but you don't have to.

    Just create a new branch from the commit #80 and work on that branch going forward. The other 20 commits will stay on the old orphaned branch.

    If you absolutely want your new branch to have the same name, remember that branch are basically just labels. Just rename your old branch to something else, then create the new branch at commit #80 with the name you want.

    0 讨论(0)
  • 2020-11-28 17:26

    To undo the most recent commit I do this:

    First:

    git log
    

    get the very latest SHA id to undo.

    git revert SHA
    

    That will create a new commit that does the exact opposite of your commit. Then you can push this new commit to bring your app to the state it was before, and your git history will show these changes accordingly.

    This is good for an immediate redo of something you just committed, which I find is more often the case for me.

    As Mike metioned, you can also do this:

    git revert HEAD
    
    0 讨论(0)
  • 2020-11-28 17:28
    git reset --hard <old-commit-id>
    git push -f <remote-name> <branch-name>
    

    Note: As written in comments below, Using this is dangerous in a collaborative environment: you're rewriting history

    0 讨论(0)
  • 2020-11-28 17:43

    When doing branch updates from master, I notice that I sometimes over-click, and cause the branch to merge into the master, too. Found a way to undo that.

    If your last commit was a merge, a little more love is needed:

    git revert -m 1 HEAD

    0 讨论(0)
  • 2020-11-28 17:47

    In github, the easy way is to delete the remote branch in the github UI, under branches tab. You have to make sure remove following settings to make the branch deletable:

    1. Not a default branch
    2. No opening poll requests.
    3. The branch is not protected.

    Now recreate it in your local repository to point to the previous commit point. and add it back to remote repo.

    git checkout -b master 734c2b9b   # replace with your commit point
    

    Then push the local branch to remote

    git push -u origin master
    

    Add back the default branch and branch protection, etc.

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