GitHub

What you are looking at

Git works on one computer. GitHub is a website that keeps a copy of a repository on its servers, so that more than one computer can share it. At the left is your folder and, beside it, your chain of commits. At the back is the server with the shared chain. Each cylinder is one commit, newest at the right. A teammate pushes to the server too; their commits have ids starting with t.

On your computer that server copy has a name, origin. Nothing moves on its own. A commit stays where it was made until someone runs git push to send it up, or git pull to bring new commits down. This page is about those two moves, and what happens when two people push to the same place.

Getting a repository

To work on a repository that already exists on GitHub, copy its address from the green Code button and clone it. This makes a folder with the whole history inside.

git clone https://github.com/you/notes.git
cd notes

To put a project that is already on your computer onto GitHub, make an empty repository on the website, then tell Git where it is and push.

git remote add origin https://github.com/you/notes.git
git push -u origin main

The first push asks you to sign in. GitHub does not accept your account password on the command line. Use a personal access token from Settings, or install GitHub CLI and run gh auth login, and your computer remembers it.

Push and pull

CommandWhat it does
git pushSend your new commits to origin.
git fetchDownload new commits from origin, without touching your files.
git pullFetch, then merge the new commits into your copy.
git statusAlso says how far ahead or behind origin you are.

A push is refused when origin has commits you do not have. Git will not drop someone else's work. Pull first, then push again. If you and the other person changed the same lines, the pull stops with a conflict: Git marks both versions in the file between <<<<<<< and >>>>>>>, you edit the file to the version you want, then git add and git commit.

Branches and pull requests

Pushing straight to main works for one person. In a team, work goes on a branch, a separate line of commits with its own name, and main changes only when the branch is merged.

git switch -c fix-dates
git commit -am "Fix the dates"
git push -u origin fix-dates

On GitHub, open a pull request from the branch. It is a page that shows every change on the branch and lets others comment. When it is approved, press Merge and the commits land on main. Then everyone pulls main, and the branch can be deleted.

What GitHub shows

A public repository can be read by anyone on the internet, including the whole history. A private one is seen only by the people you add. Either way, never commit a password or a key. Deleting it in a later commit does not remove it from the history.

Check your understanding

  1. You commit twice and close the laptop. Does GitHub have the commits?
  2. A teammate pushed while you were working. What happens when you push, and what do you do?
  3. What is the difference between git fetch and git pull?
  4. Why does a team use pull requests instead of pushing to main?

Summary

Takeaways