Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Version Control with Git  ›  Lesson

Remotes — GitHub and GitLab

GitHub 14 minA remote is another copy of the repository, often hosted on GitHub or GitLab, kept in step with clone, push, fetch and pull.

Remotes — GitHub and GitLab

So far the bakery's history lives only on your laptop. If the laptop dies, the history dies with it, and your friend who helps with the CSS cannot see any of it. A remote is a copy of the repository on another computer, and services like GitHub and GitLab host those copies for teams and individuals.

The rule

Your laptop and the remote are two copies; push and pull keep them in step.

Git is distributed: every clone holds the full history. A hosting service is simply one copy that everyone agrees to share. GitHub and GitLab both host Git repositories and add features around them: a web view of the code, issue tracking, code review, and automated checks. GitLab can also be installed on a company's own servers.

Connecting a remote. Create an empty repository on the service, then:

git remote add origin https://github.com/crumb-bakery/site.git
git push -u origin main
  • origin is the conventional nickname for the main remote.
  • push uploads your new commits. -u remembers that your main goes with origin's main, so later you can just type git push.

Joining an existing project. Your friend copies the whole repository, history included:

git clone https://github.com/crumb-bakery/site.git

Cloning sets up origin automatically.

Getting other people's work.

  • git fetch downloads new commits from the remote but does not touch your files or your branches. It updates origin/main, Git's local record of where the remote's main is.
  • git pull is a fetch followed by combining the remote's changes into your current branch (a merge by default, or a rebase if configured).

When a push is refused. If someone else pushed first, the remote has commits you do not. Git refuses your push rather than lose their work. Pull first, resolve any conflicts, then push again.

Hosting services also handle who may do what. A repository can be public or private, and collaborators get read or write access. Signing in from the command line usually uses an SSH key or a personal access token rather than your account password.

The shortcut

Picture two notebooks, yours and the shared one. push copies your new pages into the shared notebook. fetch looks at the shared one's new pages without copying them into yours. pull looks and copies. If the shared notebook has pages you have not seen, you must read them before adding yours.

A Monday morning with a teammateStep 1 of 4
  1. You start

    git pull brings in your friend's Friday commits before you begin.

Drills

Answer fast — trust the rule.

quick drill

What does git clone <url> give you?

quick drill

What is the difference between git fetch and git pull?

quick drill

Your push is rejected because the remote has commits you do not have. What should you do?

quick drill

What is origin?

Remember

  • Your laptop and the remote are two copies; push and pull keep them in step.
  • Two notebooks: push copies yours up, fetch looks, pull looks and copies.
  • Trap: a rejected push means pull first, never force over a teammate's work.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainA rejected push

Your teammate and you both work on the bakery site hosted on GitHub. Your git push is rejected. Explain what a remote is, why Git refused, the difference between fetch and pull, and what you should do next.