Himanshu SharmaSoftware engineer
Let’s talk

Git and GitHub · Lesson 14 of 30

Remotes: clone, fetch, pull and push

Share Git history through a remote repository. Clone, push, fetch and pull, understand origin and tracking branches, and handle a rejected push without force.

16 minRemotes and GitHubUpdated 14 November 2026

Awaiting technical review. This lesson is readable but not yet listed in search engines.

By the end of this lessonClone a repository, push and pull commits through a remote, explain the difference between fetch and pull, and fix a rejected push.

What you can do after this lesson

You will share work through a central repository: copy one, send your commits to it, and receive other people's. We will do all of it on your own machine, using a bare repository as the remote. A bare repository has history but no working files, which is exactly what a server or GitHub holds. That lets you practise everything here with no account and no network.

What a remote is

A remote is just another copy of the repository, reachable by a path or a URL. Git stores a short name for it. When you clone, the source is named origin automatically. You can have several remotes with different names.

Creating a practice remote and cloning

bash
git init --bare --initial-branch=main origin.git
git clone origin.git asha
text
Initialized empty Git repository in /home/you/r/origin.git/
Cloning into 'asha'...
warning: You appear to have cloned an empty repository.
done.

The warning is expected: nothing has been pushed yet. Inside the clone, give it an identity and look at the remote:

bash
cd asha
git config user.name "Asha Rao"
git config user.email "asha@example.com"
git remote -v
text
origin	/home/you/r/origin.git (fetch)
origin	/home/you/r/origin.git (push)

Notice there are fetch and push URLs. They are usually identical. A real remote URL looks like https://example.com/team/project.git or an SSH address; the lessons on GitHub cover those.

Pushing

Commit something, then send it:

bash
git push -u origin main
text
To /home/you/r/origin.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.

git push <remote> <branch> uploads commits the remote lacks. The -u flag also remembers the pairing, so later you can type just git push and git pull. After that, git status can report your position:

text
On branch main
Your branch is up to date with 'origin/main'.

origin/main is a remote-tracking branch: a read-only bookmark in your repository recording where main was on the remote at the last time you talked to it.

A teammate clones

Another person, Ben, clones the same remote and gets everything, history included:

bash
git clone origin.git ben
cd ben
git branch -a
text
* main
  remotes/origin/HEAD -> origin/main
  remotes/origin/main

-a lists local and remote-tracking branches. Both of them now commit and push independently, so sooner or later one of them is behind.

Fetch and pull

Ben pushes a commit. Asha, who has not looked, now has stale knowledge. She can look without changing anything:

bash
git fetch
git log --oneline HEAD..origin/main
text
From /home/you/q/origin
   72f71ee..0cc9f6e  main       -> origin/main
0cc9f6e Ben: add b3

git fetch downloaded Ben's commit and moved origin/main, but her own main and files did not change. HEAD..origin/main lists commits on the remote that she lacks. She then integrates with a merge, here a fast-forward:

bash
git merge origin/main
text
Updating 72f71ee..0cc9f6e
Fast-forward
 b3.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 b3.txt

git pull does those two steps in one command: fetch, then merge (or rebase) into the current branch. Many people prefer the explicit fetch first because they can inspect before integrating. If you want pull to refuse anything that is not a clean fast-forward, use git pull --ff-only.

A rejected push

This is the situation every collaborator meets. Both Asha and Ben committed from the same starting point, and Ben pushed first:

bash
git push
text
To /home/you/q/origin.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to '/home/you/q/origin.git'
hint: Updates were rejected because the remote contains work that you do not
hint: have locally. ...

Git refuses, because accepting would erase Ben's commit from the remote's history. Do not force. Bring his work in, then push. You can rebase your commit on top of his:

bash
git pull --rebase
git push
text
From /home/you/q/origin
   9019217..f255680  main       -> origin/main
Rebasing (1/1)Successfully rebased and updated refs/heads/main.
To /home/you/q/origin.git
   f255680..72f71ee  main -> main

The history stays a straight line: Ben's commit, then Asha's. Merging instead would also work. In Git 2.55, a plain git pull on diverged branches stops and asks you to choose, printing hints about pull.rebase. You can set a preference once:

bash
git config pull.rebase true

The value false means merge, true means rebase, and pull.ff only demands a fast-forward. Pick one as a team.

Useful variations

  • Shallow clone: git clone --depth 1 <url> copies only the latest commit, which is faster for large histories, with a limited history.
  • Choose the folder name: git clone origin.git custom-name.
  • Clone one branch: git clone -b topic origin.git topic-only checks out topic.
  • Push a new branch: git push -u origin feature/search; delete it remotely with git push origin --delete feature/search.
  • List remote branches: git branch -r, and tidy stale tracking branches with git fetch --prune.
  • Change a remote: git remote rename, git remote set-url.

Force pushing, with care

If you rewrote your own branch, for instance after amending or rebasing, a normal push is rejected. git push --force-with-lease overwrites the remote branch only if it is still where you last saw it. In our test it correctly refused with stale info when a teammate had pushed to the branch in the meantime. Plain --force has no such check and can silently destroy a teammate's commits.

Common mistakes

  • Using --force to get past a rejection. The rejection is protecting someone's work.
  • Expecting git fetch to change your files. It only updates tracking branches.
  • Forgetting that commits are local until pushed. A commit on your machine is not shared.
  • Running git pull with uncommitted changes. Commit or stash first.
  • Cloning into a folder that is already a repository. Check where you are first.

Exercise

  1. Create a bare repository and clone it twice into two folders, one and two.
  2. In one, commit a file and push it with -u. In two, run git pull.
  3. Commit different files in both clones. Push from one, then try to push from two.
  4. Resolve the rejection with git pull --rebase, then push.
Show one possible answer
bash
git init --bare --initial-branch=main hub.git
git clone hub.git one && git clone hub.git two
cd one && echo 1 > a.txt && git add . && git commit -m "Add a" && git push -u origin main
cd ../two && git pull
echo 2 > b.txt && git add . && git commit -m "Add b"
cd ../one && echo 3 > c.txt && git add . && git commit -m "Add c" && git push
cd ../two && git push
git pull --rebase
git push

The first push from two is rejected, and after the rebase it succeeds. Set a name and email inside each clone before committing.

Next: GitHub accounts, SSH keys and access tokens explained in words.

Key points

  • A remote is another copy of the repository, by convention named origin when you cloned it.
  • git clone copies a repository with its full history; git push sends your commits; git fetch downloads others' commits without touching your files.
  • git pull is a fetch followed by a merge or rebase into your current branch.
  • Remote-tracking branches such as origin/main are your local record of where the remote's branches were at the last fetch.
  • A push is rejected when the remote has commits you lack; fetch and integrate them, then push again, rather than forcing.

Check yourself

1. What does git fetch do?
2. git pull is best described as
3. Your git push is rejected with non-fast-forward. What should you do first?
4. What does git push -u origin main do beyond pushing?