9

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
Eric Baldwin
  • 3,341
  • 10
  • 31
  • 71
  • Are you sure you reset back to the correct commit? – Lily Ballard Mar 21 '13 at 06:29
  • according to git log, yes – Eric Baldwin Mar 21 '13 at 06:31
  • Does `git status` say there are changes to be committed? What are they? – Stephen Jennings Mar 21 '13 at 06:38
  • git status says app/assets/images/.DS_Store.orig is untracked and I can add it. – Eric Baldwin Mar 21 '13 at 06:40
  • 3
    Sorry, but I don't think there's enough information in your question to definitively explain what you're seeing. Maybe get the SHA-1 of the first and second commits (`git reflog` might help), then use `git diff [hash1] [hash2]` to see the difference between them. Then try using `git checkout [hash1]` and `git checkout [hash2]` to see if your working directly changes appropriately. – Stephen Jennings Mar 21 '13 at 06:46
  • @StephenJennings, 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 hash 2, I see the changes I made and could reconstruct my original code from this. Thanks for your help. Still, this seems very strange. If I try to git reset to either of them, I cannot get my code from before the merge – Eric Baldwin Mar 21 '13 at 07:07
  • Obviously I don't know much about the situation, but it sounds like you might be using `git reset` when you'd be better off using `git checkout`. If you want to pull out an old version of the code, you'd use checkout. Usually you won't do a hard reset unless you committed a mistake that you want permanently erased from history. I recommend reading [Reset Demystified](http://git-scm.com/2011/07/11/reset.html) just to make sure you don't accidentally lose history. – Stephen Jennings Mar 21 '13 at 07:17
  • Are you sure you aren't using an editor that is caching the files? Please provide more information regarding your use-case so that we can understand your situation. – Kaushik Shankar Jun 17 '13 at 22:59
  • Untracked files are not touched by `git reset --hard` – knittl Aug 23 '13 at 05:51

4 Answers4

2

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.
kenorb
  • 155,785
  • 88
  • 678
  • 743
0

I am sure the sha1 value you put in 'git reset --hard ' is the HEAD. If you want to reset the HEAD to the first commit,you should run command like this:

git reset --hard <first commit's sha1 value>

or:

git reset --hard <HEAD~1>  

this site maybe help you to know how 'git reset' work:git-reset

zhumaomao
  • 9
  • 3
0

I do not use the command line for reset since gitk (branch viewer) can do this. Sometimes reset does not work. I then delete all directories, leaving only .git folder. It often happens that some working files cannot be deleted. This is the problem why git cannot reset. You must close the programs the keep the files.

Val
  • 1
  • 8
  • 40
  • 64
0

I think you should enter the first 7 characters of SHA of the second commit, not the first one. For example, you have 2 commits:

commit 212323b232g2h3jj232j323kkk23 Author: Date: ...11:40 PM ...

commit abcdefb232g2h3kk239j183kkk24 Author: Date: ...10:00 PM ...

IF you want to reset the first commit you should enter: git reset abcdefb

Dung Lemanh
  • 74
  • 1
  • 12