260

So I've done some work in the repository and when I'm about to commit I realize that I'm not currently on any branch.

This happens a lot when working with submodules and I am able to solve it, but the process is tedious and I've been thinking that there must be an easier way to do this.

Is there an easy way to get back on a branch, while keeping the changes?

10 Answers 10

276

If you have not committed:

git stash
git checkout some-branch
git stash pop

If you have committed and have not changed anything since:

git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}

If you have committed and then done extra work:

git stash
git log --oneline -n1 # this will give you the SHA
git checkout some-branch
git merge ${commit-sha}
git stash pop
8
  • 25
    if you've already committed: note the hash of the commit you made (use git show or git rev-parse HEAD), switch to the branch and then git cherry-pick followed by the commit hash.
    – araqnid
    Commented May 18, 2012 at 9:35
  • 7
    If committed, get hash of last commit. Checkout the branch you wish to be at, and git merge _hash_
    – Daniel
    Commented May 31, 2012 at 13:31
  • BE REALLY CAREFUL. IF YOU HAVE COMMITTED THE CHANGE and you follow these steps...You will see the message..."Your branch and origin/master have diverged". Commented Dec 21, 2012 at 18:13
  • 1
    @thinkanotherone If you have committed your changes already there would be nothing to stash and checking out a different branch is not the end of the world. The commit you just made is still there and you can merge it to that branch by using git merge <hash-of-the-commit-you-just-made>.
    – Erik B
    Commented Jul 8, 2013 at 13:38
  • This is risky in case you end up with merge conflicts, and you are a git beginner. I'd prefer babay's answer, because making a branch of your current point is a good first step to keep track of where your code is. Commented Jan 30, 2014 at 23:42
173

this helped me

git checkout -b newbranch
git checkout master
git merge newbranch
git branch -d newbranch
5
  • 28
    If you've already made multiple commits, this is what you need to do. Commented Mar 27, 2012 at 21:55
  • Haven't tried it, but it looks like it would work. However I think it's an unlikely scenario. I believe most people will realize that they're not on any branch before they commit and use the accepted answer to fix that.
    – Erik B
    Commented Mar 29, 2012 at 0:43
  • 2
    @ErikB I didn't even know there was such a thing as not being on a branch. This answer was very helpful for me. Commented Dec 1, 2013 at 7:28
  • 3
    lovely answer, actually just typed it in and worked, unlike pretty much everything else encountered as a GIT beginner - you might like to point out that newbranch is an arbritrary name and not intended to be replaced with a commit hash
    – Toni Leigh
    Commented Dec 2, 2013 at 9:01
  • valid, working solution as of 9/28/15. last command optional - it deletes the newly created branch. Commented Sep 28, 2015 at 16:57
26
git checkout master

That's result something like this:

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

1e7822f readme
0116b5b returned to clean django

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 1e7822f25e376d6a1182bb86a0adf3a774920e1e

So, let's do it:

git merge 1e7822f25e376d6a1182bb86a0adf3a774920e1e
3
  • I did not try it, but it seems like it would work just fine. I guess if you would run git gc between running those two commands you would lose those commits, but unless you're running git gc automatically this should be a fairly risk free approach. I would still go with babay's answer, but if you want to save yourself from writing two extra commands I guess this is the way to go.
    – Erik B
    Commented Jul 8, 2013 at 13:32
  • I had left the master branch at some point, that i'm not totally sure. I had commited my changes, there were no changes on the master. These instructions brought my changes to the master branch and everythings good. Commented Dec 2, 2013 at 14:49
  • +1 I was nervous about checking out another branch while leaving commits behind, seeing this message added confidence to go for it.
    – J-Dizzle
    Commented Jan 28, 2015 at 21:39
15

One way to end up in this situation is after doing a rebase from a remote branch. In this case, the new commits are pointed to by HEAD but master does not point to them -- it's pointing to wherever it was before you rebased the other branch.

You can make this commit your new master by doing:

git branch -f master HEAD
git checkout master

This forcibly updates master to point to HEAD (without putting you on master) then switches to master.

1
  • Worked perfectly as required. I had already staged the files and committed, but unable to push due to above error. Above two lines of code worked perfectly fine and pushed my code as expected. Commented Jan 9, 2020 at 11:45
13

Leaving another way here

git branch newbranch
git checkout master 
git merge newbranch 
3
  • 3
    @ErikB He may have done multiple commits. In that case, see babay's answer. Commented Mar 27, 2012 at 22:02
  • @BenjaminOakes That may be so, but these git commands aren't even valid.
    – Erik B
    Commented Mar 29, 2012 at 0:40
  • @ErikB Definitely true. :) babay's answer is the valid form of what Alex seems to have been trying to do. Commented Mar 29, 2012 at 12:35
10

Alternatively, you could setup your submodules so that rather than being in their default detached head state you check out a branch.

Edited to add:

One way is to checkout a particular branch of the submodule when you add it with the -b flag:

git submodule add -b master <remote-repo> <path-to-add-it-to>

Another way is to just go into the submodule directory and just check it out

git checkout master
3
  • 1
    Could you tell me how to do that?
    – Erik B
    Commented Jan 20, 2011 at 18:18
  • is there a way to update an existing submodule this default branch mode? update gitmodules perhaps? Commented May 21, 2012 at 14:44
  • @HertzelGuinness Not really. The submodule is checked out at a particular commit sha. A branch is just a pointer to the sha and the sha that it points to can change. This isn't useful because it doesn't freeze the state of the checked out submodule. Checking out a branch is just a convenience if you are making changes to the submodule.
    – Abizern
    Commented May 21, 2012 at 15:01
5

I recently ran into this problem again. It's been a while since I last worked with submodules and having learned more about git I realized that simply checking out the branch you want to commit on is sufficient. Git will keep the working tree even if you don't stash it.

git checkout existing_branch_name

If you want to work on a new branch this should work for you:

git checkout -b new_branch_name

The checkout will fail if you have conflicts in the working tree, but that should be quite unusual and if it happens you can just stash it, pop it and resolve the conflict.

Compared to the accepted answer, this answer will save you the execution of two commands, that don't really take that long to execute anyway. Therefore I will not accept this answer, unless it miraculously gets more upvotes (or at least close) than the currently accepted answer.

2

The following method may work:

git rebase HEAD master
git checkout master

This will rebase your current HEAD changes on top of the master. Then you can switch the branch.


Alternative way is to checkout the branch first:

git checkout master

Then Git should display SHA1 of your detached commits, then you can cherry pick them, e.g.

git cherry-pick YOURSHA1

Or you can also merge the latest one:

git merge YOURSHA1

To see all of your commits from different branches (to make sure you've them), run: git reflog.

1

I know I told babay in 2012 that I thought it was unlikely that someone wouldn't realize that they weren't on a branch and commit. This just happened to me, so I guess I have to admit that I was wrong, but considering that it took until 2016 for this to happen to me, you could argue that it is in fact unlikely.

Anyway, creating a new branch is overkill in my opinion. All you have to do is:

git checkout some-branch
git merge commit-sha

If you didn't copy the commit-sha before checking out the other branch, you can easily find it by running:

git reflog
1
  • it's a rare (unlikely) issue for one man. It didn't happen to me since 2012. But if you multiply the chance to amount of git-users... It will be very likely. It might happen every day to someone. :)
    – babay
    Commented Oct 4, 2017 at 7:41
-1

The best way that I found was to copy the files that I have made changes to to a separate folder, then delete the repository folder I currently have on my computer, then clone the main repository and put the files back.

Not the answer you're looking for? Browse other questions tagged or ask your own question.