Pushing only specific branches to GIT remotes
Isolating branches to specific remotes is helpful when you want different branches to be visible in different repositories, such as for feature-based repos or user-specific visibility. I learned many of these tips while building launchCourseKit, and they’ve been crucial in helping me manage branch-specific workflows. Let’s walk through how to set up these branch-specific remotes.
Step 1: Adding Branch-Specific Remotes
We'll first add separate remotes for each branch.
# In the main repo directory
cd /path/to/your-repo
# Add a remote for the `single-course` branch
git remote add single-course <single-course-repo-url>
# Add a remote for the `multi-course` branch
git remote add multi-course <multi-course-repo-url>
Now each branch has its own remote repository. In the next step, we’ll look at fetching and preparing these branches for pushing.
Step 2: Fetching and Checking Out Branches
Before pushing, make sure you’re working with the latest updates in each branch.
# Fetch all updates from the main repository
git fetch origin
# Check out the single-course branch to make it the active branch
git checkout single-course
After this, repeat for the multi-course
branch if needed.
Step 3: Pushing Only Specific Branches to Their Remotes
Now, we’ll push each branch to its corresponding remote, ensuring only the relevant branch is visible.
Pushing single-course
Branch
To push only the single-course
branch to the single-course
remote:
git push single-course single-course:main
This command pushes the single-course
branch to the main branch of the single-course
repository, keeping all other branches private.
Pushing multi-course
Branch
Similarly, for the multi-course
branch:
# Switch to the multi-course branch
git checkout multi-course
# Push to the multi-course remote
git push multi-course multi-course:main
Only the multi-course
branch will now be visible in the multi-course
repo.
Bonus: Adding Branch Protections on GitHub
For further control, set branch protections to restrict access to these branches.
- Go to Settings > Branches in your GitHub repository.
- Add a new protection rule for each branch to enforce security measures like pull request reviews.
This setup ensures isolated branch visibility and control over each branch in GitHub.
That’s it, folks! Now you know how to set up branch-specific visibility with Git remotes. These insights have been incredibly useful, especially while building launchCourseKit.