T7 Git and GitHub · Lesson 5
GitHub and remotes
A repository kept in one place can be lost in one bad day. Keep a second copy of it, with all its history, somewhere else: a remote. Send your commits there with git push, copy a repository with git clone, bring other people's work down with git fetch and git pull, and settle a push that is refused. GitHub is a service that keeps such remotes for you.
- say what a remote is, and connect one with git remote add
- send commits to a remote with git push, and set an upstream with -u
- copy a repository, history and all, with git clone
- bring down new work with git fetch, and see what arrived
- take it into your branch with git pull, and settle a refused push
- connect a repository to GitHub, by its HTTPS or SSH address
The back cover
A repository kept in one place can be lost in one bad day: a broken disk, a lost laptop, a mistaken command. A second copy somewhere else, with all its history, keeps the work safe and lets others join in.
These pages show what a remote is, and how git push sends your commits to it. They show how git clone copies a whole repository, and how git fetch and git pull bring other people's work down. They also show GitHub, and how to connect to it safely with a token.
Before you start
The story: The flood drill
The Warden sends word from the Underway that the lower tunnels may flood in the spring. If the Archive's drawers are under water, every version of every sheet goes with them, however carefully filed.
A record kept in one place is a record that one bad day can end.
"A copy in one place is not a copy," says the Keeper. She points up, to the great city archive on the hill, where every hall keeps a second set of drawers.
Until now, the whole history lived in one folder on one computer. If that disk fails, or the laptop is lost, every version goes with it. And nobody else can work on the same project. The answer is a second copy of the repository, kept somewhere else, that you send your work to and bring other people's work from.
It builds on these ideas. Each one has a tiny example, so you can check that you remember it.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Commits and their names. | aba517b | A copy elsewhere has the same names. |
| Branches. | main | Each copy has its own, and they are compared. |
| Names in brackets. | (HEAD -> main) | They will name the other copy's branches too. |
| Fast-forward. | Fast-forward | Bringing down new work is often one. |
| Merging. | git merge --no-edit east | When both copies moved on. |
| git status. | git status | It says how you stand with the other copy. |
| Folders and paths. | ../hill.git | The other copy, here, is in a folder nearby. |
| cd. | cd ../clerk | To work in a second copy. |
| Pipes. | 2>&1 | cat | To see git's messages without its progress lines. |
| Secrets. | .env | A password never goes into a command or a file. |
By the end of this lesson you will be able to:
- say what a remote is, and connect one with git remote add;
- send commits to a remote with git push, and set an upstream with -u;
- copy a repository, history and all, with git clone;
- bring down new work with git fetch, and see what arrived;
- take it into your branch with git pull, and settle a refused push;
- connect a repository to GitHub, by its HTTPS or SSH address.
How to follow this lesson
This page has no network, so a folder named ../hill.git stands in for the copy on the hill, as a GitHub repository would on a real computer. Git treats both in the same way; the last section shows what changes with GitHub. Most blocks start in the one-commit repository; the label above each says what else is already set up. Each block starts afresh.
In one line
This lesson keeps a second copy of the repository somewhere else, a remote, and moves commits between the two: push to send, clone to copy, fetch and pull to bring down.
A copy somewhere else
At a glance
A remote is another copy of your repository, kept somewhere else and known by a short name. The usual name is origin.
The command git remote add gives a remote its name and address, and git remote -v lists the remotes with their addresses. Adding a remote only writes the address down; no network is needed. The copy on a server is usually a bare repository: the history alone, with no desk, which people send commits to and fetch commits from.
Take a guess: Does a remote have to be on a server, or can it be a folder on a USB stick?
A remote is another copy of your repository, kept somewhere else, that git knows by a name. Somewhere else may be a server across the world, such as GitHub's, or another folder on the same computer. Git moves commits between the two copies; the commits keep their names, as a commit's name comes from what it holds.
The copy on a server is usually a bare repository: the history alone, with no desk. Nobody edits files there; people only send commits to it and fetch commits from it. git init --bare makes one. Here it is made in a folder next to the shop, named hill.git, after the archive on the hill:
In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main
git init --bare ../hill.git
ls ../hill.git
Initialized empty Git repository in /home/asha/hill.git/
HEAD config description hooks info objects refs
Those are the same parts that live inside a normal repository's .git folder. A bare repository is a .git folder with nothing around it, which is why its name ends in .git by custom.
Giving the remote a name
git remote add connects your repository to the remote, under a short name. The usual name for the main remote is origin. git remote lists the names, and -v adds each one's address:
In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main
git init -q --bare ../hill.git
git remote add origin ../hill.git
git remote
git remote -v
origin
origin ../hill.git (fetch)
origin ../hill.git (push)
Each remote has two addresses: one to fetch from and one to push to. They are almost always the same. Git keeps all this in .git/config, the repository's settings file:
In the same repository, with a bare repository ../hill.git added as origin, and nothing pushed yet
tail -n 3 .git/config
[remote "origin"]
url = ../hill.git
fetch = +refs/heads/*:refs/remotes/origin/*
The fetch line says where the remote's branches go when you fetch them: under refs/remotes/origin, as origin/main and so on. The section on git fetch comes back to it.
| Word | Means |
|---|---|
| remote | another copy of the repository, known by a name |
| origin | the usual name of the main remote |
| bare repository | a repository with history but no desk, as kept on a server |
git remote -v | lists each remote with its fetch and push address |
Worked example 1
The Keeper wants a second set of drawers on the hill before the spring floods. Make the far copy, connect the shop to it as origin, and check what it holds so far.
What we have: the shop's repository, with one commit
What we need: a bare repository ../hill.git, known to the shop as origin
The plan: git init --bare makes the far copy; git remote add names it; git remote -v checks; its refs/heads folder shows its branches
In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main
git init -q --bare ../hill.git
git remote add origin ../hill.git
git remote -v
ls ../hill.git/refs/heads | wc -l
origin ../hill.git (fetch)
origin ../hill.git (push)
0
- The remote is connected, but it holds no branch yet: its refs/heads folder is empty. Nothing is copied until you push.
origin points at ../hill.git, which is still empty.
Check: git remote prints origin.
A path that fails: expecting git remote add to copy the commits. It only records a name and an address; git push sends the work, in the next section.
When you are stuck: git remote does not show what you expected
git remote prints nothing: no remote has been added yet.
error: remote origin already exists: that name is taken; check its address with git remote -v.
A wrong address: git remote set-url origin and the right one replace it; the last section uses it.
Your turn
git init -q --bare ../hill.git
git remote add origin ../hill.git
git remote
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
origin
- git remote lists the names of the remotes.
- One has been added, origin.
It prints origin.
What does a bare repository have that a normal one has, and what does it lack?Tap to reveal
- It has what is inside .git.
- Nobody edits files in it.
It has the history, but no desk of files.
After git remote add, how many commits does the remote hold?Tap to reveal
- git remote add records a name and an address.
None yet: nothing is sent until git push.
Did you know?
origin is only a habit
origin is a default, not a rule. The git clone manual says its -o option makes clone use another name instead of origin to keep track of the repository it copied, and that this overrides the setting clone.defaultRemoteName. So git clone -o hill address names the remote hill, and every command that would say origin then says hill.
Why it fits here: It sharpens this topic's origin: it is only the default name for a remote, and clone can give another.
Source: git manual: git-clone
A remote on a USB stick
A remote need not be on a server. The git clone manual says git also supports local repositories natively, written as a plain path such as /path/to/repo.git/ or as file:///path/to/repo.git/. So a bare repository on a USB stick can be a remote: git push sends commits to it, and git clone, git fetch and git pull read from it, with no network at all.
Why it fits here: It answers this topic's question: a remote can be any folder git can reach, even one on a USB stick.
Source: git manual: git-clone (GIT URLS)
In one line
A remote is another copy of the repository, known by a name, usually origin; git remote add names it, and git remote -v lists the remotes with their addresses. A server keeps a bare repository: the history without a desk.
Sending your work: git push
At a glance
Commits made on your machine stay there until you send them. The command git push sends them to a remote and moves the remote's branch to match.
The first push of a branch names the remote and the branch, with -u to set its upstream, as in git push -u origin main; after that, git push alone is enough. The name origin/main records where the remote's main was when you last spoke to it, and git status compares your branch with it, saying ahead by so many commits.
Take a guess: Can git set the upstream by itself, so that the first push needs no -u?
git push sends your commits to a remote, and moves the remote's branch to match. The first time, name the remote and the branch, and add -u. Here the output goes through a pipe into cat, for a reason the next paragraph gives:
In the same repository, with a bare repository ../hill.git added as origin, and nothing pushed yet
git push -u origin main 2>&1 | cat
To ../hill.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
| Line | Means |
|---|---|
To ../hill.git | the remote that received the work |
* [new branch] main -> main | your main made a new branch main there |
branch 'main' set up to track 'origin/main'. | -u made origin's main your branch's upstream |
Typed straight into a terminal, git also prints progress lines while it sends: how many objects it counted, and how many bytes it wrote, and how fast. The numbers depend on the computer, so this page cannot run them; through a pipe, git leaves them out. Here is what one run in a real terminal showed:
Typed in a real terminal, after the same setup
asha@tower:~/shop$ git push -u origin main
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 247 bytes | 247.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
To ../hill.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
An object is git's word for a stored thing: each commit, each folder listing and each version of a file. Three objects went up: the commit, its folder listing, and orders.txt.
The upstream
The upstream of a branch is the remote branch it follows. With it set, a plain git push knows where to send, and git status can compare the two:
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
git log --oneline
git status
aba517b (HEAD -> main, origin/main) First orders
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
The name origin/main in the brackets is a remote-tracking branch: your repository's record of where origin's main was the last time the two spoke. Make a commit, and git status says that you are ahead; push, and you are level again:
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
echo 'open gate 6' >> orders.txt
git commit -q -am 'Open gate 6'
git status | head -n 3
git push -q
git status | head -n 2
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
On branch main
Your branch is up to date with 'origin/main'.
The -q, for quiet, leaves out every line when all goes well. git branch -vv shows the same comparison for every branch, in square brackets:
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
echo 'open gate 6' >> orders.txt
git commit -q -am 'Open gate 6'
git branch -vv
git push 2>&1 | cat
git branch -vv
* main 915c421 [origin/main: ahead 1] Open gate 6
To ../hill.git
aba517b..915c421 main -> main
* main 915c421 [origin/main] Open gate 6
The line aba517b..915c421 says that origin's main moved from the first to the second: a fast-forward, on the remote.
Worked example 2
Corin files the order to open gate 6. Send it up the hill, and check that the far copy has it.
What we have: the shop's main, pushed once before, with upstream origin/main
What we need: the new commit on origin's main
The plan: commit; git push with -q; then read origin/main, and the log inside the bare repository itself with git -C
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
echo 'open gate 6' >> orders.txt
git commit -q -am 'Open gate 6'
git push -q
git log --oneline -1 origin/main
git -C ../hill.git log --oneline
915c421 (HEAD -> main, origin/main) Open gate 6
915c421 (HEAD -> main) Open gate 6
aba517b First orders
- git -C runs git as if it had been started in another folder, here the bare repository. Its main now holds both commits, with the same names as in the shop.
The far copy's main is at 915c421, Open gate 6.
Check: git status says Your branch is up to date with 'origin/main'.
A path that fails: committing and stopping there. A commit stays in the shop until it is pushed; the hill knows nothing of it.
When you are stuck: git push did not do what you expected
Nothing printed, and nothing went: with -q, look at git status, which says ahead or up to date.
Everything up-to-date: the remote already has every commit of the branch.
has no upstream branch: push once with -u, naming the remote and the branch, as the Mistakes section shows.
rejected: the remote has work you do not have; the section on git pull settles it.
Your turn
What does -u do in git push -u origin main?Tap to reveal
- It is short for --set-upstream.
It makes origin's main the upstream of your main, so that later a plain git push knows where to send.
echo 'open gate 6' >> orders.txt
git commit -q -am 'Open gate 6'
git status | sed -n 2p
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Your branch is ahead of 'origin/main' by 1 commit.
- The new commit is not on origin yet.
- So the branch is one commit ahead.
It prints Your branch is ahead of 'origin/main' by 1 commit.
What is origin/main?Tap to reveal
- It is in your repository, not on the remote.
A remote-tracking branch: your record of where origin's main was when you last spoke to origin.
Did you know?
No -u needed with autoSetupRemote
The -u of a first push can be made automatic. The git config manual says that when push.autoSetupRemote is true, a plain git push assumes --set-upstream for a branch that has no upstream yet. So after git config --global push.autoSetupRemote true, git push alone sends a new branch to the default remote and sets its upstream in the same step.
Why it fits here: It answers this topic's question: push.autoSetupRemote makes git push set the upstream by itself.
simple, the safe default
A plain git push sends only the current branch, and that was a deliberate choice. The git config manual says the setting push.default decides what a push sends when none is named. Its mode simple pushes only the current branch. When pushing back to the remote you pull from, it pushes only to an upstream branch of the same name. That mode has been the default since Git 2.0, as the safest option for beginners.
Why it fits here: It explains this topic's plain git push: since Git 2.0 the simple mode sends only the current branch, to its own upstream.
In one line
The command git push sends commits to a remote and moves its branch; the first push of a branch uses -u to set its upstream. origin/main records where the remote's main was, and git status compares your branch with it.
Copying a repository: git clone
At a glance
Joining a project means getting its repository. You do not copy files one by one: you clone the whole thing.
The command git clone copies a whole repository, with every commit, into a new folder. It sets up a remote named origin that points back to the source, and checks out the main branch, with origin/main as its upstream. A clone is a full repository of its own, so it works even when the source is out of reach. Cloning is how you start on someone else's project.
Take a guess: Must a clone always copy every commit ever made?
A clerk from another hall needs the orders too. git clone copies a whole repository, every commit of its history, into a new folder, and sets up origin to point back where it came from:
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
git clone ../hill.git ../clerk
cd ../clerk
git log --oneline
git remote -v
git branch -a
Cloning into '../clerk'...
done.
aba517b (HEAD -> main, origin/main, origin/HEAD) First orders
origin /home/asha/shop/../hill.git (fetch)
origin /home/asha/shop/../hill.git (push)
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
Read what the clone set up:
- the whole history, with the same commit names, here the one commit aba517b;
- a remote named origin, whose address git wrote out from the folder you cloned in;
- a branch main, checked out on the desk, with origin/main as its upstream;
origin/HEAD: which branch origin itself has checked out, here main.
git branch -a lists all the branches, your own and the remote-tracking ones. The clone's desk holds the files of main's tip, and git status already knows the upstream:
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
git clone -q ../hill.git ../clerk
ls ../clerk
git -C ../clerk status
orders.txt
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
| Command | Makes |
|---|---|
git clone ../hill.git | a new folder named hill, after the source |
git clone ../hill.git ../clerk | a new folder ../clerk |
git clone -q ... | the same, without the two lines |
A clone is a full repository in its own right. The clerk can commit, branch and merge in it with no network at all, and push when ready.
Worked example 3
The clerk copies the orders, files a note about pump 7, and sends it up the hill.
What we have: the hill's repository, with one commit
What we need: a clone in ../clerk with a new commit, and the hill holding it
The plan: git clone; then, inside the clone, commit and push, which needs no -u, as the clone's main already has its upstream
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
git clone -q ../hill.git ../clerk
cd ../clerk
printf 'pump 7 low\n' > notes.txt
git add notes.txt
git commit -q -m 'Notes'
git push -q
git log --oneline -1 origin/main
c61fd71 (HEAD -> main, origin/main, origin/HEAD) Notes
- The clerk's commit c61fd71 is now on the hill: origin/main points at it. The shop does not know yet; the next section brings it down.
The hill's main is at c61fd71, the clerk's Notes.
Check: the push needed no -u: git clone set main's upstream already.
A path that fails: copying the folder with cp -r instead. The copy works, but it knows nothing of the hill: no origin, no upstream.
When you are stuck: git clone refused, or the copy is not what you expected
already exists and is not an empty directory: choose a new folder name, or remove the old copy first.
does not appear to be a git repository: check the address; for a folder, check the path from where you are.
The files are there but git log is short: the source had only those commits; a clone copies everything it has.
Your turn
git clone -q ../hill.git ../clerk
git -C ../clerk branch -a
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
* main
remotes/origin/HEAD -> origin/main
remotes/origin/main
- The clone has its own main, checked out.
- -a adds the remote-tracking branches, origin/HEAD and origin/main.
It prints * main, then remotes/origin/HEAD -> origin/main and remotes/origin/main.
What folder does git clone ../hill.git make?Tap to reveal
- With no folder given, git names it after the source, without .git.
A folder named hill.
Why can the clerk push from a fresh clone without -u?Tap to reveal
- git clone sets up more than the files.
Because the clone's main already has origin/main as its upstream.
Did you know?
A clone of the last commit only
A clone can leave the old history behind. The git clone manual says --depth creates a shallow clone, with a history cut to the given number of commits. So git clone --depth 1 address copies only the latest commit, which saves time and space with a large project. The manual adds that --depth implies --single-branch, so only one branch comes along.
Why it fits here: It answers this topic's question: git clone --depth 1 copies only the latest commit, not the whole history.
Source: git manual: git-clone
Every branch arrives, as a record
A clone shows one branch, yet holds them all. The git clone manual says it creates a remote-tracking branch for each branch in the repository it copies, visible with git branch --remotes, and checks out one branch of your own. So git branch alone may list only main, while git branch --remotes lists origin/main and every other branch of the source.
Why it fits here: It adds to this topic's clone: every branch of the source arrives as a remote-tracking branch such as origin/main.
Source: git manual: git-clone
In one line
The command git clone copies a whole repository, with every commit, into a new folder, sets up origin to point back to the source, and checks out its main branch with origin/main as the upstream.
Bringing work down: git fetch
At a glance
Other people push their commits to the remote too. To see their work, you bring it down.
The command git fetch brings down every commit that the remote has and you lack, and moves origin/main to match. Your own branch and your desk stay exactly as they were. So you can look before you take: git log main..origin/main lists the new commits, and you decide when to bring them into your branch. Fetching never touches your branches or your desk, so it is safe to run at any time.
Take a guess: Does a second git fetch download everything again?
The clerk's note is on the hill, but the shop does not know about it. git fetch asks the remote what it has, brings down the commits you lack, and moves your remote-tracking branches to match. It changes nothing else:
In the same repository; a second clone, ../clerk, has since pushed a commit of its own (Notes), which this one has not fetched
git fetch 2>&1 | cat
git status | head -n 3
From ../hill
aba517b..c61fd71 main -> origin/main
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
Read the second line as: origin's main moved from aba517b to c61fd71, and origin/main followed it. Your own main did not move. The desk did not change either:
In the same repository; a second clone, ../clerk, has since pushed a commit of its own (Notes), which this one has not fetched
git fetch -q
git log --oneline --all
ls
c61fd71 (origin/main, origin/HEAD) Notes
aba517b (HEAD -> main) First orders
orders.txt
The clerk's commit is in your repository now, under origin/main, but notes.txt is not on your desk. That is the point of fetch: you can look before you take. As in lesson 03, the range main..origin/main lists what origin has that you lack:
In the same repository; a second clone, ../clerk, has since pushed a commit of its own (Notes), which this one has not fetched
git fetch -q
git log --oneline main..origin/main
c61fd71 (origin/main, origin/HEAD) Notes
Taking what arrived
origin/main is a branch like any other, so you can merge it. Here it is a fast-forward, as main has not moved since:
In the same repository; a second clone, ../clerk, has since pushed a commit of its own (Notes), which this one has not fetched
git fetch -q
git merge origin/main
ls
Updating aba517b..c61fd71
Fast-forward
notes.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
notes.txt orders.txt
| After git fetch | Changed? |
|---|---|
| origin/main, your record of the remote | yes: it moves to the remote's main |
| your own main | no |
| the desk and the tray | no |
| what you can look at | everything that arrived, with log, diff and show |
Worked example 4
Before taking anything, Keeper Hollis wants to know what the other hall has sent, and what it would change in the shop's files.
What we have: the shop, one commit behind the hill, which has the clerk's Notes
What we need: the new commits, and their change to the files
The plan: fetch quietly; list main..origin/main; then compare main with origin/main with git diff, keeping its last line
In the same repository; a second clone, ../clerk, has since pushed a commit of its own (Notes), which this one has not fetched
git fetch -q
git log --oneline main..origin/main
git diff main origin/main | tail -n 1
c61fd71 (origin/main, origin/HEAD) Notes
+pump 7 low
- One commit arrived, Notes, and its change adds one line, pump 7 low, in a new file. Nothing in the shop has changed yet.
One commit, c61fd71 Notes, adding pump 7 low.
Check: git status says Your branch is behind 'origin/main' by 1 commit.
A path that fails: expecting notes.txt on the desk after git fetch. fetch only brings the commits; merging, or git pull, puts them on your branch.
When you are stuck: git fetch seemed to do nothing
No output: there was nothing new on the remote, or -q kept it quiet.
The files did not change: that is right; fetch moves only origin/main. Merge it, or use git pull.
To see what arrived: git log --oneline main..origin/main.
Your turn
After git fetch, which of these moved: main, origin/main, the files on the desk?Tap to reveal
- fetch updates your record of the remote only.
Only origin/main.
git fetch -q
git status -sb
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
## main...origin/main [behind 1]
- The short status's first line compares main with its upstream.
- origin/main is one commit ahead now.
It prints ## main...origin/main [behind 1].
Which command lists the commits that origin/main has and main lacks?Tap to reveal
- Two dots, as in lesson 03.
git log --oneline main..origin/main.
Did you know?
Only what is missing
A fetch downloads far less than a clone. The git fetch manual says it fetches branches and tags from another repository, along with the objects needed to complete their histories. Objects you already hold are not sent again, since each one is known by its name. So a fetch right after another, with nothing new on the remote, moves almost nothing at all.
Why it fits here: It explains why this topic's git fetch is quick: only the objects missing from your history come down.
Source: git manual: git-fetch
FETCH_HEAD remembers the last fetch
Every fetch leaves a note behind. Git's manual on naming revisions says FETCH_HEAD records the branch you fetched from a remote with your last git fetch. So right after a fetch, git log HEAD..FETCH_HEAD lists what came down, even when the fetch named a branch that has no remote-tracking branch of its own.
Why it fits here: It adds to this topic's look before you take: FETCH_HEAD names what the last fetch brought down.
Source: git manual: gitrevisions
In one line
The command git fetch brings down the commits that the remote has and you lack, and moves origin/main to match; your branch and your desk stay as they were, so you can look before you take.
git pull, and when a push is refused
At a glance
Most of the time you want the remote's new commits in your own branch at once. git pull does both steps: a fetch, then a merge.
The command git pull fetches and then merges the upstream into your branch. When both lines have moved on, git asks how to join them; give --no-rebase to merge. A push that would drop commits the remote has is refused, so pull first, settle any conflict, then push. Never force a push to a shared branch.
Take a guess: Is there a safer kind of force push?
Fetching and then merging is so common that one command does both. git pull fetches from the upstream's remote, then merges the upstream into your branch:
In the same repository; a second clone, ../clerk, has since pushed a commit of its own (Notes), which this one has not fetched
git pull 2>&1 | cat
git log --oneline
From ../hill
aba517b..c61fd71 main -> origin/main
Updating aba517b..c61fd71
Fast-forward
notes.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
c61fd71 (HEAD -> main, origin/main, origin/HEAD) Notes
aba517b First orders
The first two lines are git fetch; the rest is the fast-forward of lesson 03. In a real terminal, a pull shows the progress lines of the transfer as well:
Typed in a real terminal, after the same setup
asha@tower:~/shop$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
From ../hill
aba517b..c61fd71 main -> origin/main
Updating aba517b..c61fd71
Fast-forward
notes.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 notes.txt
Lines that start with remote: come from git on the other side, as it packs the objects to send.
A push that is refused
Now suppose the shop made a commit of its own, Open gate 6, while the clerk pushed Notes. The two lines have moved apart, and the shop tries to push:
In the same repository, which has a commit of its own (Open gate 6) since the push, while ../clerk has pushed another (Notes)
git push 2>&1 | cat
To ../hill.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to '../hill.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. This is usually caused by another repository pushing to
hint: the same ref. If you want to integrate the remote changes, use
hint: 'git pull' before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
A push may only fast-forward the remote's branch. Moving the hill's main to the shop's commit would drop the clerk's Notes, so git refuses. Nothing was lost on either side. In a real terminal, these lines are in red and yellow.
Joining the two lines
The hint says to pull first. But a plain pull of two lines that have both moved on stops too, and asks you to choose how to join them:
In the same repository, which has a commit of its own (Open gate 6) since the push, while ../clerk has pushed another (Notes)
git pull 2>&1 | cat
From ../hill
aba517b..c61fd71 main -> origin/main
hint: You have divergent branches and need to specify how to reconcile them.
hint: You can do so by running one of the following commands sometime before
hint: your next pull:
hint:
hint: git config pull.rebase false # merge
hint: git config pull.rebase true # rebase
hint: git config pull.ff only # fast-forward only
hint:
hint: You can replace "git config" with "git config --global" to set a default
hint: preference for all repositories. You can also pass --rebase, --no-rebase,
hint: or --ff-only on the command line to override the configured default per
hint: invocation.
fatal: Need to specify how to reconcile divergent branches.
The fetch happened; only the joining waits. Of the three ways, merging is the one this course has taught: --no-rebase tells git pull to merge, and --no-edit takes git's own message, as in lesson 04. Then the push goes through:
In the same repository, which has a commit of its own (Open gate 6) since the push, while ../clerk has pushed another (Notes)
git pull -q --no-rebase --no-edit
git push -q
git log --oneline --graph
* 68b59ec (HEAD -> main, origin/main, origin/HEAD) Merge branch 'main' of ../hill
|\
| * c61fd71 Notes
* | c58e49f Open gate 6
|/
* aba517b First orders
| git pull with | When both lines moved on |
|---|---|
--no-rebase | merges, with a merge commit |
--ff-only | refuses, unless a fast-forward is possible |
--rebase | rewrites your commits on top of the remote's; this course leaves it for later |
| nothing | stops and asks, as above, unless a setting chooses |
If the merge has a conflict, it stops exactly as in lesson 04: settle it, git add, git commit, then push.
Worked example 5
The shop's push was refused, as the clerk got there first. Join the two lines and send the result up the hill.
What we have: the shop with Open gate 6; the hill with the clerk's Notes
What we need: both commits on the hill's main, and the shop level with it
The plan: pull with --no-rebase and --no-edit to merge; push; then the short status's first line
In the same repository, which has a commit of its own (Open gate 6) since the push, while ../clerk has pushed another (Notes)
git pull -q --no-rebase --no-edit
git push -q
git status -sb
## main...origin/main
- No ahead or behind in the brackets: main and origin/main are the same commit, the merge commit that joins both lines.
The hill's main holds both commits, joined by a merge commit.
Check: git log --oneline --graph shows the merge with the two lines under it.
A path that fails: forcing the push, with git push --force. It would move the hill's main to the shop's commit and drop the clerk's Notes from it. Never force a push to a shared branch.
When you are stuck: a push is refused, or a pull stops
rejected (fetch first): the remote has commits you lack; pull first, then push.
Need to specify how to reconcile divergent branches: add --no-rebase to merge.
CONFLICT during a pull: settle it as in lesson 04, then git add, git commit and git push.
Already up to date: there was nothing new on the remote.
Your turn
What two commands does git pull run, in effect?Tap to reveal
- One brings the commits down.
- One joins them into your branch.
git fetch, then git merge of the upstream.
Why does git refuse a push when the remote has commits you lack?Tap to reveal
- A push may only fast-forward the remote's branch.
Because moving the remote's branch to your commit would drop the commits you lack.
git pull -q --no-rebase --no-edit
git log --oneline -1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
68b59ec (HEAD -> main) Merge branch 'main' of ../hill
- The two lines are joined by a merge commit.
- A pull's message names the branch and where it came from.
- The push has not happened yet, so origin/main is not on it.
It prints 68b59ec (HEAD -> main) Merge branch 'main' of ../hill.
Did you know?
A force with a safety catch
When a force push is truly needed, there is a careful kind. The git push manual says --force-with-lease protects each remote branch about to be updated by requiring that its current value still matches your remote-tracking branch for it. If someone else has pushed since your last fetch, the push is refused instead of erasing their work.
Why it fits here: It answers this topic's question: --force-with-lease refuses to overwrite commits you have not seen.
Source: git manual: git-push
GitHub blocks force pushes for you
Hosting services can enforce the rule against forcing. GitHub's documentation says branch protection rules decide whether collaborators can delete or force push to a branch. By default, each branch protection rule disables force pushes to the matching branches and prevents them from being deleted. So a protected main cannot lose its history to a forced push.
Why it fits here: It backs this topic's never force a shared branch: on GitHub, a protected branch refuses force pushes by default.
In one line
The command git pull fetches and then merges the upstream; when both lines moved on, give --no-rebase to merge. A push that would drop the remote's commits is refused: pull first, then push, and never force a shared branch.
GitHub
At a glance
GitHub is a service that keeps remotes on its servers, each with a page of its own. Each page shows the files, the history and more.
To connect a repository, copy its HTTPS or SSH address from GitHub and give it to git remote add, or move origin with git remote set-url. Over HTTPS, git asks for a password, and you give a personal access token, never your account's password. A token is a secret: it never goes into a file or a commit.
Take a guess: Since when has GitHub refused account passwords for git?
GitHub is a service that keeps remotes for you, on its own servers, with a web page for each. A repository on GitHub is a bare repository like hill.git, with an owner, a name and a page where people can read the files and the history. Git talks to it exactly as it talked to the hill: remote add, push, clone, fetch and pull. Only the address changes.
Making a repository on GitHub
On GitHub's website, signed in to your account, the steps are short, as GitHub's own documentation gives them:
- Click the + menu at the top right, then New repository.
- Choose the owner: your own account, or an organisation you belong to.
- Type a name. It may hold ASCII letters, digits, and the marks . - and _.
- Choose public, which anyone can read, or private, which only the people you allow can read.
- Optionally add a README, a file that describes the project, and click Create repository.
For a project that already has commits, as the shop has, leave the README out: the repository starts empty, ready for your first push.
Two kinds of address
GitHub gives each repository two addresses to push to. An HTTPS address works everywhere. An SSH address uses a key on your computer instead of a token:
| Kind | The address looks like | Git proves who you are with |
|---|---|---|
| HTTPS | https://github.com/asha/shop.git | your username and a personal access token |
| SSH | git@github.com:asha/shop.git | an SSH key, made on your computer and added to your account |
Connecting the shop to GitHub is the git remote add you already know, with that address. It needs no network, as it only writes the address down:
In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main
git remote add origin https://github.com/asha/shop.git
git remote -v
origin https://github.com/asha/shop.git (fetch)
origin https://github.com/asha/shop.git (push)
To move an existing remote to a new address, git remote set-url replaces it:
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
git remote set-url origin https://github.com/asha/shop.git
git remote -v
origin https://github.com/asha/shop.git (fetch)
origin https://github.com/asha/shop.git (push)
Proving who you are
When you push over HTTPS, git asks for a username and a password. GitHub no longer accepts your account's password there. Instead, you type a personal access token: a long secret string, made on GitHub's settings pages, that works like a password for git alone. A credential helper, such as Git Credential Manager, can keep the token for you, so that you are not asked each time.
A token is a secret
Never write a token into a command, a script, a file or a commit message. Anything in a commit stays in the history, on every copy. The Mistakes section shows where a token in an address ends up.
This page, and the machine that checks it, have no network, so a push to github.com cannot run here. On a computer of your own, connected to the internet, the commands of this lesson work the same with a GitHub address.
| On GitHub | Means |
|---|---|
| repository | a project's remote, with its own page |
| owner | the account or organisation the repository belongs to |
| public, private | who may read it |
| README | the file that the repository's page shows first |
| personal access token | a secret that git uses instead of a password |
Worked example 6
The shop is moving its far copy from the hill to GitHub, at https://github.com/asha/shop.git. Point origin at the new address, and check that main still follows origin/main.
What we have: the shop, whose origin is ../hill.git and whose main follows origin/main
What we need: origin at the GitHub address, and the upstream unchanged
The plan: git remote set-url replaces the address; git remote -v checks it; git branch -vv checks the upstream
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
git remote set-url origin https://github.com/asha/shop.git
git remote -v
git branch -vv
origin https://github.com/asha/shop.git (fetch)
origin https://github.com/asha/shop.git (push)
* main aba517b [origin/main] First orders
- The remote keeps its name, so main still follows origin/main. The next git push, on a computer with a network, would go to GitHub.
origin is now https://github.com/asha/shop.git, and main still tracks origin/main.
Check: the [remote "origin"] section of .git/config holds the new url.
A path that fails: adding a second remote named origin. The command git remote add refuses, as the name is taken; set-url is the way to change it.
When you are stuck: connecting to GitHub
Authentication failed over HTTPS: use a personal access token where git asks for the password, not the account's password.
Permission denied (publickey) over SSH: your SSH key is not added to your GitHub account yet.
Repository not found: check the owner and the name in the address, and that you may read the repository.
Your turn
git remote add origin git@github.com:asha/shop.git
git remote -v | head -n 1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
origin git@github.com:asha/shop.git (fetch)
- git remote -v prints each remote twice, for fetch and for push.
- head keeps the first line.
It prints origin, a tab, git@github.com:asha/shop.git (fetch).
Which kind of address starts with git@github.com?Tap to reveal
- It uses a key instead of a token.
The SSH address.
Git asks for a password while pushing to github.com over HTTPS. What do you type?Tap to reveal
- GitHub does not accept the account's password for git.
A personal access token, or let a credential helper supply it.
Did you know?
No passwords since 13 August 2021
GitHub set a date for the end of passwords in git. Its blog announced that beginning 13 August 2021, it would no longer accept account passwords when authenticating git operations on GitHub.com. The change covered command-line git and apps that use git to reach GitHub. Since then, a personal access token, or another kind of key, has taken the password's place.
Why it fits here: It answers this topic's question: GitHub has refused account passwords for git operations since 13 August 2021.
Source: GitHub Blog: Token authentication requirements for Git operations
GitHub stops a pushed secret
GitHub can catch a secret before it lands. Its documentation describes push protection, a secret scanning feature designed to prevent hardcoded credentials, such as secrets or tokens, from ever being pushed to a repository. Rather than warning after the leak, it blocks the push that contains them. It is a second guard, not a reason to stop checking your own commits.
Why it fits here: It backs this topic's rule that a token never goes into a commit: GitHub's push protection blocks pushes holding secrets.
In one line
GitHub keeps remotes on its servers, each with a page; connect one with git remote add and its HTTPS or SSH address, or move origin with git remote set-url. Over HTTPS, git uses a personal access token, never your password, and a token never goes into a file or a commit.
Where it is used
Remotes are how work leaves one computer. Here is where each idea of this lesson does its work.
| What you learned | Where it is used | A real example |
|---|---|---|
| a remote, origin | every project shared with anyone | a team's repository on GitHub |
git push | publishing your commits | sending a finished fix for others to see |
git clone | starting on a project | a new teammate's first command |
git fetch | looking at others' work before taking it | checking what changed overnight |
git pull | staying up to date | the first command of a working day |
| a refused push | two people pushing to one branch | pull, merge, push again |
| a personal access token | pushing to GitHub over HTTPS | kept by a credential helper, never in a file |
A remote is also a backup. The flood in the story is any lost laptop or failed disk: with the history on a remote, nothing that was pushed is lost.
Mistakes and debugging
These are the slips that people meet most often with remotes. Every output below is the real one, from the code shown.
1. Pushing a branch that has no upstream
In the same repository, with a bare repository ../hill.git added as origin, and nothing pushed yet
git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
A plain git push needs to know where to send. The fix: the first time, name the remote and the branch with -u, as the message says: git push -u origin main. --set-upstream is the long name of -u.
2. Expecting git fetch to change your files
In the same repository; a second clone, ../clerk, has since pushed a commit of its own (Notes), which this one has not fetched
git fetch -q
ls
git status -sb
orders.txt
## main...origin/main [behind 1]
The clerk's notes.txt arrived in the repository, under origin/main, but not on the desk. The fix: merge origin/main, or use git pull, which fetches and merges.
3. Cloning into a folder that already exists
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
git clone -q ../hill.git ../clerk
git clone ../hill.git ../clerk
fatal: destination path '../clerk' already exists and is not an empty directory.
The fix: give the clone a new folder name, or, if the old copy is no longer wanted, remove it first.
4. Committing and forgetting to push
In the same repository, whose main has been pushed to origin, the bare repository ../hill.git
echo 'open gate 6' >> orders.txt
git commit -q -am 'Open gate 6'
git status -sb
## main...origin/main [ahead 1]
The commit is safe in the shop, and nowhere else: if the laptop is lost tonight, so is the commit. The fix: git push. Look for ahead in git status before you stop work.
5. A token in the remote's address
In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main
git remote add origin https://asha:TOKEN@github.com/asha/shop.git
grep url .git/config
url = https://asha:TOKEN@github.com/asha/shop.git
Some guides put the token inside the address. Git then keeps it, in plain text, in .git/config, where any program or person reading the folder can find it. The fix: keep the address without it, as https://github.com/asha/shop.git, and let a credential helper keep the token. If a real token was ever written down like this, revoke it on GitHub and make a new one.
The debugging corner: where do the copies stand?
Before and after any push or pull, run git status -sb: its first line says ahead, behind, both, or nothing. git log --oneline --graph --all shows your branches and origin's side by side, and git remote -v says where origin is.
Quick card
The whole lesson on one card. Come back to it whenever you need a reminder.
The lesson in code
git init -q --bare ../hill.git # the far copy
git remote add origin ../hill.git # its name
git push -q -u origin main # send main; follow origin/main
git clone -q ../hill.git ../clerk # a second copy
git -C ../clerk log --oneline # the same commits, the same names
git fetch # nothing new yet
git status -sb # main against origin/main
git pull -q # fetch, then merge
aba517b (HEAD -> main, origin/main, origin/HEAD) First orders
## main...origin/main
| When you see | It means | Do |
|---|---|---|
Your branch is ahead of 'origin/main' | commits not pushed yet | git push |
Your branch is behind 'origin/main' | the remote has commits you lack | git pull |
have diverged | both have moved on | git pull --no-rebase, then push |
! [rejected] ... (fetch first) | the remote has work you lack | pull first, then push |
has no upstream branch | a first push | git push -u origin main |
Need to specify how to reconcile | a pull of two lines that moved on | add --no-rebase |
does not appear to be a git repository | a wrong address or path | git remote -v |
Everything up-to-date | nothing to send | nothing to fix |
The story: The far archive
You copy the whole drawer, with its history, to the city archive, and name that far drawer origin. From then on you send each new picture up the hill, and bring down any that other halls have filed there. When a clerk from another hall wants the orders, he copies the drawer from the hill, history and all.
In spring the lower tunnels flood to the knee. The orders, and every one of their pictures, are dry on the hill.
The Keeper hangs the drill's report in the far drawer too. "Now," she says, "we can lose a table without losing a day."
Reagents gathered: remote, origin, git clone, git push, git pull, git fetch.
Sources
- Git reference documentation (the manual pages of git 2.53) (GPL-2 (Debian's copyright file for git 2.53, which covers the documentation with the code; read 2026-11-07); facts and ideas only)
- GitHub Docs (CC BY 4.0 (the github/docs repository's LICENSE, read 2026-11-07); facts and ideas only)
- Pro Git (second edition), by Scott Chacon and Ben Straub (CC BY-NC-SA 3.0 (the book's page on git-scm.com, read 2026-11-07); facts and ideas only)