Search code examples
gitbranchcommitgit-checkoutgit-detached-head

Go back two commits but keep the two last in new branch


I want to go back two commits, which would lead to a detached head, with two commits after HEAD. However I would like these two commits to keep them in a new branch, and master pointing to the checked out commit.

How should I do this?


Solution

  • I suppose you are talking about master going 2 commit before Pretty easy stay on master at the tip of your branch. No need to detach head.

    D---E---F---G master
    

    Create the new branch git branch myNewBranch

                 master
                |
    D---E---F---G
                |
                myNewBranch
    

    then reset master two commit before git reset --hard HEAD~2

       master
        |
    D---E---F---G
                |
                myNewBranch