tech katta

Remotes: fetch, pull, push and upstream branches

Lesson 17 of 23 · 5 min readFree

In this lesson

Connect your repository to a shared one, understand the difference between fetch and pull, read ahead and behind, and fix a rejected push.

On this page
  1. Why this matters
  2. A remote is a named address
  3. Push and the upstream branch
  4. Fetch: look without touching
  5. Pull: fetch plus integrate
  6. A rejected push
  7. Remote branches and cleaning up
  8. Try it

So far everything has happened on one machine. Real teams share code through a remote: another copy of the repository, usually on a hosting service such as GitHub, that everybody pushes to and pulls from. This lesson explains how your copy talks to that remote, and why Git sometimes refuses to push.

Why this matters#

"Rejected: fetch first", "diverged", "no upstream branch": these are the first Git messages that stop most new developers. They are not errors in the usual sense. They are Git telling you that your copy and the shared copy have gone different ways, and asking you to combine them before it will overwrite anything. When you understand fetch, pull, push and upstream, those messages become routine.

To make the lesson runnable on one machine, we created a bare repository, which is a repository with no working files that is only a hub, and two clones of it called dev1 and dev2, playing two developers. A bare repository works exactly like the one a hosting service keeps for you. All output below is real.

A remote is a named address#

A remote is a short name that stands for a URL. The default name is origin, because it is the one Git creates when you clone. A new repository created with git init has no remotes, so pushing fails:

Shell
git remote -v
git push
Text
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

Add one with git remote add, and list them with git remote -v. (We used a relative path for the lesson; a real URL looks like git@example.com:team/api.git or https://example.com/team/api.git.)

Shell
git remote add origin ../origin.git
git remote -v
Text
origin	../origin.git (fetch)
origin	../origin.git (push)

Your repository stores the remote's address and its short name in .git/config. When you clone, this is all set up for you, and Git also checks out the default branch.

Push and the upstream branch#

git push sends your commits to the remote. The first time, tell Git which remote branch your local branch relates to. That relation is called the upstream.

Shell
git push -u origin main
git status -sb
Text
To ../origin.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
## main...origin/main

The -u flag (short for --set-upstream) records that local main tracks origin/main. From then on, plain git push, git pull and git status know where to look. The -sb flags print a short status with a branch line. origin/main is a remote-tracking branch: your local note of where main was on the remote the last time you talked to it. It only moves when you fetch, pull or push.

Look at all your branches and what they track:

Shell
git branch -vv
Text
* main eb99985 [origin/main] Initial commit

For a new feature branch with no upstream, a plain push stops and tells you what to do:

Text
fatal: The current branch feature/health has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin feature/health

Do what it says (or git push -u origin feature/health) and from then on it works with plain git push.

Fetch: look without touching#

git fetch downloads new commits from the remote and updates the remote-tracking branches such as origin/main. It never changes your branches or your files. That makes it safe to run at any time.

Suppose dev1 pushed a commit while dev2 was working. On dev2:

Shell
git fetch
git status -sb
Text
From /tmp/g17b/origin
   eb99985..4c2ced4  main       -> origin/main
## main...origin/main [ahead 1, behind 1]

Git says dev2's main is ahead 1 (it has one commit the remote lacks, our own) and behind 1 (the remote has one commit we lack). When both numbers are above zero, the histories have diverged. The graph shows it:

Shell
git log --oneline --graph --all
Text
* c53d326 dev2: add two
| * 4c2ced4 dev1: add one
|/  
* eb99985 Initial commit

To see what is waiting for you before you combine anything, use git log main..origin/main (commits on the remote that you lack) or git diff main origin/main.

Pull: fetch plus integrate#

git pull is a fetch followed by an integration step: it brings the remote's new commits into your current branch. When your branch is simply behind, that is a fast-forward and nothing can go wrong. When you have diverged, Git must either merge or rebase, and recent versions will not choose for you unless you configured it:

Shell
git pull
Text
fatal: Need to specify how to reconcile divergent branches.

You choose per command:

  • git pull --no-rebase merges the remote's commits into yours, creating a merge commit (lesson 15).
  • git pull --rebase replays your commits on top of the remote's (lesson 18).
  • git pull --ff-only pulls only if a fast-forward is possible and refuses otherwise.
Shell
git pull --ff-only
Text
fatal: Not possible to fast-forward, aborting.

--ff-only is a good habit because it never creates a surprise merge. If it refuses, you decide how to combine the histories on purpose. You can set a default for every repository with git config --global pull.rebase false (merge) or true (rebase); teams usually agree on one.

Here is the rebase version, which keeps the history in a straight line:

Shell
git pull --rebase
git log --oneline --graph
git push
Text
Successfully rebased and updated refs/heads/main.
* 78b662c dev2: add two
* 4c2ced4 dev1: add one
* eb99985 Initial commit
   4c2ced4..78b662c  main -> main

Rebasing replaced our commit c53d326 with a new one, 78b662c, that sits on top of dev1's commit. Git also reports Rebasing (1/1) while it works. Lesson 18 explains what that means.

A rejected push#

When the remote has commits that you do not, Git refuses to push, so that it never overwrites someone else's work:

Shell
git push
Text
To /tmp/g17b/origin.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '/tmp/g17b/origin.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally.

The fix is always the same sequence: fetch or pull, combine the histories, then push again. If a conflict appears while combining, resolve it as in lesson 16. When a rejected push says non-fast-forward, the meaning is the same: your branch is behind its remote counterpart.

You can overwrite the remote with git push --force, and it works. Do not use it on a shared branch such as main: it erases other people's commits from the remote. If you must force-push your own feature branch after rebasing, use git push --force-with-lease, which refuses if someone else pushed in the meantime.

Remote branches and cleaning up#

git branch -r lists remote-tracking branches. After a teammate pushes a new branch, your next fetch discovers it:

Shell
git fetch
git branch -r
Text
From /tmp/g17b/origin
 * [new branch]      feature/health -> origin/feature/health
  origin/HEAD -> origin/main
  origin/feature/health
  origin/main

To delete a branch on the remote, and then clear the stale local note of it on another machine:

Shell
git push origin --delete feature/health
git fetch --prune
Text
 - [deleted]         feature/health
 - [deleted]         (none)     -> origin/feature/health

--prune removes remote-tracking branches that no longer exist on the remote. Without it, git branch -r goes on listing the deleted branch.

If you ever need to point a branch at its upstream again, run git branch --set-upstream-to=origin/main. To check which upstream a branch has, use git rev-parse --abbrev-ref @{u}.

Try it#

  1. Create a bare repository with git init --bare origin.git, then clone it twice into dev1 and dev2. Make a commit in dev1 and push it.
  2. In dev2, run git fetch and git status -sb. Read behind 1. Run git log main..origin/main, then git pull.
  3. Make a commit in each clone without pulling. Push from dev1. Then try to push from dev2 and read the rejection.
  4. In dev2, run git pull --rebase, view the graph and push. Repeat the exercise with a fresh divergence using git pull --no-rebase and compare the graphs.
  5. Create a branch in dev1 and push it with git push alone, then with -u. Delete it on the remote and use git fetch --prune in dev2.