I was attempting to pull a change into my repository using Git Tower. When I did so, there was a conflict and I mistakenly hit stage all (as I wanted to commit after resolving the conflict). When I did so, the conflict marked itself as resolved.
I wanted to manually resolve the change so I hit "Abort Merge", however, when I did this, It rolled back all my changes! Is there any way to get them back?
To expand on Alexander's answer with a simpler alternative: yes, if you've staged your changes then you can probably get your files back. When you run
git add
, files are actually added to Git's object database. At the moment that you do, git will put the file in the index:Note that the entry for bar.txt contains the object ID of the file. Git has actually added the file to its object database. In this case, Git has added it to the repository as a loose object:
These files will eventually be garbage collected (so indeed, do not explicitly run
git gc
). Thankfully, by default, this will happen in a matter of months, not days. Until these files are garbage collected, you can recover them.The easiest way to do this is to download and install the
git-recover
program in interactive mode:git-recover
looks for files in the object database that are not committed (or in the index). You can find out more aboutgit-recover
in the blog post announcing it.If you had anything staged to git, you probably should be able to get that back. (If you just changed working copy, you wouldn't be able to restore it.)
First of all: do not run
git gc
. Backup your repository and working copy before going ahead. (Make sure to backup.git
directory.) Also avoid closing terminal where this happened, and/or rebooting — if all fails, you have a chance to find stuff in history / memory.Anyway, first thing to try is:
It will print something like
Since you lost staged files and did not do a commit, you're interested in
dangling blob
entries.Run
git show <sha>
for each one — some of them should be your files.A very late answer that could simplify (a lot? comparing to other answers) the recover by letting you figure it out quicker which blobs should be recovered:
This command will create a file containing the hash and content of all the blobs that could be recovered.
That way, you could easily search into this file and recover the blobs you want with the given hash (with the command
git cat-file -p 8f72c7d79f964b8279da93ca8c05bd685e892756 > myFile.txt
)Disclaimer: this file could become huge and slow to create if you've got a lot of unreachable blobs.