Isaac
Isaac

Reputation: 12874

How to make checked-out branch to become master

I ran command git checkout {previousCommit}, and continue-ing working from there. Reason is something messed up with HEAD version and I wished to go back to earlier state and re-do.

So now that I created a branch previousCommit, and done updating, I wish to take everything into master again. While on branch previousCommit, I made 2 commit.

And now when I ran command git checkout master, I got below message and I'm not sure what to do

git checkout master
Warning: you are leaving 2 commits behind, not connected to
any of your branches:

  70a95ef someCommitMessage
  05ebd0f someCommitMessage

If you want to keep them by creating a new branch, this may be a good time
to do so with:

 git branch <new-branch-name> 70a95ef

Switched to branch 'master'
Your branch is up to date with 'origin/master'.

Probably I should give a clearer picture:

Basically I have 10 commits in total on master branch, lets call it commit 1...commit 10

Now master is latest with commit 10, and I executed a command git checkout commit 7, and so git created a branch commit 7 for me, and I continuing my work from commit 7, and made 2 commits on this branch.

Now my objective is to have this 2 commits replace commit 8,9,10 on master, is that possible?

I know I should've git reset hard commit 7 earlier, but I'm wondering anything that I can do on current state?

Upvotes: 2

Views: 446

Answers (2)

Arjan
Arjan

Reputation: 9874

If you do git checkout {previousCommit}, you are in a detached head state. That means you are not on any branch at that time. So the commits you made are not in any branch and cannot be reached after you switched to the master branch.

Git actually tells you how to create a branch that includes those commits:

git branch <new-branch-name> 70a95ef

Then you can use the new branch and continue working in that later, or merge it into master.

Note that commits that are not reachable at all (because they are not part of any branch/tag) are likely to be removed by garbage collection after some time. You can fix the issue today, but it may not be possible for this case in (for example) one month. It's best to create a branch now, you could always delete the branch later if you decide you don't need it.

Upvotes: 2

OznOg
OznOg

Reputation: 4722

on master, do:

git rebase 70a95ef

this will cherry-pick for you the commits of master above 70a95ef

that said, you may notice that you did not do what you think, you were not on the previousCommit branch when you created you two new commits, as git tells you.

Upvotes: 0

Related Questions