Reputation: 2040
I created and checkout a branch "Deep" from the master branch. Master branch had some uncommitted changes at this point. While on the "Deep" branch, I checkout the last saved commit.
After that I checkout to "Master" Branch.
Using the checkout command overwritten all the unsaved changes on "Master" branch.
Is there any way I can retrieve the lost changes on "Master" branch which now has the code of the last saved commit. When I type the reflog command in terminal, I get the following information:
f3df3fe HEAD@{16}: checkout: moving from f3df3fee373a68b65543a952721a75b9765d321a to master
f3df3fe HEAD@{17}: checkout: moving from Deep to f3df3fee373a68b65543a952721a75b9765d321a
f3df3fe HEAD@{18}: checkout: moving from master to Deep
f3df3fe HEAD@{19}: commit: Node touch detection
aa47485 HEAD@{20}: commit: Area Manager Created
These are the commands that I used:
git checkout -b Deep (Switching from master to Deep branch)
git log (It gave me the list of all previous commits)
git checkout f3df3fee373a68b65543a952721a75b9765d321a (checkout last saved commit)
After this command, I get the following message in the terminal: "You are in 'detached HEAD' state. You can look around, make experimental changes and commit them, and you can discard any commits you make in this state without impacting any branches by performing another checkout.If you want to create a new branch to retain commits you create, you may do so (now or later) by using -b with the checkout command again. Example:git checkout -b "
Then I list all the branches and checkout the master branch
git branch
* (HEAD detached at f3df3fe)
Deep
master"
git checkout -f master
Listing all the branches again here
git branch (Detached head is gone here which I assume contains the local changes)
Deep
* master
I hope I can retrieve the lost code from here. Any help would be greatly appreciated.
Upvotes: 0
Views: 3181
Reputation: 33083
Detached head is gone here which I assume contains the local changes
Detached head does not contain the local changes because you did not commit them.
Also, you used the phrase "unsaved changes" in your question. This has a different meaning from "uncommitted changes". Uncommitted changes are ones that have not been committed to git. Unsaved changes are ones which have not even been saved to disk. You should familiarise yourself with the difference between these two concepts, for future reference.
Also, when you checked out the last committed revision (you should not say "last saved revision"), that changed git to detached head mode but did nothing else, because you were already on that revision. It is apparent that you do not understand how git works and need to familiarise yourself with basic git concepts.
In any case, you may be able to use undo or "local changes" in your editor/IDE (Eclipse has the concept of "local changes") to retrieve the lost changes.
Upvotes: 0
Reputation: 1135
You could try searching the dangling blobs.
Go back to master and do
$ git fsck --unreachable
This give you a list of dangling blobs.
unreachable blob 070204aa62dc0ef612f922a02d06d3....
unreachable blob 821c4ca73cb5c99f7fa5f23358e3a9....
unreachable blob b91c74fc1b5c0f8eb8ccfd1a8024c6....
Then do git Show b91c74fc1b5c0f8eb8ccfd1a8024c6....
This lets you see the content of the dangling blob but NOT THE NAME OF THE FILE. At this point you can continue investigate the content in the unreachable blob reference SHA1's until you see the content that's lost.
Then to write that content to a file just do
git show f0480 > myfile
This puts myfile, a .txt file, in the directory you are currently in. You can then copy paste the content or convert to the right MIME type for editing.
Hope this helps.
Upvotes: 2