Find a course

Module 4: Working with GitHubLesson 9 of 1213 min

Remotes: Push, Clone, Fetch and Pull

Connect a repository to GitHub, authenticate securely, push your branches, clone projects, and keep up to date with fetch and pull.

Step 1 of 16Learn

A copy that survives a dead laptop

Lena's recipe repository has six months of history, all of it in one .git folder on one laptop. When the laptop's drive fails, the history goes with it. Putting a copy on GitHub protects the work and makes it possible for her brother to contribute from his own computer.

The first step happens on github.com. After signing in, she clicks New repository, names it recipes, chooses Public or Private, and, because she already has a local repository, leaves the options to add a README, .gitignore or license unchecked. Adding them would create a first commit on GitHub that her local history doesn't have, and the two would need merging before her first push.

GitHub then shows an empty repository page with a URL, either HTTPS (https://github.com/lena/recipes.git) or SSH (git@github.com:lena/recipes.git), plus the exact commands to connect a local repository.

If you're starting a project from scratch, the opposite route is often easier: create the repository on GitHub with a README, then clone it to your computer. Either way, you end up with two copies of the same history, one local and one on GitHub, that you keep in sync.

Step 2 of 16Learn

Remotes and origin

A remote is a named link from your local repository to another copy of it. Lena adds one:

git remote add origin https://github.com/lena/recipes.git

origin is just a name, but it's the universal convention for "the main copy I cloned from or push to," so stick with it. You can list remotes and their URLs:

git remote -v

Which URL type should you use?

  • HTTPS works everywhere, including behind strict company firewalls. You authenticate with a credential helper or a token.
  • SSH uses a key pair stored on your computer. Once set up, it never asks for credentials.

Both are secure; pick one and set it up once. You can change a remote's URL later with git remote set-url origin <new-url>.

Git also keeps remote-tracking branches, such as origin/main, which record where each branch on the remote was the last time you talked to it. They're read-only bookmarks: you don't commit to origin/main directly. They update when you fetch, pull or push, and they let Git tell you things like Your branch is ahead of 'origin/main' by 2 commits.

Step 3 of 16Diagram

Local and remote

Local and remoteProcess diagram: Your repository → git push → origin (GitHub) → git fetch / pull → origin/main.Your repositorygit pushorigin (GitHub)git fetch / pullorigin/main

Tap a shape with to see more.

Your repository

Your commits on main.

git push

Uploads your new commits.

origin (GitHub)

The shared copy everyone syncs with.

git fetch / pull

Downloads teammates' commits.

origin/main

Your local bookmark of where GitHub's main was at the last fetch.

Step 4 of 16Practice · Fill in the blanks

Fill in the blanks

To connect your local repository to a new GitHub repository under the conventional name, run `git remote add https://github.com/you/project.git`.

Step 5 of 16Learn

Authenticating with GitHub

GitHub doesn't accept your account password for Git operations; it stopped in 2021. You need one of these instead:

  • A credential manager. Git for Windows includes Git Credential Manager, and macOS can use the Keychain. The first time you push over HTTPS, a browser window asks you to sign in to GitHub, and the credential is stored securely for next time. This is the easiest route for most people.
  • GitHub CLI. Running gh auth login signs you in through the browser and configures Git to use it.
  • A personal access token. A long random string generated in GitHub's settings that you paste when Git asks for a password. Give it only the permissions it needs and an expiration date.
  • An SSH key. Generate a key pair with ssh-keygen -t ed25519, add the public key (the .pub file) to your GitHub account settings, and use SSH URLs. The private key never leaves your computer.

Whatever you choose, protect it like a password. Never paste tokens into code, commit messages or chat. Turn on two-factor authentication for your GitHub account, which GitHub requires for accounts that contribute code.

Step 6 of 16Compare

Four ways to sign in from Git

AspectCredential managerGitHub CLIAccess tokenSSH key
SetupBrowser sign-in on first pushgh auth loginCreate in GitHub settingsssh-keygen, upload .pub
URL typeHTTPSHTTPSHTTPSSSH (git@github.com:...)
Best forMost beginnersPeople who use gh anywayScripts, CI, limited scopesLong-term power users
Watch outSign in to the right accountKeep gh updatedSet expiry and least scopesNever share the private key

Step 7 of 16Practice · Choose one

Git asks for your password when you push over HTTPS, and your GitHub account password is rejected. What should you use instead?

Step 8 of 16Learn

Your first push

With the remote added, Lena sends her main branch to GitHub:

git push -u origin main

This means: push the local main branch to the remote called origin. The -u flag (short for --set-upstream) also links local main to origin/main as its upstream. From then on, a plain git push or git pull on main knows where to go, and git status reports whether you're ahead or behind.

Refresh the GitHub page and the files, commit history and README (if any) are all there.

The same command shares a new branch:

git switch -c new-layout

git push -u origin new-layout

Now the branch exists on GitHub, where teammates can see it and you can open a pull request from it.

Remember what push does and doesn't do. It uploads commits. Uncommitted edits and staged-but-uncommitted changes stay on your computer. If a change isn't on GitHub after a push, check git status and git log: you probably haven't committed it yet. And push only sends the branch you name (or the current one), not every branch you have.

Step 9 of 16Practice · True or false

git push uploads your uncommitted edits to GitHub along with your commits.

Step 10 of 16Learn

Cloning an existing repository

To get a copy of a repository that already exists on GitHub, whether it's your team's project or an open-source library, use git clone with the URL from the green Code button:

git clone https://github.com/lena/recipes.git

Git creates a folder named recipes, downloads the entire history of every branch, checks out the default branch and sets up origin to point back to the URL. You can start working immediately; there's no need to run git init or git remote add.

A few variations:

  • git clone <url> my-folder clones into a folder with a different name.
  • git clone --depth 1 <url> downloads only the latest snapshot, not the full history. It's faster for huge repositories when you only need the current code.

After cloning, git branch shows only main locally, but git branch -a reveals the remote branches as remotes/origin/.... To work on one, just switch to it: git switch new-layout notices that origin/new-layout exists and creates a local branch that tracks it.

You can clone any public repository. You can only push to repositories where you've been given write access; for everything else, there are forks, covered in the collaboration lesson.

Step 11 of 16Diagram

A daily sync rhythm

A daily sync rhythmCycle diagram: git pull → Edit and commit → git fetch → git push.Stay in syncgit pullEdit and commitgit fetchgit push

Tap a shape with to see more.

git pull

Start the day with teammates' work.

Edit and commit

Small commits on your branch.

git fetch

Check what changed on GitHub before you push.

git push

Share your commits.

Step 12 of 16Practice · Match the pairs

Match each command to its job.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. git clone <url>
  2. git remote -v
  3. git push -u origin main
  4. git fetch
  5. git pull

Step 13 of 16Learn

fetch, pull, and the rejected push

When teammates push, your local copy doesn't change by itself. Two commands bring their work down.

git fetch downloads new commits and updates remote-tracking branches like origin/main, but leaves your own branches and files untouched. It's always safe. Afterward you can inspect what arrived with git log main..origin/main or git diff main origin/main.

git pull is fetch followed immediately by integrating the changes into your current branch, by merging (the default) or by rebasing if you've set pull.rebase true or run git pull --rebase. If your branch had no new commits, the pull simply fast-forwards. If both sides have new commits, you get a merge, and possibly a conflict.

This explains a message every Git user meets:

! [rejected]  main -> main (fetch first)
error: failed to push some refs
hint: Updates were rejected because the remote contains work that you do not have locally.

Someone pushed to main after your last pull. Git refuses to overwrite their commits. The fix is to git pull, resolve any conflicts, then git push again. The wrong fix is git push --force, which replaces the remote branch with yours and deletes their work from it.

Step 14 of 16Compare

fetch vs. pull

Aspectgit fetchgit pull
Downloads new commitsYesYes
Updates origin/main and other tracking branchesYesYes
Changes your current branch and filesNoYes, merges or rebases
Can cause merge conflictsNoYes
Best whenYou want to look before combiningYou are ready to update your branch now

Step 15 of 16Practice · What would you do?

What would you do?

You try to push your commits on main and get "Updates were rejected because the remote contains work that you do not have locally." What should you do?

Step 16 of 16Practice · Choose one

Which statement about git fetch is true?