How to create, push, and track a new local branch in a remote Git repository
How do I:
-
Create a local branch from another branch (via
git branch
orgit checkout -b
). -
Push the local branch to the remote repository (i.e. publish), but make it trackable so that
git pull
andgit push
will work.
How to Create, Push, and Track a New Local Branch in a Remote Git Repository
Brief Answer
To create a local branch from another branch in Git, use git branch <new-branch>
followed by git checkout <new-branch>
, or the combined command git checkout -b <new-branch>
. Then push it to the remote with git push -u origin <new-branch>
to establish tracking, which allows subsequent git pull
and git push
commands to work without specifying the remote branch each time.
Contents
- Creating a Local Branch from Another Branch
- Pushing the Branch to the Remote Repository
- Setting Up Tracking for the Branch
- Common Scenarios and Best Practices
- Troubleshooting Branch Issues
Creating a Local Branch from Another Branch
There are two primary methods to create a local branch from an existing branch in Git:
Method 1: Using git branch
and git checkout
# First, create the new branch from the current branch
git branch feature-branch
# Then switch to the new branch
git checkout feature-branch
Method 2: Using git checkout -b
(combined approach)
# Create and switch to the new branch in one command
git checkout -b feature-branch
If you want to create a branch from a specific other branch (not your current one):
# Create and switch to a new branch from main
git checkout -b feature-branch main
# Or from a different branch
git checkout -b feature-branch develop
Important note: When creating a new branch, Git uses the exact state of the source branch at that moment. If you’ve made commits since checking out the source branch, those won’t be included in the new branch unless you’ve pushed them to the remote.
Pushing the Branch to the Remote Repository
After creating your local branch, you’ll want to share it with your team by pushing it to the remote repository. Here’s how to do it:
Method 1: Push with tracking setup (recommended)
# Push the branch and set up tracking in one command
git push -u origin feature-branch
The -u
flag (or --set-upstream
) does two things:
- Pushes your local branch to the remote
- Sets up tracking so future
git pull
andgit push
commands work without arguments
Method 2: Push without tracking setup
# Push the branch but don't set up tracking
git push origin feature-branch
With this approach, you’ll need to specify the remote branch for subsequent operations:
# Pull changes
git pull origin feature-branch
# Push changes
git push origin feature-branch
Method 3: Set up tracking after pushing
If you’ve already pushed the branch without setting up tracking:
# Set up tracking after the fact
git branch --set-upstream-to=origin/feature-branch feature-branch
Setting Up Tracking for the Branch
Understanding branch tracking is crucial for efficient Git workflows. When a local branch tracks a remote branch:
git pull
automatically fetches from the tracked remote and merges into your local branchgit push
automatically pushes to the tracked remote branch- Branch status shows relationship with remote (ahead, behind, diverged)
Checking tracking status
# See relationship between your branches and remote ones
git branch -vv
Output example:
* feature-branch abc1234 [origin/feature-branch: ahead 1] Added new feature
main def5678 [origin/main] Main branch updates
Modifying tracking configuration
If you need to change what your local branch tracks:
# Change the upstream branch
git branch --set-upstream-to=origin/new-remote-name feature-branch
Or remove tracking entirely:
# Remove the upstream reference
git branch --unset-upstream feature-branch
Common Scenarios and Best Practices
Creating a feature branch from main/master
# Switch to main/master first to ensure you're up to date
git checkout main
git pull origin main
# Create your feature branch
git checkout -b feature-123-new-login
# Push with tracking
git push -u origin feature-123-new-login
Collaborating on an existing remote branch
If a team member has already created a remote branch and you need to work on it:
# Fetch the latest from all remotes
git fetch origin
# Create a local tracking branch
git checkout -b feature-123-new-login origin/feature-123-new-login
# Now you can push and pull without specifying the remote
git push
git pull
Renaming a local branch
# Rename the local branch
git branch -m feature-123-new-login feature-123-improved-login
# Update the remote reference
git push origin --delete feature-123-new-login
git push -u origin feature-123-improved-login
Keeping branches in sync
Regular synchronization helps prevent merge conflicts:
# Update all local branches with remote changes
git fetch --all
# Rebase your feature branch on latest main
git checkout feature-branch
git rebase main
Troubleshooting Branch Issues
“fatal: The current branch [branch-name] has no upstream branch”
This error occurs when you try to git push
or git pull
without tracking setup:
# Solution: Set up tracking
git push --set-upstream origin branch-name
“fatal: Couldn’t find remote ref refs/heads/branch-name”
This happens when trying to push to a non-existent remote branch:
# Solution: Create the remote branch first
git push -u origin branch-name
Diverged branches (local and remote have different commits)
When your local and remote branches have different commits not reachable from each other:
# Option 1: Force push (use with caution!)
git push --force-with-lease origin branch-name
# Option 2: Pull and resolve conflicts
git pull origin branch-name
# Resolve conflicts
git add .
git commit
# Option 3: Rebase local changes on top of remote
git fetch origin
git rebase origin/branch-name
git push
Deleting remote branches
# Delete the remote branch
git push origin --delete branch-name
# Delete the local branch
git branch -d branch-name
Conclusion
Creating, pushing, and tracking a new local branch in a remote Git repository is a fundamental workflow for collaborative development. Here are the key takeaways:
- Use
git checkout -b <branch-name>
to create and switch to a new local branch in one command. - Always push your new branch with
git push -u origin <branch-name>
to establish tracking. - Tracking enables
git push
andgit pull
to work without specifying remote branches. - Regular synchronization with
git fetch
andgit rebase
helps maintain clean project history. - When working with others, communicate branch naming conventions to avoid confusion.
By mastering these branch operations, you’ll be able to collaborate more effectively on Git projects and maintain a clean, organized codebase.