I was trying to fix a problem in git and accidentally used git reset --hard to some previous commit. So now I cannot get back to the old head.
However I did clone the repository before I did this, and so I pushed the missing commits back to the original. This seemed to work yesterday, but today I see that the original is still stuck on an old commit and the new ones seemingly don't exist. Trying to push the new commits from the clone again don't work as git tells me everything is up to date.
How do I fix this?
git revert <sha-1>"Undo" the given commit or commit range. The reset command will "undo" any changes made in the given commit. A new commit with the undo patch will be committed while the original commit will remain in the history as well.
The process for recovering a deleted commit is quite simple. All that is necessary is to use `git reflog` to find the SHA value of the commit that you want to go to, and then execute the `git reset --hard <sha value>` command.
If you are using any JetBrains software, right click and go to "Local History > Show History". From here, you should see a list of local edit history separate from the Git history. You can choose any version to revert to.
To remove the last commit from git, you can simply run git reset --hard HEAD^ If you are removing multiple commits from the top, you can run git reset --hard HEAD~2 to remove the last two commits. You can increase the number to remove even more commits.
To get your HEAD back in the right place:
git reflog
to get a list of where HEAD has been lately.git show sha1
to find the spot you want your HEAD to be.git merge
to get your master back into the right spot.Some explanation: In a git
commit there is nothing pointing one commit to the one that happend after it. When you reset the HEAD, you pointed it to an older commit. Your previous head is now dangling without anything pointing to it.
We use reflog
to see where HEAD has been lately. Once it is set back to where you want it, you point the master, or some other, branch back to that spot and all is well!
I did it a little differently. I did...
git reflog 3bd79d2 HEAD@{2}: checkout: moving from edbfb06528c43586a0e0e10a73051e06980b9281 to master edbfb06 HEAD@{3}: commit: added general comments for rubric f8ca172 HEAD@{4}: checkout: moving from 904d63bf08f6f6b1494bfa473b158b9509b18423 to 904d63b HEAD@{10}: commit: updated results page and csv 933f2a6 HEAD@{11}: commit: updates f56e6cd HEAD@{12}: clone: from [email protected]:xxxx.git
...in this case my "added general comments for rubric" was the commit that I lost. Now that I have the commit ID I used cherry-pick to get it back...
git cherry-pick edbfb06
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