Sync the latest main branch code to local and switch to the main branch. Supports two scenarios: forked projects sync from the official remote, and your own projects sync from origin.
Trigger Conditions
Use this skill when the user requests syncing upstream code, updating the main branch, or pulling the latest main branch code.
Execution Steps
-
Check remote configuration:
git remote -v, confirm whetherofficialremote。 -
Execute in two cases depending on whether an official remote exists:
Case 1: official remote exists (forked project)
-
Detect the main branch name: via
git remote show officialor by checking local branches, determine the main branch name (mainormaster)。 -
Pull the latest official code:
git fetch official。
-
Switch to the local main branch:
git checkout <main-branch>。 -
Sync code:
git reset --hard official/<main-branch>, reset the local main branch to the latest official state. -
Push to origin:
git push origin <main-branch> --force-with-lease, sync the remote main branch of your own fork.
Case 2: official remote does not exist (your own project)
-
Detect the main branch name: by checking local branches, determine the main branch name (
mainormaster)。 -
Pull the latest origin code:
git fetch origin。 -
Switch to the local main branch:
git checkout <main-branch>。 -
Sync code:
git pull origin <main-branch>, sync the local main branch to the latest origin state.
Common Steps
- Verify the result:
git log --oneline -5, display the latest few commits to confirm the sync succeeded.
Notes
- Always use
officialas the upstream remote name (instead ofupstream), to maintain consistency. - For forked projects, when pushing to origin use
--force-with-leaseinstead of--force, for a safer force push. - If there are currently uncommitted changes, first prompt the user to handle them (stash or commit), to avoid losing work.