Question

I cloned a repository, made some changes and committed a few files, then used git pull to fetch remote modifications. Realizing it had automerged some changes, I wanted to go back to my latest (local) commit, to just git fetch remote and merge manually.

So, I used git log to find my latest commit, and git reset --hard my_commit_hash. But now, I’m back at square one, with every single one of my commits lost. How can I go back to my latest one?

Was it helpful?

Solution

If you actually made commits, and somehow went to a commit that was not actually your latest, then you can use the reflog to get back to where you were. But it sounds more like you added files with git add and didn't actually git commit them. In which case, your problem is much harder. Added files will have created objects in your object database which you can find with git fsck, but restoring them requires a little more work than you would otherwise need. So which situation are you in?

edit:

Once you see the commit you want in the reflog, say for instance HEAD@{3}, all you have to do is git reset --hard HEAD@{3}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top