Git Reset and Branch Merge Guide: Master Version Rollback and Branch Management
Git Common Rollback and Branch Merge Operations Guide
This guide outlines the standard operational procedures for rolling back branches to specific commits, force pushing, and merging rollback branches to the main branch during project development.
1. Rollback Branch to Specific Commit
1.1 View Commit History
git log --oneline
Find the target commit hash value (e.g., 0d177db
).
1.2 Rollback Branch to Specific Commit
Local rollback only (does not affect remote):
git checkout 0d177db
This puts you in a “detached HEAD” state. It’s recommended to create a new branch to continue development:
git checkout -b restore-0d177db
Force rollback current branch (overwrites history):
git reset --hard 0d177db
Push to remote (use with caution, will overwrite remote history):
git push -f origin branch-name
2. Local and Remote Branch Synchronization
Local operations do not automatically sync to GitHub; you need to execute push:
- Normal push (does not overwrite history):
git push origin branch-name
- Force push (overwrites remote history):
git push -f origin branch-name
3. Merge Rollback Branch to Main Branch
Assuming you’ve created a restore-0d177db
branch.
3.1 Switch to Main Branch
git checkout main
3.2 Merge Rollback Branch
git merge restore-0d177db
If there are conflicts, resolve them and then:
git add .
git commit
3.3 Push Main Branch to Remote
git push origin main
4. Completely Replace Main Branch with Rollback Branch (Optional)
If you want the main branch content to be exactly equal to restore-0d177db
(discarding main’s existing history), you can use:
git checkout restore-0d177db
git branch -f main
git checkout main
git push -f origin main
5. Important Notes
- Force push (
-f
) will overwrite remote history; communicate with your team beforehand when collaborating. - When merging branches, conflicts need to be resolved manually if encountered.
- It’s recommended to make backups before operations.