41

I was not currently on any branch when I commited my changes. I didn't really notice the message and checked out another branch.

How can I retrieve my changes? I can't merge or checkout, since there is no branch to merge from.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Sewdn
  • 613
  • 2
  • 7
  • 13
  • do you still have the console output from when you committed? did it contain the SHA1 of the commit? If so, you have the commit object, which is all you need – rlc May 18 '11 at 17:12
  • 4
    I... didn't know you could ever *not* be in a branch... – Ignacio Vazquez-Abrams May 18 '11 at 17:13
  • 3
    @Ignacio most common case of not being on a branch is when you checkout a commit that it's not of the tip of a branch – CharlesB May 18 '11 at 17:19
  • 4
    @Ignacio: and it's very useful! You can jump about between commits in your history without moving any branch tip - particularly useful, for example, when trying to find a good commit for `git bisect`. "detached HEAD" is the name of this state since HEAD is pointing directly to a commit rather than pointing to one indirectly via a particular ref. – Mark Longair May 18 '11 at 18:40
  • Does this answer your question? [What happens to git commits created in a detached HEAD state?](https://stackoverflow.com/questions/9984223/what-happens-to-git-commits-created-in-a-detached-head-state) – mkrieger1 Jul 14 '22 at 18:52

7 Answers7

74

You can use git reflog to get the commit hash of the commit that you did while in "no branch" ( a detached HEAD) and merge that in to the branch that you are currently in ( master maybe)

Something like git merge HEAD@{1}

You can also git rebase -i and "pick" the commit you want from the reflog.

manojlds
  • 290,304
  • 63
  • 469
  • 417
12

I was in a similar state after committing some work:

Lilith:Manager KelSolaar$ git status

Not currently on any branch.

I issued a git log to see my last commit hash:

Lilith:Manager KelSolaar$ git log

commit 49984303037e970d637161c3154b7fd7d6ae3a43 Author: KelSolaar Date: Wed Oct 5 22:41:31 2011 +0100

Introduce new "QObject" components category and rename existing ones to "Def

I then checked out my master branch:

Lilith:Manager KelSolaar$ git checkout master

Previous HEAD position was 4998430... Introduce new "QObject" components categorie and rename exising ones to "Default" and "QWidget".

Switched to branch 'master'

And I finally merged using the commit hash:

Lilith:Manager KelSolaar$ git merge 49984303037e970d637161

Updating 141bc69..4998430

Fast-forward

src/manager/component.py | 2 +-

...

Kel Solaar
  • 3,660
  • 1
  • 23
  • 29
10

git checkout - will switch you back to the previous branch:

Thu Feb 21 12:50 AM /src/test ((08f84f4...)) $ git checkout master

Warning: you are leaving 1 commit behind, not connected to
any of your branches:

  08f84f4 Fix everything

Switched to branch 'master'

Thu Feb 21 12:50 AM /src/test (master) $ git checkout -

HEAD is now at 08f84f4... Fix everything

Thu Feb 21 12:50 AM /src/test ((08f84f4...)) $
Max Nanasy
  • 5,871
  • 7
  • 33
  • 38
  • 2
    I think this is the best answer. Also worth mentioning, you can check your current branch using `git branch` – Adam Fowler Jun 18 '14 at 16:50
  • This answers the question with the simplest solution. `git checkout -` (just to highlight it again) – N8TRO May 20 '15 at 15:19
2

Your commit isn't gone, you can recover by asking git to show you the hidden commits, and put them back in a temporary branch.

See this answer for instructions.

Community
  • 1
  • 1
CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 2
    It's easier than that in this case, since the reflog will have the commit - [manojlds's answer](http://stackoverflow.com/questions/6048425/not-currently-on-any-branch-git-commit-checkout-when-not-in-any-branch-did-i/6048609#6048609) describes that. – Mark Longair May 18 '11 at 18:42
2

Use "git reflog" it shows the commit hashes of the results of your git command history. You can then "git co hash" and when you've found the right one, set/make a branch for it.

karmakaze
  • 34,689
  • 1
  • 30
  • 32
0

The example

Branches list

Here, I have just 1 local branch: main. The other branches are from origin.

Trying to checkout to a commit

Click to checkout

Message:

message

It is because that is a commit, not a branch. I clicked on Cancel

Creating a new branch

Creating a new branch

Trying to "checkout" the new branch to the commit

checkout to the new branch

Same result

result 1

Reset branch to this commit

reset branch to this commit

Pop up window

enter image description here

Result

result - reset branch

Same try with main (previously, checkout to main branch)

reset main to a commit

Result:

enter image description here

So, in my case, the desired behaviour was "Reset", not "Checkout"

I can reset a branch to any commit.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Hernando N
  • 305
  • 3
  • 7
-5

You're never "not on any branch". You might have been on the branch called master, but you WERE on a branch when you committed. So that commit is SOMEWHERE.

Use git log to look at history. You can use git reset to go back in time (including, optionally, leaving the changes in your working directory).

Dan Ray
  • 21,623
  • 6
  • 63
  • 87
  • 13
    This is wrong - you *can* not be on a branch. This state is known as "detached HEAD" because `HEAD` points to a particular commit's hash rather than to a branch name. Any commits you make with detached HEAD won't advance a branch tip, but will still be accessible for some time via the reflog. – Mark Longair May 18 '11 at 18:37
  • Not true. Right now "$ git status" gives me "# Not currently on any branch." (instead of the usual "# On branch master"), followed by "# Changes to be committed:" etc. – weronika Jan 21 '12 at 00:50