How can I recover from an erronous git push -f origin master?

Viewed 101186

I just committed the wrong source to my project using --force option.

Is it possible to revert? I understand that all previous branches have been overwritten using -f option, so I may have screwed up my previous revisions.

9 Answers

Yes you can recover commits after git push -f your_branch

Text from Doc:

Prune entries older than the specified time. If this option is not specified, the expiration time is taken from the configuration setting gc.reflogExpire, which in turn defaults to 90 days. --expire=all prunes entries regardless of their age; --expire=never turns off pruning of reachable entries (but see --expire-unreachable).

So you can do:

1- git reflog

enter image description here

2- you choose Head_Number does you want recover with git reset –hard HEAD@{HEAD-NUMBER}

enter image description here

3- you can see all commits on this head by git cherry -v branch_name

4- in the end you should force push git push -f branch_name

OR

1- get the number of SHA from your GIT client (interface)

git reset --hard commit_SHA

2- force push

git push -f your_branch

Hope this helps

Another way to recover the lost commit or even to figure out what commits were lost, if the previous push came not from your local repo, is to look at your CI machine.

If you have a job which tests the master branch after every commit (or series of consecutive commits), which you should have, you can have a look what it was testing last. That is the commit you need to restore.

The CI machine may even keep a local clone of the repo, from which you may be able to perform this recovery.

Source: probably Continuous Delivery: Reliable Software Releases through Build, Test, and Deployment Automation (Addison-Wesley Signature Series (Fowler))

For people in really bad situations like I was (for example, if you're getting bad object errors when running git reset --hard):

I wrote a script called treesaver that pulls all your files from the GitHub API as a last resort. Here's how to use it:

  1. Clone the treesaver script and cd to it.
  2. Find the SHA string of the tree you'd like to restore by accessing https://api.github.com/repos/<your_username_or_org>/<repo>/events.
  3. In the payload property corresponding to your push event, find the commit you'd like to revert to and click on its url.
  4. Under commit.tree, copy the tree's url.
  5. Run python3 main.py <tree_url> <path_to_save_to>.

For example, in my case, I would run:

python3 main.py https://api.github.com/repos/anthonykrivonos/my-repo/git/trees/1234567 .

Of course, PRs welcome.

Here you can read decisions https://evilmartians.com/chronicles/git-push---force-and-how-to-deal-with-it

The second one helped me. I did wrong these commands

1) (some-branch) git pull -> correct command was git pull origin some-branch

2) (some-branch) git push -f origin some-branch

After these commands I lost three commits. To recover them I looked to terminal where I did wrongly 'git pull' and have seen there output like

60223bf...0b258eb some-branch -> origin/some-branch

The second hash 0b258eb was exactly what I needed. So, I took this hash and produce command

git push --force origin 0b258eb:some-branch
Related