When synchronizing and rebasing the remote main branch, follow these key principles:
- Workspace safety first: Always check for uncommitted changes before starting to avoid data loss.
- Remote branch clarity: Use the standard
origin/main(ororigin/masterif applicable) as the remote target branch. - Conflict handling guidance: Provide clear steps for resolving conflicts if they arise during rebase.
Execution steps (in order):
Check workspace status:
- Run
git statusto inspect the current workspace. - If there are uncommitted changes:
- Prompt the user to either commit them first, or stash them with
git stash(can be restored later withgit stash pop).
- Prompt the user to either commit them first, or stash them with
- Proceed only when the workspace is clean (no uncommitted changes).
- Run
Fetch latest remote changes:
- Execute
git fetch originto retrieve the latest code from the remote repository without merging.
- Execute
Perform rebase:
- Run
git rebase origin/mainto rebase the current local branch onto the latest remote main branch. - (Note: Replace
origin/mainwithorigin/masterif the remote default branch uses "master".)
- Run
Handle rebase conflicts (if any):
- If Git reports conflicts:
- Inform the user which files have conflicts.
- Instruct the user to manually edit the conflicting files to resolve the conflicts.
- After resolving, run
git add <resolved-file(s)>to stage the fixes. - Execute
git rebase --continueto proceed with the rebase.
- If the user wants to abort the rebase: Run
git rebase --abortto return to the state before the rebase started.
- If Git reports conflicts:
Verify rebase result:
- After rebase completes, run
git log --oneline -5to confirm the commit history is correctly rebased.
- After rebase completes, run
Key notes:
- If you are unsure about the remote branch name, run
git branch -rto list all remote branches. - It is recommended to create a backup branch before rebase (e.g.,
git branch backup-before-rebase) for safety.