0

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) ?

user
  • 39
  • 9
  • 2
    You can only use `reflog` if that commit was ever on your machine - if not, whoever pushed it will have to run `reflog`. – jonrsharpe Sep 27 '21 at 13:23
  • @jonrsharpe then if I do `git reset –hard HEAD@{HEAD-NUMBER}` it will just recover to my commit that I have did ? – user Sep 27 '21 at 13:27

2 Answers2

3

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.

Dmitry
  • 353
  • 2
  • 8
  • ohhh, it must be a fun! – confiq Sep 27 '21 at 14:17
  • it is actually very simple as long as one tracks what is happening. Git is simple as long as you can trace what actions took place and in which order. – Dmitry Sep 27 '21 at 14:19
  • ohh sorry, I was sarcastic on part where you wrote "force push their [master] branch" :) You are correct though – confiq Nov 08 '21 at 13:41
2

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 --force-with-lease

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

Alex028502
  • 3,486
  • 2
  • 23
  • 50
  • hi, thanks for your answer. I've told my team, and when he's trying to do `git push origin [original-hash]:master` He get the error `! [rejected] [original-hash] -> master (non-fast-forward) error: failed to push some refs to [repo-url]` – user Sep 28 '21 at 04:46
  • sorry I didn't mean to literally put `[original-hash]`. I mean that when your colleague looks at `git reflog origin/master`, they will see a list of hashes down the left.. – Alex028502 Sep 28 '21 at 08:41
  • oh wait you knew that - yeah they have to force push; I will edit – Alex028502 Sep 28 '21 at 08:41
  • done - sorry about taht – Alex028502 Sep 28 '21 at 08:42