Git子模块:能否让主项目各分支追踪子模块对应分支?
Great question! You absolutely can configure your main project's branches to track matching branches in your submodule, and there are a couple of simple, reliable ways to pull this off.
手动配置分支追踪(最直接的方法)
This method lets you explicitly set each main project branch to track its corresponding submodule branch:
Set up the
masterbranch tracking- Switch to your main project's
masterbranch:git checkout master - Navigate to the submodule directory and switch it to its own
masterbranch:cd path/to/submodule && git checkout master && cd .. - Configure the submodule to track the
masterbranch for this main project branch:git config submodule.path/to/submodule.branch master - Commit this configuration to your main project's
masterbranch:git add .gitmodules && git commit -m "Configure submodule to track master branch in main project master"
- Switch to your main project's
Repeat for the
productionbranch- Switch to your main project's
productionbranch:git checkout production - Navigate to the submodule and switch it to its
productionbranch:cd path/to/submodule && git checkout production && cd .. - Set the tracking configuration:
git config submodule.path/to/submodule.branch production - Commit the change:
git add .gitmodules && git commit -m "Configure submodule to track production branch in main project production"
- Switch to your main project's
After this setup, whenever you're on the main project's master or production branch, running git submodule update --remote will automatically pull the latest commits from the matching submodule branch.
自动化分支切换(更便捷的长期方案)
If you want to avoid manually switching the submodule branch every time you change main project branches, you can use a Git post-checkout hook to automate this:
Create a
post-checkoutfile in your main project's.git/hooksdirectory with the following content:#!/bin/sh # Automatically switch submodule branch to match the main project's current branch # Get the current main project branch CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD) SUBMODULE_DIR="path/to/submodule" # Check if the submodule exists and the target branch exists in it if [ -d "$SUBMODULE_DIR/.git" ]; then if git -C "$SUBMODULE_DIR" show-ref --verify --quiet "refs/heads/$CURRENT_BRANCH"; then echo "Auto-switching submodule $SUBMODULE_DIR to branch $CURRENT_BRANCH" git -C "$SUBMODULE_DIR" checkout "$CURRENT_BRANCH" fi fiGive the file executable permissions:
chmod +x .git/hooks/post-checkout
Now, every time you switch branches in the main project, the hook will automatically switch the submodule to the same branch name (if that branch exists in the submodule). Pair this with the manual tracking configuration above, and you'll have a seamless workflow.
Quick Notes
- Make sure your submodule actually has both
masterandproductionbranches before setting this up—otherwise the configuration/hook won't have a branch to track. - If you want to merge the latest submodule changes directly into your main project's submodule reference, use
git submodule update --remote --mergeinstead of just--remote.
内容的提问来源于stack exchange,提问作者Emilio

