git clone, git fetch & git pull
Clone and stay up to date
Lesson 14 of 20 · 10 min · Module 4: Working with GitHub
GitHub works in both directions: you can download any repository you have access to, and bring in what your teammates pushed.
Start this lesson →Free. No installation: you write and run the code in your browser, and it is checked automatically.
By the end, you’ll be able to…
- Clone a repository from GitHub
- Tell git fetch and git pull apart
- Check whether you are behind
The idea
git clone URL downloads a whole repository with its history and sets up origin for you. After that, your copy does not update by itself. git fetch downloads new commits into origin/main, Git's memory of GitHub, without touching your files; git status then tells you whether you are behind. git pull fetches and merges in one step.
git clone https://github.com/codetemper/git-cheatsheet.git
git fetch
git status
git pull
01Clone: a full copy with history
git clone downloads a repository with every commit and sets up origin to point back to where it came from. It creates a folder named after the repository.
cd ~
git clone https://github.com/codetemper/git-cheatsheet.gitCloning into 'git-cheatsheet'...
remote: Enumerating objects: 9, done.
remote: Counting objects: 100% (9/9), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 9 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (9/9), done.
Resolving deltas: 100% (1/1), done.02fetch looks, pull also updates
Your repository does not watch GitHub. git fetch downloads new commits and updates origin/main without touching your files; git status then compares main with it.
cd ~/website
git fetch
git statusremote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 329 bytes | 329.00 KiB/s, done.
From https://github.com/learner/website
4abfd2a..5a4cd2e main -> origin/main
On branch main
Your branch is behind 'origin/main' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean03Pull to catch up
git pull is git fetch followed by a merge. When you have no new commits of your own, the merge is a fast-forward: main simply moves to origin/main.
git pullUpdating 4abfd2a..5a4cd2e
Fast-forward
reading.md | 3 +++
1 file changed, 3 insertions(+)
create mode 100644 reading.mdYour turn
https://github.com/codetemper/git-cheatsheet.git, then list its history. Then, back in ~/website: your teammate Sam pushed a commit to GitHub since you last looked. Run git status, then git fetch and git status again to see the difference, and bring in Sam's commit with git pull.What the checks look for
- The cheatsheet is cloned in ~/git-cheatsheet with its history
- Lists the cheatsheet history
- Fetches and sees that you are behind
- Your website has Sam's commit
Take this with you
Clone once; fetch to look; pull to bring changes in.
Go a little further
After git fetch, git log --oneline main..origin/main lists the commits you are about to pull.