Clone, pull and fetch: working on more than one computer
Download a repository with git clone, bring in other people's commits with git pull, check for changes safely with git fetch, and fix a rejected push by pulling first.
🎯 By the end of this lesson you will be able to:
- Copy a repository from GitHub with git clone
- Update your copy with git pull, and preview changes with git fetch
- Understand why a push is rejected and fix it
Outline
What you'll learn and why it matters
As soon as a project lives on GitHub, it can change without you: a teammate pushes, or you push from another computer. This lesson is about keeping copies in step: getting a copy (clone), getting new commits (pull) and checking what is new (fetch).
| Command | Direction | Does |
|---|---|---|
git clone <url> | GitHub → new folder | Downloads a whole repository, with all its history, once |
git pull | GitHub → your branch | Downloads new commits and merges them into your branch |
git fetch | GitHub → Git's memory | Downloads new commits but does not change your files |
git push | Your branch → GitHub | Uploads your new commits |
git clone: get a copy
Asha wants to work on her laptop too. She opens a terminal in the folder where she keeps projects and clones the repository, using the address from GitHub's green Code button:
cd ..
git clone https://github.com/asha-kumar/my-website.git laptop
cd laptop
git log --oneline
git remote -vOutput
Cloning into 'laptop'... 1ce7ed1 (HEAD -> main, origin/main, origin/HEAD) Add intro sentence ec605b9 Add stylesheet 6103896 Add home page origin https://github.com/asha-kumar/my-website.git (fetch) origin https://github.com/asha-kumar/my-website.git (push)
clone creates the folder (here called laptop; without a name it would be called my-website), downloads every commit and sets up origin automatically. There is no need for git init or git remote add.
git pull: bring in new commits
On her laptop, Asha added a sentence about freelance work and pushed it. Back on her desktop, the project does not have that commit yet. git pull fetches it and updates the branch:
git pull
git log --oneline -2
cat index.htmlOutput
From https://github.com/asha-kumar/my-website 1ce7ed1..aca1eaa main -> origin/main Updating 1ce7ed1..aca1eaa Fast-forward index.html | 1 + 1 file changed, 1 insertion(+) aca1eaa (HEAD -> main, origin/main, origin/HEAD) Mention freelance work 1ce7ed1 Add intro sentence <h1>Asha Kumar</h1> <p>Junior web developer</p> <p>I build accessible websites.</p> <p>Now available for freelance work.</p>
This pull was a fast-forward: the desktop had no new commits of its own, so Git just moved main forward. If both sides had new commits, git pull would make a merge commit, or show a conflict to resolve exactly as in lesson 7.
git fetch: look before you pull
git fetch downloads new commits into Git's memory (origin/main) without touching your files. Then you can see what is coming:
git fetch -q
git status
git log --oneline main..origin/mainOutput
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 clean aca1eaa (origin/main, origin/HEAD) Mention freelance work
main..origin/main means *commits on origin/main that main does not have*. When you are happy, git pull (or git merge origin/main) brings them in.
When your push is rejected
If GitHub has commits you do not have, Git refuses to push, so you never overwrite someone else's work by accident:
printf 'footer { color: gray; }\n' >> styles.css
git commit -q -am "Style the footer"
git pushOutput
To https://github.com/asha-kumar/my-website.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/asha-kumar/my-website.git' hint: Updates were rejected because the remote contains work that you do not hint: have locally. This is usually caused by another repository pushing to hint: the same ref. If you want to integrate the remote changes, use hint: 'git pull' before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
The fix is in the hint: pull first, then push. Here the two changes were in different files, so Git merges them automatically (with the default merge message):
printf 'footer { color: gray; }\n' >> styles.css
git commit -q -am "Style the footer"
git pull
git push -q
git log --graph --format="%s" -4Output
From https://github.com/asha-kumar/my-website 1ce7ed1..aca1eaa main -> origin/main Merge made by the 'ort' strategy. index.html | 1 + 1 file changed, 1 insertion(+) * Merge branch 'main' of https://github.com/asha-kumar/my-website |\ | * Mention freelance work * | Style the footer |/ * Add intro sentence
In your own terminal, git pull may open your editor with the message *Merge branch 'main' of …*: save and close it to finish.
Practice
Guided exercise
Clone, change, push
Clone Asha's repository into a folder called copy, add a commit there and push it.
- From the folder above your project,
git clone <address> copyandcd copy. - Change a file and commit it.
git push(no-uneeded: clone already linked the branch).
Show the solution
cd ..
git clone -q https://github.com/asha-kumar/my-website.git copy
cd copy
printf '<p>Updated from another computer.</p>\n' >> index.html
git commit -q -am "Add update note"
git push
git statusOutput
To https://github.com/asha-kumar/my-website.git 1ce7ed1..0fe1b78 main -> main On branch main Your branch is up to date with 'origin/main'. nothing to commit, working tree clean
How to get there: A cloned repository is already connected to origin and its main branch already tracks origin/main, so a plain git push works straight away.
Your turn
Catch up
Asha's laptop pushed a new commit. On the desktop, check with fetch and status first, then pull and show the latest two commits.
Show the solution
git fetch -q
git status
git pull -q
git log --oneline -2Output
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 clean aca1eaa (HEAD -> main, origin/main, origin/HEAD) Mention freelance work 1ce7ed1 Add intro sentence
How to get there: Fetch updates origin/main, so status can say you are behind by 1 commit. Pull then brings the commit into main. After that, main and origin/main point at the same commit.
Common mistakes
Running git clone inside another repository
You see: A repository inside a repository, and confusing status output
Fix: cd .. to a normal folder (not a project) before cloning.
Forcing a rejected push with git push --force
You see: Your teammate's commits are deleted from GitHub
Fix: Pull first, resolve any conflicts, then push. Never force-push shared branches.
Starting work without pulling
You see: Conflicts later that could have been avoided
Fix: Run git pull at the start of each session.
Expecting git fetch to change your files
You see: Nothing seems to happen
Fix: Fetch only updates origin/main; use git pull or git merge origin/main to bring the changes in.
Recap
git clone <url>downloads a repository with its history and sets up origin.git pullbrings new commits into your branch; start each session with it.git fetchdownloads without changing your files; compare with main..origin/main.- A rejected push means GitHub has commits you lack: pull, resolve, push. Never force-push shared work.
Key terms
- Clone
- A full copy of a remote repository on your computer, including history.
- Pull
- Fetch new commits from the remote and merge them into your branch.
- Fetch
- Download new commits from the remote without changing your branch or files.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which command downloads a GitHub repository you do not have yet?
2. Your push is rejected with `fetch first`. What should you do?
3. What is the difference between fetch and pull?
4. Why run git pull before starting work?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.