git-sync-main
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Sync Main
Git Sync Main
同步最新主分支代码到本地并切换到主分支。支持两种场景:fork 的项目从 official remote 同步,自己的项目从 origin 同步。
Sync the latest main branch code to the local machine and switch to the main branch. Supports two scenarios: syncing forked projects from the official remote, and syncing your own projects from origin.
触发条件
Trigger Conditions
当用户要求同步上游代码、更新主分支、或拉取最新主分支代码时使用此 skill。
Use this skill when the user requests to sync upstream code, update the main branch, or pull the latest main branch code.
执行步骤
Execution Steps
-
检查 remote 配置:,确认是否存在
git remote -vremote。official -
根据是否存在 official remote 分两种情况执行:
-
Check remote configuration:to confirm if the
git remote -vremote exists.official -
Execute in two scenarios based on whether the official remote exists:
情况一:存在 official remote(fork 的项目)
Scenario 1: official remote exists (forked project)
-
检测主分支名称:通过或查看本地分支,确定主分支名称(
git remote show official或main)。master -
拉取 official 最新代码:。
git fetch official -
切换到本地主分支:。
git checkout <main-branch> -
同步代码:,将本地主分支重置到 official 最新状态。
git reset --hard official/<main-branch> -
推送到 origin:,同步自己 fork 的远端主分支。
git push origin <main-branch> --force-with-lease
-
Detect main branch name: Determine the main branch name (or
main) viamasteror by checking local branches.git remote show official -
Pull latest code from official:.
git fetch official -
Switch to local main branch:.
git checkout <main-branch> -
Sync code:to reset the local main branch to the latest state of official.
git reset --hard official/<main-branch> -
Push to origin:to sync the main branch of your own forked remote.
git push origin <main-branch> --force-with-lease
情况二:不存在 official remote(自己的项目)
Scenario 2: official remote does not exist (your own project)
-
检测主分支名称:通过查看本地分支,确定主分支名称(或
main)。master -
拉取 origin 最新代码:。
git fetch origin -
切换到本地主分支:。
git checkout <main-branch> -
同步代码:,将本地主分支同步到 origin 最新状态。
git pull origin <main-branch>
-
Detect main branch name: Determine the main branch name (or
main) by checking local branches.master -
Pull latest code from origin:.
git fetch origin -
Switch to local main branch:.
git checkout <main-branch> -
Sync code:to sync the local main branch to the latest state of origin.
git pull origin <main-branch>
共同步骤
Common Steps
- 验证结果:,展示最新的几条 commit 确认同步成功。
git log --oneline -5
- Verify results: to display the latest few commits and confirm successful sync.
git log --oneline -5
注意事项
Notes
- 始终使用 作为上游 remote 名称(而非
official),以保持一致性。upstream - 对于 fork 项目,推送到 origin 时使用 而非
--force-with-lease,更安全地强制推送。--force - 如果当前有未提交的改动,先提示用户处理(stash 或 commit),避免丢失工作。
- Always use as the upstream remote name (instead of
official) to maintain consistency.upstream - For forked projects, use instead of
--force-with-leasewhen pushing to origin for safer forced pushes.--force - If there are uncommitted changes currently, prompt the user to handle them first (stash or commit) to avoid losing work.