TetSharing
Git & CLI tutorial · 12 chapters
  1. Terminal basics
  2. Working with files
  3. Pipes and redirection
  4. What Git is
  5. Staging and commits
  6. Viewing history
  7. Comparing changes
  8. Branches
  9. Merging
  10. Merge conflicts
  11. Undoing changes
  12. Remotes, push and pull

Final Git & CLI quiz
Cheat sheet: git commands
Cheat sheet: linux shell commands

Chapter 12 of 12 · By Ethan Cole · Updated November 21, 2026

Git Remotes: clone, push, fetch and pull Explained

So far everything has happened in one repository on one computer. A remote is another copy of the repository, usually on a server such as GitHub, GitLab or a company Git host. Teams share work by pushing their commits to a remote and pulling everyone else's.

Cloning

git clone <url> downloads a full copy of a repository, history included, into a new folder. The address can be https://..., an SSH address like git@host:team/project.git, or even a path on the same machine. Git automatically names the source remote origin.

git remote -v lists the remotes of a repository with their addresses. You can add one to an existing repo with git remote add origin <url>.

Pushing

git push origin main sends your new commits on main to the remote. The first time, add -u (git push -u origin main) to remember the connection, so later you can just type git push. Git refuses to push if the remote has commits you do not have yet; you must bring those in first, which prevents you from overwriting a teammate's work.

Fetching and pulling

  • git fetch downloads new commits from the remote but does not touch your files. The remote's state appears as remote-tracking branches like origin/main, which you can inspect with git log origin/main.
  • git pull is a fetch followed by a merge (or a rebase, if configured) of the remote branch into yours.

Fetching first and looking before you merge is the cautious habit; pull is the convenient one.

A remote you can run locally

Our transcript does not need the internet. It creates a bare repository (a repository with no working files, which is what servers store) to act as the remote, clones it twice to simulate two teammates, and passes a commit from one to the other with push and pull.

Terminal · bash + git · copy and run locally
# A bare repository plays the role of the server
git init -q --bare server.git
git clone -q server.git alice
cd alice
echo "Shared notes" > notes.txt
git add notes.txt
git commit -q -m "Add notes"
git remote -v
git push -u origin main
cd ..
git clone -q server.git bob
cd alice
echo "More notes" >> notes.txt
git commit -q -am "Add more notes"
git push -q
cd ../bob
git pull -q
git log --oneline --format="%an: %s"

Transcript (verified by our test suite in a fresh temp folder)

$ git init -q --bare server.git
$ git clone -q server.git alice
warning: You appear to have cloned an empty repository.
$ cd alice
$ echo "Shared notes" > notes.txt
$ git add notes.txt
$ git commit -q -m "Add notes"
$ git remote -v
origin	~/demo/server.git (fetch)
origin	~/demo/server.git (push)
$ git push -u origin main
To ~/demo/server.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
$ cd ..
$ git clone -q server.git bob
$ cd alice
$ echo "More notes" >> notes.txt
$ git commit -q -am "Add more notes"
$ git push -q
$ cd ../bob
$ git pull -q
$ git log --oneline --format="%an: %s"
Ethan Cole: Add more notes
Ethan Cole: Add notes

In the push output, the line starting with To names the remote that was updated, and main -> main shows which branch moved. Git writes these to the error stream even when everything works, which is why they appear mixed in with normal output.

Authentication

Hosting services require you to prove who you are before pushing. Use an SSH key or the credential helper your host recommends. Never paste passwords or tokens into a repository's files or commit messages.

Common mistakes

  • Pushing before pulling on a busy branch and getting "rejected". Run git pull, resolve anything needed, then push again.
  • Committing directly to a shared main when the team works through branches and pull requests.

Summary

  • clone copies a repository and sets up origin.
  • push sends commits; fetch downloads; pull downloads and merges.
  • Pull before you push when others share the branch.

Quiz: Remotes, push and pull

1. What is origin?

Clone names the source remote origin by default.

2. Which command downloads new remote commits without changing your files?

fetch updates remote-tracking branches only; pull also merges.

3. Your push is rejected because the remote has new commits. What should you do first?

Bring in the remote commits first so you do not overwrite others' work.

Official docs

Written under the pen name Ethan Cole with AI-assisted drafting. Every code example on this page is executed by our test suite before publishing. Found a mistake? Tell us.