Skip to content

### How to restore main branch to previous state after wrong merge #144684

Discussion options

You must be logged in to vote

There are several ways to restore your main branch to its previous state. I'll provide both simple and advanced solutions:

1. Using git reset (If changes haven't been pushed to remote)

git checkout main
git reset --hard HEAD~1   # Goes back 1 commit (the merge commit)

2. Using git revert (Safer option, especially if changes are already pushed)

git checkout main
git revert -m 1 <merge-commit-hash>

3. Step-by-step solution (Recommended approach)

  • First, find the merge commit:
git log --merges
  • Choose your preferred method:
For local-only changes: Use reset method (#1)
For pushed changes: Use revert method (#2)
  • After fixing, push the changes:
git push origin main --force  # For reset…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by haroshi-hub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Codespaces Your development environment, in the cloud. Run VS Code and code on GitHub's cloud platform, Question
2 participants