GLM 4.5 Air

Complete Guide: Create Git Branch & Push to Remote

Learn how to create, push, and track Git branches in remote repositories. Complete guide with examples for setting up branch tracking and troubleshooting common issues.

Question

How to create, push, and track a new local branch in a remote Git repository

How do I:

  1. Create a local branch from another branch (via git branch or git checkout -b).

  2. Push the local branch to the remote repository (i.e. publish), but make it trackable so that git pull and git push will work.

GLM 4.5 Air

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

There are two primary methods to create a local branch from an existing branch in Git:

Method 1: Using git branch and git checkout

bash
# 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)

bash
# 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):

bash
# 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)

bash
# 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:

  1. Pushes your local branch to the remote
  2. Sets up tracking so future git pull and git push commands work without arguments

Method 2: Push without tracking setup

bash
# 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:

bash
# 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:

bash
# 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:

  1. git pull automatically fetches from the tracked remote and merges into your local branch
  2. git push automatically pushes to the tracked remote branch
  3. Branch status shows relationship with remote (ahead, behind, diverged)

Checking tracking status

bash
# 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:

bash
# Change the upstream branch
git branch --set-upstream-to=origin/new-remote-name feature-branch

Or remove tracking entirely:

bash
# Remove the upstream reference
git branch --unset-upstream feature-branch

Common Scenarios and Best Practices

Creating a feature branch from main/master

bash
# 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:

bash
# 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

bash
# 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:

bash
# 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:

bash
# 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:

bash
# 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:

bash
# 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

bash
# 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:

  1. Use git checkout -b <branch-name> to create and switch to a new local branch in one command.
  2. Always push your new branch with git push -u origin <branch-name> to establish tracking.
  3. Tracking enables git push and git pull to work without specifying remote branches.
  4. Regular synchronization with git fetch and git rebase helps maintain clean project history.
  5. 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.