Git Pull Branch from GitHub
A teammate pushed a branch and asks you to try it. You need a local branch that tracks theirs, so you can run the code, add commits and pull their later updates.
Switching to a remote branch
If a remote-tracking branch origin/<name> exists and you have no local branch with that name, git switch <name> creates the local branch and sets up tracking automatically:
git init -q --bare ~/github/game.git
mkdir game && cd game && git init -q
echo "level 1" > levels.txt && git add . && git commit -q -m "Add first level"
git remote add origin ~/github/game.git && git push -q -u origin main
git clone -q ~/github/game.git ~/teammate && cd ~/teammate
git switch -q -c boss-fight
echo "boss: dragon" > boss.txt && git add . && git commit -q -m "Add dragon boss"
git push -q -u origin boss-fight
cd ~/game
git fetch -q
git switch boss-fight
git branch -vv
lsbranch 'boss-fight' set up to track 'origin/boss-fight'.
Switched to a new branch 'boss-fight'
* boss-fight 347c1e8 [origin/boss-fight] Add dragon boss
main 0d9c609 [origin/main] Add first level
boss.txt
levels.txt
git branch -vv shows each local branch's upstream in brackets. The long form of the same command is git switch -c boss-fight --track origin/boss-fight, which you need when the local name should differ from the remote one.
Pulling later updates
Because tracking is set up, git pull on that branch brings in your teammate's new commits:
git init -q --bare ~/github/game.git
mkdir game && cd game && git init -q
echo "level 1" > levels.txt && git add . && git commit -q -m "Add first level"
git remote add origin ~/github/game.git && git push -q -u origin main
git clone -q ~/github/game.git ~/teammate && cd ~/teammate
git switch -q -c boss-fight
echo "boss: dragon" > boss.txt && git add . && git commit -q -m "Add dragon boss" && git push -q -u origin boss-fight
cd ~/game && git fetch -q && git switch -q boss-fight
cd ~/teammate
echo "boss hp: 500" >> boss.txt && git commit -q -am "Give the boss hit points" && git push -q
cd ~/game
git pull
git log --onelineFrom /home/learner/github/game
347c1e8..95d7c4b boss-fight -> origin/boss-fight
Updating 347c1e8..95d7c4b
Fast-forward
boss.txt | 1 +
1 file changed, 1 insertion(+)
95d7c4b Give the boss hit points
347c1e8 Add dragon boss
0d9c609 Add first level
Fetching everything at once
git fetch downloads all branches of origin. git fetch --all does that for every remote. With --prune, Git also removes remote-tracking branches whose branch was deleted on the server:
git init -q --bare ~/github/game.git
mkdir game && cd game && git init -q
echo "level 1" > levels.txt && git add . && git commit -q -m "Add first level"
git remote add origin ~/github/game.git && git push -q -u origin main
git push -q origin main:refs/heads/old-experiment main:refs/heads/sound
git fetch -q
git branch -r
git -C ~/github/game.git branch -q -D old-experiment # someone deletes it on GitHub
git fetch --prune
git branch -r origin/HEAD -> origin/main
origin/main
origin/old-experiment
origin/sound
From /home/learner/github/game
- [deleted] (none) -> origin/old-experiment
origin/HEAD -> origin/main
origin/main
origin/sound
Tip:
git config --global fetch.prune truemakes every fetch prune, sogit branch -rnever lists branches that no longer exist.
Checking out a pull request
GitHub exposes every pull request as a hidden ref, refs/pull/<number>/head. You can fetch it into a local branch to test someone's PR, even from a fork you have no remote for:
git fetch origin pull/42/head:pr-42
git switch pr-42The GitHub CLI wraps this as gh pr checkout 42.
Test yourself
origin/fix-menu exists and you have no local fix-menu. What does git switch fix-menu do?
- Fails: the branch doesn't exist
- Creates a local fix-menu that tracks origin/fix-menu and switches to it
- Switches to origin/fix-menu in detached HEAD
- Deletes the remote branch
What does git fetch --prune do in addition to a normal fetch?
- Deletes local branches
- Removes remote-tracking branches whose branch no longer exists on the remote
- Compresses the repository
- Deletes old commits on the server