git: what to do if you commit to no branch

Using git, you’ll sometimes find that you’re not on any branch. This usually happens when you’re using a submodule inside another project. Sometimes you’ll make some changes to this submodule, commit them and then try to push them up to a remote repository:

ed$ git commit -m "My excellent commit"
[detached HEAD d2bdb98] My excellent commit
 3 files changed, 3 insertions(+), 3 deletions(-)
ed$ git push origin master
Everything up-to-date

Er, what? Everything is not up to date – I just made changes! The clue is in the first part of the commit response – [detached HEAD d2bdb98]. This just means that we’ve made a commit without actually being on any branch.

Luckily, this is easy to solve – all we need to do is checkout the branch we should have been on and merge in that commit SHA:

ed$ git checkout master
Previous HEAD position was d2bdb98... My excellent commit
Switched to branch 'master'
ed$ git merge d2bdb98
Updating 88f218b..d2bdb98
Fast forward
 ext-mvc-all-min.js |    2 +-
 ext-mvc-all.js     |    2 +-
 view/FormWindow.js |    2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

Once we got onto the master branch, we just called git merge with the SHA reference for the commit we just made (d2bd98), which applied our commit to the master branch. The output tells us that the commit was applied, and now we can push up to our remote repository as normal:

ed$ git push origin master
Counting objects: 11, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 692 bytes, done.
Total 6 (delta 4), reused 0 (delta 0)
To git@github.com:extmvc/extmvc.git
   88f218b..d2bdb98  master -> master

This had me puzzled for a while so hopefully it’ll save someone banging their head against a nearby wall.

22 Responses to git: what to do if you commit to no branch

  1. David says:

    Thanks man, you saved me. I keep getting into trouble working with so many foreign submodules – I always forget that I shouldn’t work on their code because I end up with changes on “no branch”.

  2. Gergely says:

    This saved my life just now, thanks πŸ™‚
    I don’t think I did it with a submodule, though (I don’t even know what that is). I believe it was an uncautious tag creation with EGit then a tag deletion from the command line, and I don’t recall switching back to master… but I started coding anyway x)

  3. Sean says:

    Thank you. Great explanation.

  4. Barry Rountree says:

    Thanks for posting this. The “no branch” can also show up if you’ve checked out a specific commit without creating a branch.

  5. Chris says:

    Thanks man! You saved my day πŸ™‚

  6. Thanks Spence. You did indeed keep me from banging my head against the wall.

  7. Daniel says:

    Thanks Spence. You did indeed keep me from banging my head against the wall. (2)
    Hopefully I won’t lost commits anymore. πŸ˜€

  8. Denis says:

    I really love you (and I am not gay). Thank you !!!

  9. Adam says:

    Thanks Ed. I’m not sure how I ended up committing to an empty branch but I’m glad that I found your blog post.

  10. Tom says:

    Thanks for saving me again and again. I already bookmarked your blog post, because I constantly forget to checkout the correct branch before commiting…

  11. raultm says:

    As I can see with all the comments you have saved a lot of re-work.

    Thanks!

  12. David says:

    Thank you!!!!

  13. thijs says:

    lifesaver !! works like a charm πŸ™‚

  14. Jeff says:

    Thought this problem was gonna take me awhile to fix, but after finding this post it only took a few seconds. Thanks a lot!

  15. Kang Fan says:

    Very nice!

  16. Pingback: What to do if you want to commit and push git submodule updates | Nubits - … just a few more web bits

  17. Dimension says:

    Thanks, for this very helpful information!

  18. Shekhar says:

    Yes you saved me.

  19. mileszhou says:

    What if you have lost the SHA of that “detached commit” (so to say)?

  20. WL says:

    Many thanks for your post. I got into this state when checking out an older commit (as the latest one is broken and waiting for someone to fix), and continued working/modifying files and forgot about it… same question as to what if you have lost the SHA of the detached commit and already did git checkout to an existing branch? how do your “find” the lost commit/SHA?

  21. janom says:

    Many thanks!

  22. edspencer says:

    @WL and @mileszhou look up git reflog – it is a way to find all of the SHAs for all of your commits – even if you think you’ve lost them!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: