Git
version control
branch management
git pull
software development

How do you get git to always pull from a specific branch?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

To make git pull always pull from a specific remote branch, set the upstream tracking branch with git branch --set-upstream-to=origin/branch-name. This stores the tracking configuration in .git/config so that git pull (without arguments) knows which remote and branch to fetch from and merge into. You can also set this during git push -u origin branch-name or configure it manually in .git/config. The tracking relationship means git pull = git fetch origin + git merge origin/branch-name automatically.

Setting Upstream with --set-upstream-to

bash
1# Set the upstream branch for the current branch
2git branch --set-upstream-to=origin/main
3
4# Short form
5git branch -u origin/main
6
7# Verify the tracking relationship
8git branch -vv
9# * feature-x abc1234 [origin/main] Latest commit message
10
11# Now git pull always pulls from origin/main
12git pull
13# Already up to date. (or merges changes from origin/main)

After setting the upstream, git pull without arguments fetches from origin and merges origin/main into your current branch.

Setting Upstream During Push

bash
1# Push and set upstream in one command
2git push -u origin feature-branch
3# Equivalent to:
4# git push origin feature-branch
5# git branch --set-upstream-to=origin/feature-branch
6
7# After this, git pull always pulls from origin/feature-branch
8git pull

The -u (or --set-upstream) flag during git push sets the tracking relationship automatically. This is the most common way developers configure tracking — it happens naturally on the first push of a new branch.

Manual Configuration in .git/config

bash
1# View current config
2git config --local --list | grep branch
3
4# Set tracking manually
5git config branch.feature-x.remote origin
6git config branch.feature-x.merge refs/heads/main
7
8# The .git/config file now contains:
9# [branch "feature-x"]
10#     remote = origin
11#     merge = refs/heads/main
ini
1# .git/config — what it looks like
2[branch "feature-x"]
3    remote = origin
4    merge = refs/heads/main
5
6[branch "develop"]
7    remote = origin
8    merge = refs/heads/develop

The remote setting specifies which remote to fetch from, and merge specifies which remote branch to merge. Together they define what git pull does on that branch.

Pulling from a Different Branch Than You're On

bash
1# Your current branch is feature-x
2# But you want to always pull from origin/develop
3
4git branch --set-upstream-to=origin/develop feature-x
5
6# Now when on feature-x:
7git pull
8# Fetches origin/develop and merges into feature-x
9
10# One-time pull from a specific branch (no tracking change)
11git pull origin main
12# Pulls from origin/main this one time only

You can track any remote branch, not just one with the same name. This is useful when feature branches should stay in sync with a development branch.

Default Branch for New Branches

bash
1# Configure git to automatically set up tracking for new branches
2git config --global push.autoSetupRemote true  # Git 2.37+
3
4# Now 'git push' on a new branch automatically sets upstream
5git checkout -b new-feature
6git push  # Automatically pushes to origin/new-feature and sets tracking
bash
1# Older Git: configure default push behavior
2git config --global push.default current
3# 'git push' pushes to a remote branch with the same name
4
5git config --global branch.autoSetupMerge always
6# Automatically track the remote branch when creating local branches

Checking and Changing Tracking

bash
1# See all branches and their tracking info
2git branch -vv
3# * main       abc1234 [origin/main] Latest commit
4#   develop    def5678 [origin/develop] Feature complete
5#   feature-x  ghi9012 [origin/main: ahead 3] Work in progress
6
7# Remove tracking
8git branch --unset-upstream feature-x
9
10# Change tracking to a different branch
11git branch -u origin/develop feature-x
12
13# See what git pull would do (without doing it)
14git fetch --dry-run

Handling Multiple Remotes

bash
1# Add a second remote
2git remote add upstream https://github.com/original/repo.git
3
4# Track upstream/main instead of origin/main
5git branch -u upstream/main
6
7# Pull from upstream
8git pull
9# Fetches upstream/main and merges into current branch
10
11# Or specify explicitly for one-time use
12git pull upstream main

When working with forks, you typically have origin (your fork) and upstream (the original repo). Set tracking to upstream/main to stay in sync with the original project.

Configuring Pull Strategy

bash
1# Set pull to use rebase instead of merge
2git config --global pull.rebase true
3
4# Now git pull = git fetch + git rebase (instead of merge)
5git pull
6# Rebases your local commits on top of the fetched branch
7
8# Set for a specific branch only
9git config branch.feature-x.rebase true
10
11# Or use --rebase flag per-pull
12git pull --rebase

Common Pitfalls

  • No upstream set: Running git pull without an upstream configured produces the error "There is no tracking information for the current branch." Fix with git branch -u origin/branch-name or git push -u origin branch-name.
  • Tracking the wrong branch: If you set upstream to origin/main but intended origin/develop, git pull merges the wrong branch silently. Check tracking with git branch -vv and change with git branch -u origin/correct-branch.
  • Confusing git pull with git fetch: git pull = git fetch + git merge (or git rebase). If you only want to see what changed without merging, use git fetch followed by git log origin/main..HEAD.
  • Merge conflicts from tracking a different branch: If your feature branch tracks origin/main, every git pull merges main into your feature branch, creating merge commits. Use git pull --rebase to keep a linear history, or only pull from your feature's own remote branch.
  • push.default mismatch with tracking: With push.default=simple (the default since Git 2.0), git push refuses to push if the local and remote branch names differ. Set push.default=upstream if you intentionally track a differently-named branch.

Summary

  • Use git branch -u origin/branch-name to set which remote branch git pull fetches from
  • Use git push -u origin branch-name to set tracking during the first push
  • The tracking configuration is stored in .git/config under [branch "name"]
  • Use git branch -vv to see all branches and their tracked remotes
  • Set push.autoSetupRemote=true (Git 2.37+) for automatic tracking on new branches
  • Use git pull --rebase to rebase instead of merge when pulling from the tracked branch

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.