Push, Pull and Clone

Connect a local repository to GitHub, push your commits, pull others' changes and clone repositories.

Lesson 6 of 30Beginner17 min read

Your commits live on your computer until you send them somewhere. A remote is another copy of the repository, usually on GitHub, that you can send commits to and receive commits from. Connecting your project to a remote is a one-time setup; after that, keeping in sync is just two commands.

git push uploads your new commits to GitHub. git pull downloads commits that other people (or you, from another computer) have pushed. And git clone copies an entire repository from GitHub onto your computer to start working on it.

In this lesson you'll connect a local project to GitHub, push and pull, clone a repository and fix the most common error beginners see: a rejected push.

Adding a remote

After creating an empty repository on GitHub, connect your local project to it:

Terminal
git remote add origin https://github.com/samlee/portfolio.git
git remote -v
  • origin is the conventional name for your main remote. It's just a nickname for the URL.
  • git remote -v lists your remotes and their URLs.
  • Copy the URL from the green Code button on the repository page.

Pushing

Terminal
git push -u origin main
  • This sends the main branch to origin.
  • -u (short for --set-upstream) links your local main to the one on GitHub, so from now on you can simply type git push.

Push a new branch the same way: git push -u origin dark-mode. Pushing only sends commits, so commit your changes first.

Pulling and fetching

Terminal
git pull

git pull downloads new commits from the remote and merges them into your current branch. Do it at the start of every work session and before you push, so you're building on the latest version.

git fetch downloads the new commits without merging them, so you can look before you combine: git log --oneline main..origin/main shows what's new on GitHub.

Cloning

To work on a repository that's already on GitHub, clone it:

Terminal
git clone https://github.com/samlee/portfolio.git
cd portfolio

git clone creates a folder, downloads every commit and branch, and sets up origin for you automatically. You don't need git init or git remote add for a cloned repository.

When git pull asks how to reconcile divergent branches

If you and someone else have both committed since you last pulled, a fast-forward isn't possible: each side has a commit the other doesn't. Instead of guessing what you want, Git stops with "You have divergent branches and need to specify how to reconcile them" followed by "fatal: Need to specify how to reconcile divergent branches". You choose a strategy once and Git remembers it:

SettingWhat git pull does when branches have diverged
git config --global pull.rebase falseMerges, creating a merge commit
git config --global pull.rebase trueReplays your local commits on top of the new remote ones
git config --global pull.ff onlyRefuses, so you decide each time

Rebasing keeps the history in a single straight line, which many teams prefer for small updates. Merging keeps a record of exactly how two lines of work came together. Either is fine while you're learning, as long as you pick one. You can override the setting for a single pull with git pull --rebase or git pull --no-rebase.

The message only appears when both sides really have new commits. If you have no new local commits, git pull simply fast-forwards your branch, whatever you've configured.

GitHub Push and Pull Syntax

Remote commands

git remote add origin <url>
git remote -v
git push -u origin <branch>
git push
git pull
git fetch
git clone <url>

GitHub Push and Pull Summary

  • A remote is another copy of the repo; origin is the usual name for GitHub's copy.
  • git remote add origin <url> connects a local project once.
  • git push -u origin main sends commits the first time; afterwards git push.
  • git pull brings in and merges others' commits; git fetch only downloads them.
  • git clone <url> copies a whole repository and sets up origin for you.

GitHub Push and Pull Examples

Each example shows the code, the output it produces and what each part does.

Example 1

First push of an existing project

Terminal
git remote add origin https://github.com/samlee/portfolio.git
git push -u origin main
Output
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Writing objects: 100% (9/9), 2.31 KiB | 2.31 MiB/s, done.
To https://github.com/samlee/portfolio.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
Line-by-line explanation
  1. Line 1Give the GitHub address the name origin.
  2. Line 2Upload main and link it, so future pushes are just git push.
Example 2

Everyday sync

Terminal
git pull
git add about.html
git commit -m "Add about page"
git push
Output
Already up to date.
[main 7c2d91a] Add about page
 1 file changed, 30 insertions(+)
To https://github.com/samlee/portfolio.git
   e7d0b3c..7c2d91a  main -> main
Line-by-line explanation
  1. Line 1Start by pulling. Nobody else has pushed, so there's nothing new.
  2. Lines 2 to 3Make a commit as usual.
  3. Line 4Push it. The output shows main moving from the old commit to the new one on GitHub.
