Home > Net >  How can I recover last commit after git push force
How can I recover last commit after git push force

Time:09-28

I have a project repository on bitbucket with master branch. This branch has some commit that have been committed by my team (the latest commit was not committed by me).

I just did the git push --force without knowing that it would delete all the commit after the latest pull I did. So now my team's commit just deleted and the latest commit was the git push --force I just did.

Is there a way I could recover my team's commit after I did the git push --force ?

I have read some solution and it says by doing git reflog then doing git reset –hard HEAD@{HEAD-NUMBER}. But I'm afraid, could it recover the latest commit or just recover my latest commit (not the latest commit that my team did) ?

CodePudding user response:

Git reflog will not help you as you did not pull changes of your teammates and forcefully removed them from remote by pushing your changes.

Your teammates who have pushed original commits that you have forcefully removed still have them in the their local branch.

they can force push their branch again and overwrite your changes with that what was there before. Then you have to fetch their changes and merge them to your branch.

CodePudding user response:

Had you fetched before you force pushed? If so, you should have it here:

git reflog origin/master

Also, when you force push, it tells you the old and new hash (with an ... or something between) so if you have the console open still, you have the old hash, and might be able to get it.

Your team mate that pushed that commit will still have it on their computer if you don't have it on yours.

They will almost always be able to see it like this

git reflog origin/master

And then they can just force push the original hash back

git push origin [original-hash]:master

Also in future, always use --force-with-lease, so that you know that you are not deleting something that you haven't fetched... so if you make a mistake, whatever you deleted is on your computer somewhere

  • Related