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 mainoriginis the conventional nickname for the main remote.pushuploads your new commits.-uremembers that yourmaingoes withorigin'smain, so later you can just typegit push.
Joining an existing project. Your friend copies the whole repository, history included:
git clone https://github.com/crumb-bakery/site.gitCloning sets up origin automatically.
Getting other people's work.
git fetchdownloads new commits from the remote but does not touch your files or your branches. It updatesorigin/main, Git's local record of where the remote'smainis.git pullis 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.
You start
git pullbrings in your friend's Friday commits before you begin.
Drills
Answer fast — trust the rule.
What does git clone <url> give you?
What is the difference between git fetch and git pull?
Your push is rejected because the remote has commits you do not have. What should you do?
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.
