Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git lost commits

Tags:

git

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?

like image 359
ggg Avatar asked Jul 16 '11 16:07

ggg


People also ask

How can recover the lost commit in git?

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.

How do I restore a commit?

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.

How do I revert lost Changes in git?

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.

Does git ever delete commits?

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.


2 Answers

To get your HEAD back in the right place:

  1. git reflog to get a list of where HEAD has been lately.
  2. git show sha1 to find the spot you want your HEAD to be.
  3. Once you find the commit you want, 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!

like image 52
Andy Avatar answered Oct 16 '22 01:10

Andy


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 
like image 42
Mark Locklear Avatar answered Oct 16 '22 02:10

Mark Locklear