The branch is on the server. Your laptop does not have it until you fetch. Checkout is the local name you then work on, tied to that remote branch so pull and push know where to go.
This is not merge versus rebase. That choice is git merge vs rebase. Hub: Architecture.
Real-world analogy
The office has a folder with today's drawings. You can read it over someone's shoulder, which is checking out origin/feature and ending up detached. Or you take a copy, put your name on it, and write on the copy. That copy is your local branch. Push sends your copy back. Reading over the shoulder and then writing in the margin is how a commit disappears the next time you switch away.
Worked example
A teammate pushes feature/invoices. You run git checkout feature/invoices. Git says the path does not match any file, or it offers to create a branch and then complains it cannot see a start point. git branch -a does not list origin/feature/invoices. The remote on GitHub is ahead of your last fetch. git fetch origin downloads the ref. git switch -c feature/invoices origin/feature/invoices creates your local branch at that commit and sets upstream to origin/feature/invoices. git status says "Your branch is up to date with 'origin/feature/invoices'." Now pull and push have a destination.
| Command | What you get |
|---|---|
git fetch origin | The remote branch names, no local work branch yet |
git switch feature/invoices | Works when Git can match one remote branch of that name |
git switch -c feature/invoices origin/feature/invoices | A local branch you can commit on, tracking the remote |
git switch origin/feature/invoices | Detached HEAD. Do not commit here |
Code
git fetch origin
git switch -c feature/invoices origin/feature/invoices
If the branch was fetched before and Git already knows the name:
git fetch origin
git switch feature/invoices
Confirm the tracking link:
git status -sb
You want ## feature/invoices...origin/feature/invoices. If the second half is missing, the next push will ask you where to send it. Set it once:
git branch -u origin/feature/invoices
Do not commit while git status says "HEAD detached." Switch to a named branch first, then commit. The same fetch-then-switch is what you want before a container build that must use a review branch: Docker with Angular.