Lesson 5 of 5 in Remotes & Collaboration

Remote branches

Story: The archive holds more timelines than main. Find them, follow them, and close the ones that are done.

origin has branches of its own, not just main. Your repository keeps a remote-tracking branch for each of them: origin/main, origin/menu and so on. You've been using origin/main since your first clone.

Seeing them

git branch lists only your own branches. git branch -r lists the remote-tracking ones, and git branch -a lists both:

$ git branch -a
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main
  remotes/origin/menu

origin/HEAD points at origin's default branch, here main.

A remote-tracking branch is your repository's record of where that branch was on origin the last time you talked to it. Only git fetch, git pull and git push move it. So when a teammate pushes a new branch, git branch -r shows it only after your next fetch. You never commit on origin/menu yourself: git keeps it up to date for you.

Working on a teammate's branch

To work on menu, you need a menu of your own. Switch to it by name:

$ git switch menu
branch 'menu' set up to track 'origin/menu'.
Switched to a new branch 'menu'

You had no menu, but origin/menu exists, so git creates menu at the same commit and makes origin/menu its upstream. From then on, git pull and git push on menu work just as they do on main. git switch -c menu origin/menu does the same, spelled out.

Deleting a branch on origin

When a branch has been merged and is finished, delete it on origin, so old branches don't pile up there:

$ git push origin --delete contact
To https://git.example/team/project.git
 - [deleted]         contact

Your origin/contact goes too. Your own contact stays until you delete it with git branch -d contact. Other clones keep their origin/contact for now: a plain git fetch doesn't remove it.

Deleting a branch doesn't delete its commits. If it was merged into main, they're still in main.

Adding a remote

origin is just the name git clone gives the repository you cloned from. A repository can have any number of remotes, and git remote add adds one, with a name and a URL.

That's how you publish a project you started with git init. It has no remote at all, so add one, then push with -u, because your main has no upstream yet:

git remote add origin https://git.example/you/website.git
git push -u origin main

A second remote works the same way. Say another team keeps its own copy of your project:

git remote add design https://git.example/design/project.git
git fetch design

Their branches arrive as design/main, design/new-look and so on. git remote -v lists every remote with its URL.

In short

Command What it does
git branch -r lists the remote-tracking branches
git branch -a lists your branches and the remote-tracking ones
git switch <branch> when you have no such branch but a remote does, creates one that tracks it
git push origin --delete <branch> deletes a branch on origin
git remote add <name> <url> adds a remote
git fetch <name> fetches from that remote

New commands

  • git branch -r
  • git branch -a
  • git push --delete
  • git remote add

Explainer: Remote-tracking branches, and more than one remote

project: your repo, you are hereorigin: remote host (bare), https://git.example/team/project.git99860cd Add a README99860cdd1c0363 Add the home paged1c03634607830 Style the heading4607830728cfea Add a contact page728cfeacdab82a Try a new lookcdab82a8a68956 Add a menu page8a68956contactmaindesign/mainorigin/HEADorigin/mainHEAD → menuorigin/menudesign/new-look99860cd Add a README99860cdd1c0363 Add the home paged1c03634607830 Style the heading4607830728cfea Add a contact page728cfea8a68956 Add a menu page8a68956mainmenu
The repository at the end of the Explainer.
Graph as text

project: your repo, you are here

  • HEAD → menu at 8a68956 "Add a menu page"

Branches and tags

  • contact at 728cfea "Add a contact page", tracks origin/contact
  • main at 728cfea "Add a contact page", tracks origin/main
  • menu at 8a68956 "Add a menu page", tracks origin/menu
  • design/main at 728cfea "Add a contact page"
  • design/new-look at cdab82a "Try a new look"
  • origin/HEAD at 728cfea "Add a contact page"
  • origin/main at 728cfea "Add a contact page"
  • origin/menu at 8a68956 "Add a menu page"

Commits, newest first

  1. 8a68956 "Add a menu page"; parent 728cfea; refs: HEAD → menu, origin/menu
  2. cdab82a "Try a new look"; parent 728cfea; refs: design/new-look
  3. 728cfea "Add a contact page"; parent 4607830; refs: contact, main, design/main, origin/HEAD, origin/main
  4. 4607830 "Style the heading"; parent d1c0363
  5. d1c0363 "Add the home page"; parent 99860cd
  6. 99860cd "Add a README"; no parent

origin: remote host (bare), https://git.example/team/project.git

Branches and tags

  • main at 728cfea "Add a contact page"
  • menu at 8a68956 "Add a menu page"

Commits, newest first

  1. 8a68956 "Add a menu page"; parent 728cfea; refs: menu
  2. 728cfea "Add a contact page"; parent 4607830; refs: main
  3. 4607830 "Style the heading"; parent d1c0363
  4. d1c0363 "Add the home page"; parent 99860cd
  5. 99860cd "Add a README"; no parent

Not shown: design, teammate.

Challenge 1 of 3

Story: The Partner opened a new timeline in the archive. Have a look at it from your desk.

A teammate has pushed a new branch, menu, to origin. Make a menu branch of your own from it that tracks origin/menu, and switch to it.

Unlocks after Branching & Merging.

Terminal

Unlocks after Branching & Merging.

This Lesson can be played once Branching & Merging is complete. Its text and Explainer are open to everyone.