10

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.

9
  • Are you sure you reset back to the correct commit? Commented Mar 21, 2013 at 6:29
  • according to git log, yes Commented Mar 21, 2013 at 6:31
  • Does git status say there are changes to be committed? What are they? Commented Mar 21, 2013 at 6:38
  • git status says app/assets/images/.DS_Store.orig is untracked and I can add it. Commented Mar 21, 2013 at 6: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. Commented Mar 21, 2013 at 6:46

4 Answers 4

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.
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.