Search code examples
gitgithubversion-control

Github - How do I go back to a commit which is 3 pull requests in the past and then restore the main branch as to that commit


I want to be able to restore the main branch to a commit, as how is was 3 merges before. Also, although its not really important, I would like to keep all the changes that were done after that commit (the 3 merges) and store them in another branch (or branches). So basically just restoring the main branch as to how it was in the past whilst also keeping the changes that were made after. And if this can be done using the GitHub web ui that would be great.


Solution

  • The comment from user "nullptr" above is correct.

    I would add a couple of details to help avoid some confusion.

    1. Checkout the "main" branch so that HEAD is pointing to the main branch, and the creation of the new branch will point there.

    git checkout main

    1. create the new branch to save the previous state of main. I'm calling it save-old-main in my example.

    git checkout -b save-old-main

    1. You can now use the "branch" command to reset main. It is important that you do not have main as your current branch when you do this, because it is not allowed to do this to the branch you have checked out.

      git branch -f main hash-you-want-main-at