Remotes: fetch, pull, push and upstream branches
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
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:
git remote -v
git pushfatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository usingAdd 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.)
git remote add origin ../origin.git
git remote -vorigin ../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.
git push -u origin main
git status -sbTo ../origin.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
## main...origin/mainThe -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:
git branch -vv* main eb99985 [origin/main] Initial commitFor a new feature branch with no upstream, a plain push stops and tells you what to do:
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/healthDo 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:
git fetch
git status -sbFrom /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:
git log --oneline --graph --all* c53d326 dev2: add two
| * 4c2ced4 dev1: add one
|/
* eb99985 Initial commitTo 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:
git pullfatal: Need to specify how to reconcile divergent branches.You choose per command:
git pull --no-rebasemerges the remote's commits into yours, creating a merge commit (lesson 15).git pull --rebasereplays your commits on top of the remote's (lesson 18).git pull --ff-onlypulls only if a fast-forward is possible and refuses otherwise.
git pull --ff-onlyfatal: 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:
git pull --rebase
git log --oneline --graph
git pushSuccessfully rebased and updated refs/heads/main.
* 78b662c dev2: add two
* 4c2ced4 dev1: add one
* eb99985 Initial commit
4c2ced4..78b662c main -> mainRebasing 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:
git pushTo /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:
git fetch
git branch -rFrom /tmp/g17b/origin
* [new branch] feature/health -> origin/feature/health
origin/HEAD -> origin/main
origin/feature/health
origin/mainTo delete a branch on the remote, and then clear the stale local note of it on another machine:
git push origin --delete feature/health
git fetch --prune - [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#
- Create a bare repository with
git init --bare origin.git, then clone it twice intodev1anddev2. Make a commit indev1and push it. - In
dev2, rungit fetchandgit status -sb. Readbehind 1. Rungit log main..origin/main, thengit pull. - Make a commit in each clone without pulling. Push from
dev1. Then try to push fromdev2and read the rejection. - In
dev2, rungit pull --rebase, view the graph and push. Repeat the exercise with a fresh divergence usinggit pull --no-rebaseand compare the graphs. - Create a branch in
dev1and push it withgit pushalone, then with-u. Delete it on the remote and usegit fetch --pruneindev2.
Sign in to keep track of the lessons you finish.