Git Clone, Push and Pull
Level 4 · Remotes and GitHub
How your copy of a repo talks to the shared one. What a remote and origin are, and what clone, push, fetch and pull each move.
Course contentsLesson 9 of 13
Level 1 · Your first commits
Level 2 · Branches and merges
Level 3 · Undo and set aside
Level 4 · Remotes and GitHub
Level 5 · Working as a team
After this lesson you can
- Clone a repo and find its remote
- Push your commits to the team
- Tell fetch apart from pull
13 mincovers 5 glossary entries
Worth reading first: Git Stash and Git Tags
How your copy and the shared copy stay in step
A remote is a copy of your repo kept somewhere else, usually on a server. Four commands move work between it and you. Clone copies the whole repo down once. Push sends your new commits up. Fetch downloads new commits without touching your files. Pull downloads them and joins them into your branch.
Git clone copies a whole repo onto your machine. You get every file, and every commit back to the first one. Git also notes where the copy came from. It calls that place origin, which is a short name for the server's web address. You never have to type the whole thing again.
Your copy and the remote only match after you push or pull. A commit you make stays on your computer until you push it. Git push sends your new commits to the remote, and then the team can get them. The first push of a new branch adds -u, which links your branch to the one on the remote.
Git refuses a push if the remote has commits you do not have yet. That is a safety rule, not an error. It stops you from wiping out a teammate's work. You get their commits first, sort out any clash, and push again.
Git fetch downloads new commits from the remote and keeps them beside your branch, under a name like origin/main. Your own files and your own branch do not move. You can look at what changed, and decide when to join it in. A fetch is always safe to run.
Git pull is a fetch plus the joining step, done at once. If only the remote moved, your branch just slides forward. If you both made commits, recent versions of Git stop and ask you to choose a merge or a rebase. Git pull --rebase picks the rebase.
Pull often, in small doses. A pull after one day of team work brings in a few commits, and maybe a small conflict. A pull after two weeks brings in a pile of changes and conflicts all over. Commit or stash your own changes before you pull, so the pull has a clean start.
The dangerous command is git push --force. It makes the remote match your copy, even if that throws away commits others pushed. On a shared branch, that can erase a day of someone's work. If you must force, use --force-with-lease, which refuses if the remote changed since you last fetched.
$ git clone https://github.com/emma/shop.git
Cloning into 'shop'...
$ cd shop
$ git remote -v
origin https://github.com/emma/shop.git (fetch)
origin https://github.com/emma/shop.git (push)
$ git switch -c footer
Switched to a new branch 'footer'
$ git push -u origin footer
To https://github.com/emma/shop.git
* [new branch] footer -> footer
branch 'footer' set up to track 'origin/footer'.Worked example
The words for this lesson5
Check yourself
What is the difference between git fetch and git pull?
Finished this lesson?
Marking it complete also marks its 5 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.