TExcelTAD
Home › Git › GitHub Branch

GitHub Branch

Branches exist on the server too. On GitHub you can create one from the branch dropdown on the repository page (type a new name and choose Create branch), or from the Branches page. Teammates push branches there all the time. This lesson shows how remote branches appear in your local repository.

Local, remote and remote-tracking branches

Kind Example Lives Moves when
Local branch main, feature/menu your repository you commit, merge, reset
Branch on the remote main on GitHub the server someone pushes
Remote-tracking branch origin/main, origin/feature/menu your repository (read-only bookmark) you fetch/pull/push

A remote-tracking branch is your last known snapshot of the server's branch. You don't commit on it; you fetch to update it.

A branch created on GitHub

Here a teammate (the website clone, standing in for the GitHub interface) creates a branch update-prices on the server. After a fetch, it shows up in your repository as origin/update-prices:

bash · try it yourself
git init -q --bare ~/github/cafe.git
mkdir cafe && cd cafe && git init -q
printf 'tea 2.50\ncoffee 3.00\n' > prices.txt && git add . && git commit -q -m "Add prices"
git remote add origin ~/github/cafe.git && git push -q -u origin main

# "Create branch: update-prices" on GitHub, plus a commit on it
git clone -q ~/github/cafe.git ~/website && cd ~/website
git switch -q -c update-prices
sed -i 's/3.00/3.20/' prices.txt && git commit -q -am "Raise coffee price"
git push -q -u origin update-prices

cd ~/cafe
git fetch
echo "--- local branches:"
git branch
echo "--- remote-tracking branches:"
git branch -r
echo "--- all:"
git branch -a
From /home/learner/github/cafe
 * [new branch]      update-prices -> origin/update-prices
--- local branches:
* main
--- remote-tracking branches:
  origin/HEAD -> origin/main
  origin/main
  origin/update-prices
--- all:
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/update-prices

git branch only lists local branches, -r the remote-tracking ones, and -a both. origin/HEAD -> origin/main records which branch is the server's default branch. Git sets it when you clone, and recent versions also update it on fetch.

Looking at a remote branch without switching

You can inspect a remote-tracking branch like any other commit, without creating a local branch:

bash · try it yourself
git init -q --bare ~/github/cafe.git
mkdir cafe && cd cafe && git init -q
printf 'tea 2.50\ncoffee 3.00\n' > prices.txt && git add . && git commit -q -m "Add prices"
git remote add origin ~/github/cafe.git && git push -q -u origin main
git clone -q ~/github/cafe.git ~/website && cd ~/website
git switch -q -c update-prices
sed -i 's/3.00/3.20/' prices.txt && git commit -q -am "Raise coffee price"
git push -q -u origin update-prices

cd ~/cafe
git fetch -q
git log --oneline main..origin/update-prices
git show origin/update-prices:prices.txt
git diff --stat main origin/update-prices
2aac8a5 Raise coffee price
tea 2.50
coffee 3.20
 prices.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Listing branches on the server

git ls-remote --heads asks the server directly, while git remote show origin gives a summary including which local branches track which remote ones:

bash · try it yourself
git init -q --bare ~/github/cafe.git
mkdir cafe && cd cafe && git init -q
echo "menu" > menu.txt && git add . && git commit -q -m "Add menu"
git remote add origin ~/github/cafe.git && git push -q -u origin main
git push -q origin main:refs/heads/winter-menu
git ls-remote --heads origin
git remote show origin
9645eefb0adaa2a386ae687b74a40f80287c8b75	refs/heads/main
9645eefb0adaa2a386ae687b74a40f80287c8b75	refs/heads/winter-menu
* remote origin
  Fetch URL: /home/learner/github/cafe.git
  Push  URL: /home/learner/github/cafe.git
  HEAD branch: main
  Remote branches:
    main        tracked
    winter-menu tracked
  Local branch configured for 'git pull':
    main merges with remote main
  Local ref configured for 'git push':
    main pushes to main (up to date)

git push origin main:refs/heads/winter-menu created a branch on the server from your main without creating it locally, which is what clicking Create branch on GitHub does.

Test yourself

What is origin/feature in your local repository?

  1. A branch you can commit on directly
  2. A remote-tracking branch: your last-fetched view of the server's feature branch
  3. A tag
  4. A branch that exists only on GitHub

Which command lists both local and remote-tracking branches?

  1. git branch
  2. git branch -r
  3. git branch -a
  4. git remote -v

A teammate created a branch on GitHub a minute ago, but git branch -r doesn't show it. Why?

  1. Branches created on GitHub are private
  2. You haven't fetched since it was created
  3. You need to clone again
  4. It was deleted