Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not currently on any branch + git commit + checkout when not in any branch. Did i loose my changes?

Tags:

git

commit

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.

like image 876
Sewdn Avatar asked May 18 '11 17:05

Sewdn


People also ask

Does git checkout lose local changes?

Checkout an Existing Branch Generally, Git won't let you checkout another branch unless your working directory is clean, because you would lose any working directory changes that aren't committed.

What happens if I switch branches without committing?

when you switch to a branch without committing changes in the old branch, git tries to merge the changes to the files in the new branch. If merging is done without any conflict, swithing branches will be successful and you can see the changes in the new branch.

Does git checkout use current branch?

The "checkout" command can switch the currently active branch - but it can also be used to restore files. The most common use case for "checkout" is when you want to switch to a different branch, making it the new HEAD branch.

What happens when you git checkout a branch?

The git checkout command lets you navigate between the branches created by git branch . Checking out a branch updates the files in the working directory to match the version stored in that branch, and it tells Git to record all new commits on that branch.


2 Answers

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.

like image 170
manojlds Avatar answered Oct 11 '22 21:10

manojlds


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 +-

...

like image 30
Kel Solaar Avatar answered Oct 11 '22 23:10

Kel Solaar