255

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?

Erik B
  • 40,889
  • 25
  • 119
  • 135

10 Answers10

271

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
theheadofabroom
  • 20,639
  • 5
  • 33
  • 65
araqnid
  • 127,052
  • 24
  • 157
  • 134
  • 24
    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 May 18 '12 at 09:35
  • 7
    If committed, get hash of last commit. Checkout the branch you wish to be at, and `git merge _hash_` – Daniel May 31 '12 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". – thinkanotherone Dec 21 '12 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 `. – Erik B Jul 08 '13 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](http://stackoverflow.com/a/9871180/60075), because making a branch of your current point is a good first step to keep track of where your code is. – Craig McQueen Jan 30 '14 at 23:42
  • I don't understand how you can be not on a branch in the first place. Git is so bizarre. – Monica Heddneck Feb 22 '19 at 22:18
  • me helped this https://stackoverflow.com/questions/7124486/what-to-do-with-commit-made-in-a-detached-head#answer-7124513 – Hartmut Pfarr Mar 03 '21 at 13:34
  • use 'rebase' replace 'merge' is a good work – defend orca Jun 15 '22 at 07:11
171

this helped me

git checkout -b newbranch
git checkout master
git merge newbranch
git branch -d newbranch
babay
  • 4,689
  • 1
  • 26
  • 40
  • 28
    If you've already made multiple commits, this is what you need to do. – Benjamin Oakes Mar 27 '12 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 Mar 29 '12 at 00: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. – Konstantin Schubert Dec 01 '13 at 07: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 Dec 02 '13 at 09:01
  • valid, working solution as of 9/28/15. last command optional - it deletes the newly created branch. – user1870776 Sep 28 '15 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
m_messiah
  • 2,115
  • 2
  • 15
  • 12
  • 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 Jul 08 '13 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. – Matti Jokipii Dec 02 '13 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 Jan 28 '15 at 21:39
14

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.

Ian
  • 11,280
  • 3
  • 36
  • 58
  • 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. – invinciblemuffi Jan 09 '20 at 11:45
13

Leaving another way here

git branch newbranch
git checkout master 
git merge newbranch 
Observer
  • 710
  • 10
  • 14
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
Abizern
  • 146,289
  • 39
  • 203
  • 257
  • 1
    Could you tell me how to do that? – Erik B Jan 20 '11 at 18:18
  • is there a way to update an existing submodule this default branch mode? update `gitmodules` perhaps? – Hertzel Guinness May 21 '12 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 May 21 '12 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.

Erik B
  • 40,889
  • 25
  • 119
  • 135
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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
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
Erik B
  • 40,889
  • 25
  • 119
  • 135
  • 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 Oct 04 '17 at 07: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.

kelvin
  • 1,421
  • 13
  • 28