| English | 中文 |
After a force push overwrites a remote branch, first retrieve the correct commit.
If someone still retains the old branch locally, it can usually be recovered.
First notify the team to pause pushing to the related branch.
Then check locally:
git reflog
git log --oneline --decorate -20
You can also ask colleagues to check locally:
git reflog
git log --oneline --decorate -20
After confirming the good sha, first create a backup branch:
git branch backup-good-branch <good-sha>
Check the content:
git show --stat <good-sha>
git diff <bad-sha>..<good-sha>
<protected-branch> is the actual remote branch that was overwritten, such as main, master, or develop. Confirm the target, remote permissions, branch rules, and collaborator impact, then obtain authorization from the relevant owner before updating the remote:
git push origin <good-sha>:<protected-branch>
If the main branch is protected, it requires administrator handling.