I made a lot of changes to a project of mine, so I wanted to push the changes to a temporary branch just in case, but I pushed to the main one instead. I try to revert with something like
git push -f origin last_commit_before_changes:master
I wasn't expecting it to make changes locally, so by mistake I overwritten everything (including my backup.....) and I had not already pushed to the correct branch. Is it possible to fix this stupid mistake ? Is the diff still stored somewhere ? It is not shown when I do git log
.
You can tell I'm not very experienced with git and I know I made multiple mistakes leading to this situation, don't be rude about it. Thanks.
CodePudding user response:
git push -f origin last_commit_before_changes:master
This just do: forcefully update remote remote master
with local branch last_commit_before_changes
.
Assuming that your local master was up to date you can just do:
git push -f origin master:master
If you master was behind (since you working with others) then just do this command and ask all coworkers to do (note no option -f
):
git push origin master
This should restore everything.
More complex way is to check logs from:
git reflog
and find there what was hash of origin/master
before forceful push.
Then this should fix it:
git push -f origin <hash found in reflog>:master
Note that most commands in git
are none destructive. Old commits are there just hidden for UI. git retains everything for some long period or until you enforce garbage collection.