Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Git clone, push and pull

After this lesson: I can clone, push and pull from a shared server.

60 min lesson12 commandsGit and Gitea lab: learn clone, commit and push on a Git server of your own

What a Git remote is for

A Git remote is a copy of your repository kept on another computer, usually a server. It is how a team shares work. You push your commits up to it and you pull other people's commits down.

Two students write a report together. Each works on a laptop. The remote is where their two histories meet.

By the end of this lesson

Note

I can clone, push and pull from a shared server.

Key words

WordWhat it means
Remotea copy of the repository on another computer
Originthe usual name for the remote you cloned from
Cloneto copy a repository with all of its history
Pushto send your new commits to the remote
Fetchto download new commits without merging them
Pullto fetch and then merge into your branch

The cells students fill in are left blank in this preview.

A server such as Gitea holds the copy

Gitea is a small Git server with a web page, like the sites many teams use. Each lab has its own Gitea. It holds a repository called hello-git.

You can ask a server what it holds without copying anything. The command git ls-remote --heads lists its branches. Press Run to ask the lab's Gitea.

Ask the lab server for its branches

Command
git ls-remote --heads http://gitea:3000/student/hello-git.git

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Two clones of one folder play two people

To practise sharing, you need two people. You will be both. A bare repository is a folder that holds history and no working files. It stands in for the server.

Alice and Bob each clone it. The commands are the ones you would use with Gitea, and nothing here goes to the lab's server.

Make the shared folder

Command
git init --bare -b main shared.git

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Alice clones it

Command
git clone shared.git alice

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Alice makes a commit

Command
git -C alice commit --allow-empty -m "Start the project"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Alice pushes it

Command
git -C alice push -u origin main

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Fetch downloads, and pull downloads and merges

The commit in these Runs has no file changes. The option --allow-empty allows that, and it keeps the Runs short. Real commits hold changes.

Bob clones after Alice pushed, then commits and pushes his own. Alice does not know yet. git fetch downloads what is new. git pull fetches and then merges it.

Bob clones it

Command
git clone shared.git bob

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Bob makes a commit

Command
git -C bob commit --allow-empty -m "Bob's change"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Bob pushes it

Command
git -C bob push

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Alice downloads what is new

Command
git -C alice fetch

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Alice asks how she compares

Command
git -C alice status -sb

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Alice pulls

Command
git -C alice pull

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Alice reads her log

Command
git -C alice --no-pager log --format=%s

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: reading the first status line

First lineWhat it saysWhat to do
## main...origin/mainYou and the remote matchNothing
## main...origin/main [ahead 1]You have one commit the remote lacksPush
## main...origin/main [behind 1]The remote has one commit you lackPull
## main...origin/main [ahead 1, behind 1]Each side has a commit the other lacksPull, settle any conflict, then push

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think git status shows what is on the server right now. It does not. It shows what your last fetch or pull saw. Run git fetch first, and then status tells you the truth about the remote.

About this lab and its source: Git and Gitea lab: learn clone, commit and push on a Git server of your own

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.