← Git & GitHub from Zero
GitHub and remotes: clone, remote, push, fetch and pull

Lesson 4 of 7

GitHub and remotes: clone, remote, push, fetch and pull

Connect your project to GitHub and move commits between your computer and GitHub with git remote add, push, clone, fetch and pull.

The idea in plain words

Think of your project as a notebook on your desk and GitHub as a shared library. A remote is the library address written inside your notebook, and origin is its usual nickname. A push sends your new pages to the library.

Cloning is photocopying a whole book from the library onto your desk. A fetch checks the library for new pages without copying them in yet, and a pull checks and then copies the new pages into your notebook.

Commands in this lesson

git remote add origin <url>
Tell Git the address of your GitHub repository. Origin is the usual nickname.
git push -u origin main
Upload your commits. The -u remembers the link so later a plain git push is enough.
git clone <url>
Download a complete copy of a repository, with its history, and set up origin.
git fetch
Download new commits from GitHub without changing your files.
git pull
Fetch, then merge the new commits into your current branch.

Written notes from the video

Why GitHub

So far, everything lives on your own computer. If it breaks, your work is gone. This is Git and GitHub from zero, episode four: GitHub and remotes. Five commands to back up your project, and share it.

The library analogy

Think of your project as a notebook on your desk, and GitHub as a shared library. A remote is the library's address, written inside your notebook. Origin is its usual nickname. A push sends your new pages to the library. Cloning is photocopying a whole book from the library onto your desk. A fetch checks the library for new pages, without copying them in yet. A pull checks, and copies the new pages into your notebook.

What is GitHub?

First, what is GitHub? Git runs on your computer. GitHub is a website that stores Git projects online. That gives you a backup, a way to share your code, and a place to work together. Create a free account on github.com, then create a new, empty repository. Give it a name, such as my-site.

Commands 1 and 2: git remote add and git push

Commands one and two connect your project to GitHub. git remote add origin, followed by the address of your repository, tells Git where to send things. git remote -v checks the address you saved. Now git push -u origin main uploads your commits for the first time. The -u remembers the link, so from now on, plain git push is enough.

Command 3: git clone

Command three: git clone. Clone downloads a complete copy of a GitHub project, with its entire history. Use it on a new computer, or to work on someone else's project. It also sets up the remote called origin for you, so there is no need for git remote add.

Command 4: git fetch

Command four: git fetch. A teammate pushed a new commit to GitHub. Your computer does not know yet. git fetch downloads the news, but leaves your files alone. It is safe. You can look at what arrived before deciding what to do. git status now tells you that your branch is behind origin slash main.

Command 5: git pull

Command five: git pull. Pull is a fetch, followed by a merge. It brings the new commits into your branch. Run it at the start of each work session, so you begin with the latest version. And run it before you push, to avoid surprises.

Recap

Recap. git remote add origin connects your project to GitHub. git push uploads your commits. git clone downloads a project. git fetch checks for news. And git pull brings the news in.

Next episode

Next episode: teamwork. You will learn pull requests, and how to fix merge conflicts. Subscribe so you do not miss it, and find written notes for every lesson at changecv.com/skills.

Common questions

Is Git the same as GitHub?

No. Git is the version control tool that runs on your computer. GitHub is a website that hosts Git repositories online. Other sites, such as GitLab and Bitbucket, do the same job.

What does origin mean?

It is just the default name for a remote. Git uses it for the repository you cloned from or the first remote you add. You can add other remotes with different names.

What is the difference between git fetch and git pull?

git fetch downloads new commits and leaves your files alone, so you can look first. git pull does a fetch and then merges the new commits into your current branch.

Why does git push fail to sign me in?

GitHub does not accept your account password for Git over HTTPS. Sign in with a personal access token, or set up an SSH key, which episode 7 covers.

What does the -u flag do in git push -u origin main?

It links your local branch to the branch on GitHub. After that, git push and git pull work on that branch without extra arguments.