Git reset not working

Eric Baldwin picture Eric Baldwin · Mar 21, 2013 · Viewed 10.6k times · Source

I made a commit, pulled and merged some changes, and then made a second commit. When I wanted to go back to the first commit, I ran the command

git reset --hard <sha hash>

While the response was "HEAD is now at <sha hash>", my code looks just as it did before I ran that command. Usually, it changes to what I had before, but it looks like something isn't working correctly. Do I need to run a different command to unmerge before resetting head?


Extra info

When I run git status it says:

app/assets/images/.DS_Store.orig is untracked

and I can add it.

According to git reflog, I pulled before I made the commit hash1 (which I consider "before merge"). There is an sha hash2 for the pull (which git log did not show). When I dig hash1 and hash2, I see the changes I made and could reconstruct my original code from this. Still, this seems very strange. If I try to git reset to either of them, I cannot get my code from before the merge.

Answer

kenorb picture kenorb · Jul 29, 2016

The:

git reset --hard <sha-hash>

won't work when:

  • you're trying to reset untracked files (they're not part of your git repository),
  • some attributes are overridden by your git normalization file (.gitattributes),
  • you're using wrong hash or the hash is after your changes.

Maybe the solution would be to go back what you have on remote branch by:

git reset origin/master --hard

and rebase hash on top of it or git cherry-pick the commits which you really need.

If above doesn't help, you should also:

  • make sure your testing it on the right branch (git branch -a), not on detached one,
  • check for git reflog for more details about your history,
  • run git blame some/file to show at what revision your line of code was modified, this helps you to track the problem at which commit your code looks like you would not expect,
  • in case you don't like specific commit which was made before, you can revert it.