If you're here, you probably ran the dreaded git push --force
and then realized you destroyed your repository. In such a situation, you may be panicking or losing your sanity (likely both). Don't fret. Just follow these instructions:
A force push doesn't actually delete anything, it just changes references. So the first step is to find the last "good" commit hash.
git reflog show remotes/origin/main
# Or for those of you who thought the switch to main was "too heated":
git reflog show remotes/origin/master
The output should look something like this.
89b85b3 remotes/origin/master@{0}: fetch --append --prune origin: forced-update
32c0dbf remotes/origin/master@{1}: update by push
15c8e79 remotes/origin/master@{2}: update by push
b4ff13f remotes/origin/master@{3}: update by push
In this specific example, the last non-screwed up commit is the one with the hash 32c0dbf
.
Run this command, replacing 32c0dbf
with the hash you found:
git checkout 32c0dbf
Now, just run the following to start a new branch based off of the commit:
git checkout -b temp_branch
Now, run this command to push your fixed branch. Interestingly, you have to use force pushing for this.
git push --force origin temp_branch:main
# Or for those of you who thought the switch to main was "too heated":
git push --force origin temp_branch:master
At this point, your remote main
/master
branch is fixed. But your local main
/master
branch is still ruined. So let's delete it!
git branch -D main
# Or for those of you who thought the switch to main was "too heated":
git branch -D master
If you still need some of the code in your ruined main
/master
branch, cherry-pick it instead of deleting it.
git branch -m main messed_up_main
# Or for those of you who thought the switch to main was "too heated":
git branch -m master messed_up_master
Now, simply run this command to make your temporary branch your main one.
git branch -m temp_branch main
# Or for those of you who thought the switch to main was "too heated":
git branch -m temp_branch master
I simply organized instructions here for your convenient reference. The original guide is available at https://www.borfast.com/blog/2014/10/19/how-to-undo-a-git-push-force-and-undelete-things/.