1

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?

Zoe
  • 27,060
  • 21
  • 118
  • 148
luis.ap.uyen
  • 1,314
  • 1
  • 11
  • 29

1 Answers1

1

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
user43968
  • 2,049
  • 20
  • 37
  • Thank you! I didn't think it was so easy. I was convinced I had to check out. Didn't realize it was a matter of a reset. – luis.ap.uyen Aug 17 '18 at 14:24
  • 1
    I recommend to use gitk or other graphical tool to see the git tree. Launch it with gitk --all& . With right click you can achieve the same result and make les mistake – user43968 Aug 17 '18 at 14:26
  • So could I think of resetting vs checking out as moving the current branch tip vs not moving it? – luis.ap.uyen Aug 17 '18 at 14:30
  • 1
    Maybe on master if you had right-click two commit before you would have seen createNewBranch or reset master here. I suppose you will have made the right choice. But this will have cause the two commit in a state without branch and with right click on it you would have seen create new branch… But be careful !! if you leave commit without branch you can lose them (git reflog can help). It would have been better to create the branch before like in my answer. – user43968 Aug 17 '18 at 14:36