32 32. Cloning and Pulling (Working from Two Computers)
Once your repository is on GitHub, you can get a copy of it on any computer with git clone, and keep the copies in sync with git push and git pull. This page is about one person (you) using two computers, for example your laptop and a computer-lab PC, or making a quick edit on github.com. Working with other people is covered later, in Sharing a Repository: Collaborators and Forks.
LAPTOP GITHUB LAB PC
--- push ---> --- pull --->
<--- pull --- <--- push ---
GitHub is the meeting point: each computer pushes its new commits there and pulls the other computer’s commits from there.
32.1 git clone: copy a repository to a computer
In the examples, Maria pushed her practice project to GitHub on the previous page. Now she is at a Windows PC in the computer lab, using Git Bash. First she copies the repository’s URL from GitHub: on the repository’s page, she clicks the Code button and copies the HTTPS URL. Then:
mkdir -p ~/projects
cd ~/projects
git clone https://github.com/YOUR-USERNAME/git-tutorial-project.git(mkdir -p creates the projects folder if it doesn’t exist yet, and does nothing if it does.)
Output:
Cloning into 'git-tutorial-project'...
remote: Enumerating objects: 24, done.
remote: Counting objects: 100% (24/24), done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 24 (delta 5), reused 0 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (24/24), done.
Resolving deltas: 100% (5/5), done.
git clone:
- creates a new folder named after the repository (
git-tutorial-project), - downloads all the files and the complete history,
- remembers where the copy came from, as a remote named
origin, and connects the localmainbranch toorigin/main, - so you do not run
git initorgit remote addin a cloned folder.
cd git-tutorial-project
git log --onelineOutput:
736e88f (HEAD -> main, origin/main, origin/HEAD) Ignore .env and add .env.example template
fc2bb9b Revert "Make heading navy"
cb793da Make heading navy
4293e1d Add intro and contact email to home page
b90f840 Describe planned pages in README
ffd1f1f Add starter files for portfolio site
The same six commits, with the same IDs, as on her laptop. Two things are not in the clone, because they were never committed: the ignored .env file and the empty docs/ folder. Maria re-creates .env from the template (see .gitignore and Keeping Secrets Out of Git):
cp .env.example .envand then types her real key into .env with a text editor.
A few more facts about cloning:
- Each computer needs Git installed and configured (Installing and Configuring Git). To clone a private repository or to push, you must also be signed in to GitHub on that computer (Putting Your Repository on GitHub). On a shared lab computer, don’t leave your login saved: if you signed in with the GitHub CLI,
gh auth logoutremoves it when you’re done. If Git Credential Manager signed you in (the Windows default), open Windows’ Credential Manager, choose Windows Credentials, and remove the entrygit:https://github.com. - Anyone can clone a public repository without signing in. For example, GitHub’s practice repository:
git clone https://github.com/octocat/Spoon-Knife.git. You can change your copy, but you can’t push to someone else’s repository. - To use a different folder name, add it at the end:
git clone https://github.com/YOUR-USERNAME/git-tutorial-project.git portfolio. - Clone into a normal folder such as
~/projects(one that Dropbox, OneDrive or iCloud doesn’t sync; see Your First Repository), not inside another repository.
32.2 Push from the second computer
At the lab PC, Maria adds a status line to the README and commits it, exactly as on her laptop:
echo "Status: home page done, contact page next." >> README.md
git add README.md
git commit -m "Add project status to README"
git statusOutput (of git status):
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
“Ahead by 1 commit” means this computer has a commit that GitHub doesn’t have yet. Because git clone connected main to origin/main, a plain git push is enough:
git pushOutput:
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 340 bytes | 340.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
To https://github.com/YOUR-USERNAME/git-tutorial-project.git
736e88f..647a9b5 main -> main
The last line says GitHub’s main moved from commit 736e88f to 647a9b5.
32.3 git pull: bring your copy up to date
Back home, Maria opens her laptop. Watch out: git status there still says Your branch is up to date with 'origin/main'. That message only reflects what the laptop knew the last time it talked to GitHub; it doesn’t check GitHub. To actually get the new commit:
git pullOutput:
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 2), reused 0 (delta 0), pack-reused 0 (from 0)
Unpacking objects: 100% (3/3), 320 bytes | 320.00 KiB/s, done.
From https://github.com/YOUR-USERNAME/git-tutorial-project
736e88f..647a9b5 main -> origin/main
Updating 736e88f..647a9b5
Fast-forward
README.md | 1 +
1 file changed, 1 insertion(+)
git pull downloads the new commits from GitHub and adds them to your branch. Fast-forward means the laptop had no new commits of its own, so Git simply moved main forward to include the downloaded ones; there was nothing to combine.
Edits you make on github.com (for example, editing README.md in the browser) are ordinary commits on GitHub. You bring them to your computer the same way, with git pull.
32.4 The daily routine
1. git pull <- start: get the latest version
2. edit files <- (by hand, or with an AI agent)
3. git status / git diff <- review what changed
4. git add <files>
5. git commit -m "..."
6. git push <- finish: upload it for your other computer
A rule of thumb: pull when you sit down, push when you get up.
32.5 If you forget to pull: the push is rejected
One evening Maria fixed the README title on github.com. Later, on her laptop, she changed css/style.css and committed it without pulling first. Now GitHub and her laptop each have a commit the other one doesn’t have:
git pushOutput:
To https://github.com/YOUR-USERNAME/git-tutorial-project.git
! [rejected] main -> main (fetch first)
error: failed to push some refs to 'https://github.com/YOUR-USERNAME/git-tutorial-project.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.
Nothing is broken. Git refuses to push because that would throw away the commit on GitHub. The fix is the hint’s advice: pull, then push.
git pullBecause both sides have new commits, git pull creates a merge commit that combines them. Git opens your text editor with a ready-made message (Merge branch 'main' of https://github.com/...); just save and close it (in nano: Ctrl+O, Enter, Ctrl+X). The output ends with:
Merge made by the 'ort' strategy.
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Now git push works, and GitHub has both changes.
If git pull stops with this message (after several hint: lines), Git doesn’t know whether to merge or do something else. Set the beginner-friendly default once (see Installing and Configuring Git), then pull again:
git config --global pull.rebase false
git pullTwo other things can stop a pull:
- Uncommitted changes to the same files. Git prints
error: Your local changes to the following files would be overwritten by merge:and does nothing. Commit your changes (or discard them withgit restore), then pull again. - The same lines were changed on both computers. Git can’t decide which version to keep and reports a merge conflict, which you resolve by hand. See Merge Conflicts. Pulling before you start work is the best way to avoid conflicts.
git log --oneline --graph draws the history as a graph. After the merge above, Maria’s laptop shows:
* c0d14e5 (HEAD -> main) Merge branch 'main' of https://github.com/YOUR-USERNAME/git-tutorial-project
|\
| * 0b2c2b7 (origin/main) Update README.md
* | b25b21a Increase line spacing
|/
* 647a9b5 Add project status to README
The two lines of work split after 647a9b5 (the web edit on one side, the laptop’s CSS commit on the other) and come back together in the merge commit. After git push, origin/main points to the merge commit too.
It’s tempting to keep the repository in a Dropbox, OneDrive, iCloud or Google Drive folder, so that it “just appears” on your other computer. Don’t. The sync service copies the hidden .git folder one file at a time, and when both computers change it, you get “conflicted copy” files or a broken repository. It also uploads your .env files with their secret keys, which .gitignore keeps out of GitHub but not out of Dropbox. Push and pull are the safe way to move your work, and every commit you push is already backed up on GitHub. (See “Where to keep your projects” in Your First Repository.)
32.6 Forking vs. cloning
Cloning copies a repository to your computer. You can clone any public repository, but you can push only to repositories you own or have been given access to. A fork is a copy of someone else’s repository made on GitHub, in your own account (using the Fork button). Because the fork belongs to you, you can clone it and push to it. People fork projects to build their own version or to propose changes to the original. The full-stack tutorial starts this way (Fork the repo and clone it), and Sharing a Repository: Collaborators and Forks explains forks in detail.
Practice Questions
Easy
Question 1 · Multiple choice
What does git clone https://github.com/YOUR-USERNAME/git-tutorial-project.git do?
- Uploads your local project to GitHub
- Creates a folder
git-tutorial-projectwith all the files and the full history, and sets uporigin - Copies only the latest version of the files, without history
- Creates an empty repository on GitHub
Question 2 · True or false
After cloning a repository, you need to run git init and git remote add origin ... in the new folder.
Question 3 · Short answer
In one sentence, what does git pull do?
Question 4 · Matching
Match each term with its meaning.
| Term | Meaning | |
|---|---|---|
1. git clone |
A. upload your new commits to GitHub | |
2. git pull |
B. the usual name for the remote your copy came from | |
3. git push |
C. download a repository (files and history) for the first time | |
4. origin |
D. a copy of someone else’s repository in your own GitHub account | |
| 5. fork | E. get new commits from GitHub into your existing copy |
Question 5 · Fill in the blank
Rule of thumb for working from two computers: “________ when you sit down, ________ when you get up.”
Question 6 · True or false
You can clone a public GitHub repository without signing in to GitHub.
Medium
Question 7 · Write the command
Write the command that clones https://github.com/YOUR-USERNAME/git-tutorial-project.git into a folder named portfolio instead of the default name.
Question 8 · Read the output
What does this git status output mean, and what should Maria do next?
On branch main
Your branch is ahead of 'origin/main' by 2 commits.
(use "git push" to publish your local commits)
Question 9 · Ordering
Put the daily routine in order:
- A.
git push - B. edit files
- C.
git pull - D.
git commit -m "..." - E.
git statusandgit diff - F.
git add <files>
Question 10 · Short answer
Maria pushed a commit from the lab PC an hour ago. On her laptop, git status says Your branch is up to date with 'origin/main'. Is the laptop really up to date? Explain.
Question 11 · Find the bug
Sam runs git clone https://github.com/YOUR-USERNAME/git-tutorial-project.git in his projects folder and gets:
fatal: destination path 'git-tutorial-project' already exists and is not an empty directory.
What does this mean, and what are his options?
Question 12 · Read the output
The output of git pull contains the word Fast-forward. What does that tell you?
Question 13 · Scenario
Maria clones her repository on the lab PC and runs git remote -v. What does she see, and why didn’t she have to add the remote herself?
Hard
Question 14 · Scenario
Maria edited the README on github.com, then (without pulling) committed a CSS change on her laptop. git push prints ! [rejected] main -> main (fetch first). Explain why the push was rejected, and list the commands to fix it. What happens during the fix?
Question 15 · Find the bug
In the same situation, Sam’s git pull prints several hint: lines and ends with:
fatal: Need to specify how to reconcile divergent branches.
Why, and how does he fix it?
Question 16 · Scenario
Maria starts editing README.md on her laptop before pulling. When she runs git pull, Git prints error: Your local changes to the following files would be overwritten by merge: followed by README.md, and Aborting. What happened, and what can she do?
Question 17 · Compare
Compare cloning and forking: where does the copy live, and can you push to it?
Question 18 · Scenario
Maria clones a classmate’s public repository, improves the README, and commits. git push fails with a “permission denied” error. Why? What could she do instead?
Question 19 · Long answer
Maria works on her project from a laptop, a lab PC, and sometimes github.com. Describe a routine that keeps all copies in sync, and explain what can go wrong if she doesn’t follow it.