Iso Iso - 3 years ago 270
Git Question

Go back to reset local commit

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?

Answer Source

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?


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}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download