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:
git remote add origin https://github.com/samlee/portfolio.git
git remote -voriginis the conventional name for your main remote. It's just a nickname for the URL.git remote -vlists your remotes and their URLs.- Copy the URL from the green Code button on the repository page.
Pushing
git push -u origin main- This sends the
mainbranch toorigin. -u(short for--set-upstream) links your localmainto the one on GitHub, so from now on you can simply typegit 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
git pullgit 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:
git clone https://github.com/samlee/portfolio.git
cd portfoliogit 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:
| Setting | What git pull does when branches have diverged |
|---|---|
git config --global pull.rebase false | Merges, creating a merge commit |
git config --global pull.rebase true | Replays your local commits on top of the new remote ones |
git config --global pull.ff only | Refuses, 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;
originis the usual name for GitHub's copy. git remote add origin <url>connects a local project once.git push -u origin mainsends commits the first time; afterwardsgit push.git pullbrings in and merges others' commits;git fetchonly downloads them.git clone <url>copies a whole repository and sets uporiginfor you.
GitHub Push and Pull Examples
Each example shows the code, the output it produces and what each part does.
First push of an existing project
git remote add origin https://github.com/samlee/portfolio.git
git push -u origin mainEnumerating 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
- Line 1Give the GitHub address the name origin.
- Line 2Upload main and link it, so future pushes are just git push.
Everyday sync
git pull
git add about.html
git commit -m "Add about page"
git pushAlready up to date.
[main 7c2d91a] Add about page
1 file changed, 30 insertions(+)
To https://github.com/samlee/portfolio.git
e7d0b3c..7c2d91a main -> mainLine-by-line explanation
- Line 1Start by pulling. Nobody else has pushed, so there's nothing new.
- Lines 2 to 3Make a commit as usual.
- Line 4Push it. The output shows main moving from the old commit to the new one on GitHub.
A rejected push, fixed
git push
git pull
git push ! [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 -> mainLine-by-line explanation
- Line 1Someone edited the README on GitHub, so your push is rejected.
- Line 2Pull their commit and merge it with yours.
- Line 3Now your branch contains both, and the push succeeds.
Common GitHub Push and Pull Mistakes
Pushing without committing
git add .
git push # "Everything up-to-date"git add .
git commit -m "Add gallery"
git pushPush only sends commits. Staged changes stay on your computer.
Using git init inside a cloned folder
git clone https://github.com/samlee/portfolio.git
cd portfolio
git init
git remote add origin https://github.com/samlee/portfolio.gitgit clone https://github.com/samlee/portfolio.git
cd portfolioA 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
git push --forcegit 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.
Complete the command that sends main to GitHub for the first time and links it.
git origin main
What does git clone <url> do?
What's the difference between git fetch and git pull?
You run git push and see "rejected (fetch first)". What's the usual fix?
What does git fetch do that git pull doesn't?
GitHub Push and Pull Quiz
5 questions to check what you learned.
What is
origin?- The default name for the remote repository you cloned from or added
- The first commit
- The main branch
- A GitHub username
Which command lists your remotes and their URLs?
- git remote list
- git origin
- git show remote
- git remote -v
What does the
-uingit push -u origin maindo?- 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
Why should you avoid
git push --forceon 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
What does
-udo ingit 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