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 fetchdownloads new commits from the remote but does not touch your files. The remote's state appears as remote-tracking branches likeorigin/main, which you can inspect withgit log origin/main.git pullis 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.
# 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
mainwhen the team works through branches and pull requests.
Summary
clonecopies a repository and sets uporigin.pushsends commits;fetchdownloads;pulldownloads and merges.- Pull before you push when others share the branch.
Quiz: Remotes, push and pull