Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git reset not working

Tags:

git

git-reset

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.

like image 741
Eric Baldwin Avatar asked Mar 21 '13 06:03

Eric Baldwin


People also ask

How do I completely reset git?

To hard reset files to HEAD on Git, use the “git reset” command with the “–hard” option and specify the HEAD. The purpose of the “git reset” command is to move the current HEAD to the commit specified (in this case, the HEAD itself, one commit before HEAD and so on).

How does git reset work?

To review, git reset is a powerful command that is used to undo local changes to the state of a Git repo. Git reset operates on "The Three Trees of Git". These trees are the Commit History ( HEAD ), the Staging Index, and the Working Directory. There are three command line options that correspond to the three trees.

Does git reset hard remove history?

They all rewrite Git history, and they all move the HEAD back, but they deal with the changes differently: git reset --soft , which will keep your files, and stage all changes back automatically. git reset --hard , which will completely destroy any changes and remove them from the local directory.


1 Answers

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.
like image 180
kenorb Avatar answered Oct 18 '22 22:10

kenorb