Example 3

A rejected push, fixed

Terminal
git push
git pull
git push
Output
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/samlee/portfolio.git'
hint: Updates were rejected because the remote contains work that you do not have locally.
Merge made by the 'ort' strategy.
 README.md | 4 ++++
 1 file changed, 4 insertions(+)
To https://github.com/samlee/portfolio.git
   7c2d91a..b38e0f5  main -> main
Line-by-line explanation
  1. Line 1Someone edited the README on GitHub, so your push is rejected.
  2. Line 2Pull their commit and merge it with yours.
  3. Line 3Now your branch contains both, and the push succeeds.

Common GitHub Push and Pull Mistakes

Pushing without committing

Don't
git add .
git push      # "Everything up-to-date"
Do
git add .
git commit -m "Add gallery"
git push

Push only sends commits. Staged changes stay on your computer.

Using git init inside a cloned folder

Don't
git clone https://github.com/samlee/portfolio.git
cd portfolio
git init
git remote add origin https://github.com/samlee/portfolio.git
Do
git clone https://github.com/samlee/portfolio.git
cd portfolio

A clone is already a repository with origin set up. The extra commands are unnecessary, and the last one fails with "remote origin already exists".

Force-pushing to fix a rejection

Don't
git push --force
Do
git pull
git push

--force overwrites the remote and can delete other people's commits. Only use it when you're certain nobody else uses the branch.

GitHub Push and Pull in Real Projects

Syncing on a team

A team member starts the day with git pull, creates a branch, commits through the day, and pushes the branch to GitHub to open a pull request. Automatic deployment services such as GitHub Pages, Netlify and Vercel watch the repository and publish the site every time new commits are pushed to main.

GitHub Push and Pull FAQ

How do I clone a repository into a folder with a different name?

Add the folder name after the URL: git clone https://github.com/samlee/portfolio.git my-portfolio. To clone into the current folder, use . as the name; the folder must be empty.

How do I clone only the latest version without the full history?

Use a shallow clone: git clone --depth 1 <url>. It downloads only the most recent commit, which is much faster for big repositories when you just want the files. You can download the rest of the history later with git fetch --unshallow.

What does "error: src refspec main does not match any" mean?

Git can't find a local branch called main to push. Usually you haven't made a commit yet, so the branch doesn't exist, or your branch is called master. Check with git branch, make a commit, or push the branch by its real name.

What does "refusing to merge unrelated histories" mean?

The local and remote repositories were started separately, for example when you created a README on GitHub and also ran git init on your computer, so they share no commits. If you're sure they belong together, run git pull origin main --allow-unrelated-histories once, resolve any conflicts and push.

How do I clone a private repository?

You need access to it, either as its owner or as an invited collaborator, and Git must be able to sign in as you. With HTTPS, a credential manager or personal access token handles that; with SSH, your key must be added to your account. Otherwise GitHub responds as if the repository doesn't exist.

GitHub Push and Pull Exercises

Answer each one and check it instantly. If you get stuck, open the hint before the answer.

Fill in the blankseasy

Complete the command that sends main to GitHub for the first time and links it.

git   origin main
Multiple choiceeasy

What does git clone <url> do?

Multiple choicemedium

What's the difference between git fetch and git pull?

Predict the outputmedium

You run git push and see "rejected (fetch first)". What's the usual fix?

Multiple choicemedium

What does git fetch do that git pull doesn't?

GitHub Push and Pull Quiz

5 questions to check what you learned.

  1. What is origin?

    • The default name for the remote repository you cloned from or added
    • The first commit
    • The main branch
    • A GitHub username
  2. Which command lists your remotes and their URLs?

    • git remote list
    • git origin
    • git show remote
    • git remote -v
  3. What does the -u in git push -u origin main do?

    • Updates every branch
    • Undoes the last push
    • Remembers origin/main as the upstream, so later you can just run git push
    • Uploads only unstaged changes
  4. Why should you avoid git push --force on a shared branch?

    • It's slower
    • It can overwrite and delete commits other people pushed
    • GitHub doesn't allow it
    • It deletes your local branch
  5. What does -u do in git push -u origin main?

    • Undoes the last push
    • Updates every branch
    • Uploads untracked files
    • Sets origin/main as the upstream, so later you can just run git push