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
| Word | What it means |
|---|---|
| Remote | a copy of the repository on another computer |
| Origin | the usual name for the remote you cloned from |
| Clone | to copy a repository with all of its history |
| Push | to send your new commits to the remote |
| Fetch | to download new commits without merging them |
| Pull | to 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
git ls-remote --heads http://gitea:3000/student/hello-git.gitShown 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
git init --bare -b main shared.gitShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Alice clones it
git clone shared.git aliceShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Alice makes a commit
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
git -C alice push -u origin mainShown 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
git clone shared.git bobShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Bob makes a commit
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
git -C bob pushShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Alice downloads what is new
git -C alice fetchShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Alice asks how she compares
git -C alice status -sbShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Alice pulls
git -C alice pullShown here, not run. Create a free account to press Run and type it into your own lab’s terminal.
Alice reads her log
git -C alice --no-pager log --format=%sShown 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 line | What it says | What to do |
|---|---|---|
| ## main...origin/main | You and the remote match | Nothing |
| ## main...origin/main [ahead 1] | You have one commit the remote lacks | Push |
| ## main...origin/main [behind 1] | The remote has one commit you lack | Pull |
| ## main...origin/main [ahead 1, behind 1] | Each side has a commit the other lacks | Pull, 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.