Phase 4GitHub

#13 GitHub and remotes

remote, push, pull, clone

What is GitHub?

GitHub is an online platform that hosts Git repositories. It allows you to store your code in the cloud, share it with other developers, and collaborate on projects.

So far, you have been working with Git locally, on your machine. With GitHub, you can push your code to a remote server and access it from anywhere.

What is a remote?

A remote (or remote repository) is a version of your project hosted on a server, such as GitHub. It serves as a central point for synchronizing work between multiple machines or multiple developers.

By convention, the main remote is called origin. That's the name automatically given when you clone a repository or manually add a remote.

git remote -- Manage remote repositories

To link your local repository to a GitHub repository, use git remote add:

Add a remote
$ git remote add origin https://github.com/alice/my-project.git

To check the configured remotes:

List remotes
$ git remote -v
origin	https://github.com/alice/my-project.git (fetch)
origin	https://github.com/alice/my-project.git (push)

git push -- Send your commits

The git push command sends your local commits to the remote repository. The first time, use -u to link your local branch to the remote branch:

First push
$ git push -u origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
To https://github.com/alice/my-project.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.

Once the link is established, a simple git push is enough for subsequent pushes:

Subsequent push
$ git push
Everything up-to-date

git pull -- Retrieve changes

The git pull command downloads changes from the remote repository and merges them into your local branch. It's the opposite of git push:

Retrieve changes
$ git pull
Already up to date.

Use git pull regularly to stay up to date with other contributors' changes.

git clone -- Copy a repository

To retrieve an existing project from GitHub, use git clone. This command downloads the entire repository (code + history) into a new folder:

Clone a repository
$ git clone https://github.com/alice/my-project.git
Cloning into 'my-project'...
remote: Enumerating objects: 25, done.
remote: Total 25 (delta 0), reused 25 (delta 0)
Receiving objects: 100% (25/25), done.

The origin remote is automatically configured to point to the original URL.

Summary

Overview
git remote add origin URL   # Add a remote repository
git remote -v               # List remotes
git push -u origin main     # First push
git push                    # Subsequent pushes
git pull                    # Retrieve changes
git clone URL               # Copy an existing repository

Your turn

Try out the commands for working with remotes in the terminal below. Start with git remote -v to see the configured repositories.

terminal — bash
user@stemlegacy:~/my-project$