简体   繁体   中英

Restore a deleted Commit

I was working on a project today, and i committed a month worth of changes, then I did a major change in the project which broke the whole project.

So I reset to the previous commit using

git reset --hard <commit>

and then

git push origin HEAD --force

after than all my previous changes were gone because I choose the wrong commit to reset to (not sure). and whenever i write any git command now i get (not a git repository).

and I opened Bit Bucket and both commits are gone. Is there ANYWAY to restore that commit PLEASE?

The project is a groovy on Grails project. I'm using intelliJ

Your local repository keeps copies of commits for at least 30 days by default. The repository over on Bitbucket probably does not.

If you have not erased your local repository, look there for the commits. Use git reflog or git reflog <branchname> . The reflog for HEAD holds the hash IDs that HEAD represented in the past. The reflog for the given branch name, such as master , holds the hash IDs that that branch name represented in the past.

If you were on branch xyzzy when you did the git reset --hard <commit> , xyzzy@{1} represents the hash ID that xyzzy held before this git reset --hard , so git reflog xyzzy will show you the commit you want. You can now git reset --hard <commit> to that hash ID, pushing all the numbers up by one, ie, what was xyzzy@{1} is now xyzzy@{2} and the wrong commit hash ID you picked last time is now xyzzy@{1} . Meanwhile xyzzy (aka xyzzy@{0} ) is now the hash ID you just picked this time.

You can now git push origin HEAD or git push origin xyzzy to send this hash ID (and the commit(s), if they've lost it/them) to the Git at Bitbucket, and ask them to set their xyzzy to this hash ID. You will only need --force if this operation loses some commit(s) on their end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM