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?
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.
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).
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.
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.
The:
git reset --hard <sha-hash>
won't work when:
.gitattributes
),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:
git branch -a
), not on detached one,git reflog
for more details about your history,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,revert
it.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With