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.
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.
If you are not on that local repo where the forced push came from, at origin/master level there is no way to recover. But if you are lucky enough to use GitHub or GitHub for Enterprise, you can have a look to the REST API and retrieve lost commit as patch, example:
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))
If you know the commit hash, it's easy, just recreate your branch.
Delete the remote branch:
then recreate your branch with the following commands:
I did the same thing while undoing a last push for only one file. Ended up going to back to original state of the repository. I was using git commands from Linus as I had the local copy on Linux. Luckily that copy was still intact.
All I did was (after frantically making few more copies of the local repo):
(it said that origin/master was ahead by 68 commits, fine ... those were all the commits I deleted)
And everything got restored the way it was before I did forceful push. The most important thing to remember is never to do a git checkout . after you had forcefully pushed. But the best practice is to disable push option. I am never using it ever again. Learnt my lesson!!
The solution is already mentioned here
Git generally doesn't throw anything away, but recovering from this may still be tricky.
If you have the correct source then you could just push it into the remote with the
--force
option. Git won't have deleted any branches unless you told it to. If you have actually lost commits then take a look at this useful guide to recovering commits. If you know the SHA-1 of the commits you want then you're probably OK.Best thing to do: Back everything up and see what is still in your local repository. Do the same on the remote if possible. Use
git fsck
to see if you can recover things, and above all DO NOT rungit gc
.Above above all, never use the
--force
option unless you really, really mean it.