如何配置Git实现不同分支定向推送至对应GitHub仓库?
Absolutely! You can absolutely set this up with a single local repo holding both dev and prod branches—here's a step-by-step breakdown to make Git automatically push each branch to its correct target repository:
Step 1: Add Two Remote Repositories
First, link your local repo to both the private and public GitHub repos. Open your terminal and run these commands (replace placeholders with your actual repo URLs):
# Add the private repo as a remote named "private" git remote add private git@github.com:your-username/app-dev.git # Add the public repo as a remote named "public" git remote add public git@github.com:your-username/app-prod.git
Verify the remotes are set up correctly with git remote -v—you should see both private and public listed with their respective URLs.
Step 2: Configure Automatic Branch-to-Remote Pushing
You have two solid options to make Git push each branch to the right remote without manual intervention:
Option A: Per-Branch Remote Configuration
Tell Git exactly which remote each branch should push to by setting branch-specific configs:
# Set the `dev` branch to push to the "private" remote git config branch.dev.remote private git config branch.dev.merge refs/heads/dev # Set the `prod` branch to push to the "public" remote git config branch.prod.remote public git config branch.prod.merge refs/heads/prod
Now, when you're on the dev branch and run git push, it will automatically push to private/dev. When on prod, git push will send changes to public/prod. No extra commands needed!
Option B: Automated Push with a Pre-Push Hook
If you want even more control (or want to avoid remembering which branch maps to which remote), use a Git pre-push hook. This script runs automatically every time you execute git push, checking your current branch and pushing to the correct remote.
- Create a file named
pre-pushin your local repo's.git/hooks/directory:
touch .git/hooks/pre-push chmod +x .git/hooks/pre-push # Make it executable
- Open the file and paste this script:
#!/bin/bash current_branch=$(git rev-parse --abbrev-ref HEAD) case "$current_branch" in dev) echo "Pushing dev branch to private remote..." git push private dev ;; prod) echo "Pushing prod branch to public remote..." git push public prod ;; *) echo "Unknown branch $current_branch—push canceled. Only dev/prod are auto-pushed." exit 1 ;; esac exit 0
Now, whenever you run git push, the hook will handle pushing to the correct remote based on your current branch.
Step 3: Maintain Separate GitHub Actions Workflows
To keep your private and public workflows distinct:
- In your local
devbranch, keep the.github/workflows/directory with your private Actions config (including any sensitive elements tied to the self-hosted runner). - In your local
prodbranch, replace or modify the workflows to match the public repo's requirements (no sensitive data, public-friendly actions, etc.).
When merging dev into prod, be careful to avoid overwriting the public workflows. Use git merge --no-ff to review changes manually, or set up a .gitignore rule in the prod branch to exclude the private workflow files (though manual review is safer for critical configs).
Key Notes & Troubleshooting
- Permissions: Ensure your local Git setup has access to both repos (via SSH keys or GitHub CLI auth) so pushes don't fail due to authentication issues.
- Remote Branch Sync: If the remote repos already have existing branches, run
git fetch private devandgit fetch public prodfirst to sync your local branches with the remotes. - Workflow Triggers: Double-check that each repo's Actions workflows are set to trigger on the correct branch (e.g., private repo runs workflows on
devpushes, public onprodpushes).
内容的提问来源于stack exchange,提问作者WoJ

