Coding Hub  /  Tools

git & github

Save versions of your work, and get it onto GitHub where I can see it. Entries 4 and 5 are the loop you will run dozens of times a term.

The idea

Two different things with similar names.

01 Git is not GitHub

Git is a program on your computer that records snapshots of a folder. It works with no internet and no account.

GitHub is a website that stores copies of Git projects so you can share them, hand them in, and get them back if your laptop dies.

You can use Git without GitHub. You cannot sensibly use GitHub without Git.

the vocabulary
WordMeans
repository, repoA folder Git is tracking
commitOne saved snapshot, with a message saying what changed
staging areaThe list of changes that will go into the next commit
pushSend your commits up to GitHub
pullBring commits down from GitHub
cloneDownload a repo for the first time
remoteThe GitHub copy. Usually nicknamed origin.

02 Set your name once

Every commit records who made it. Set this once per computer, before your first commit, or your history is attributed to nobody.

Use the same email as your GitHub account and your commits will link to your profile.

Terminal
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Check it took
git config --global --list

# Make 'main' the default branch name for new repos
git config --global init.defaultBranch main

The everyday loop

status, add, commit, push. In that order, every time.

03 Starting a repository

Two ways in. Either you have a folder and want to track it, or the project is already on GitHub and you want a copy.

Create the .gitignore before your first commit. Committing node_modules once leaves it in the history even after you delete it.

Terminal
# A: a folder you already have
cd my-project
git init

# B: a project already on GitHub
git clone https://github.com/someone/their-project.git
cd their-project

04 status, add, commit

git status is the command to run constantly. It tells you what has changed, what is staged, and usually what to type next.

Staging is the step people find odd at first. It lets you choose which changes go into a commit, so one commit can be one idea even when you have edited five files.

Terminal
git status                  # what has changed? run this often

git add index.html          # stage one file
git add css/               # stage a folder
git add .                   # stage everything changed

git commit -m "Add the plant card layout"

git log --oneline           # the history, one line per commit
git diff                    # what changed but is not staged yet

05 Writing a commit message

A message is read by someone trying to find when something broke — usually you, in three weeks. update and stuff tell that person nothing.

Write what the commit does, in the present tense, in under about fifty characters. A good test: the message should finish the sentence “Applying this commit will…”

messages
GoodUseless
Add watering form to the plant pageupdate
Fix total not resetting between ordersfixed bug
Move styles out of index.htmlchanges
Add .gitignore for node_modulesasdf

06 Pushing to GitHub

Make an empty repository on GitHub — no README, no .gitignore, or you will have to merge before your first push. Then connect your folder to it and push.

After the first time, git push on its own is enough.

Terminal
# Once, to connect the folder to the GitHub repo
git remote add origin https://github.com/you/my-project.git
git branch -M main
git push -u origin main

# Every time after that
git push

# Check what it is connected to
git remote -v

Keeping it clean

Two files and one habit.

07 .gitignore

Some files should never be committed: installed packages, secrets, and whatever your operating system scatters about. A .gitignore file at the top of the repo lists them, one pattern per line.

.gitignore only affects files Git is not already tracking. If you committed something by mistake, add it to the file and then run git rm -r --cached <name> before committing again.

.gitignore
# Installed packages - rebuilt with npm install
node_modules/

# Secrets. Commit .env.example instead.
.env

# Python
__pycache__/
*.pyc
venv/

# Operating system and editor
.DS_Store
Thumbs.db
.vscode/

08 README.md

The first thing anyone sees on GitHub, and the first thing I read when marking. It should answer three questions: what is this, how do I run it, and what can it do.

It is written in Markdown — # for headings, - for bullets, backticks for code.

README.md
# Plant Tracker

A web app for tracking when houseplants need watering.
Built for Period 3, unit 4.

## Running it

1. `npm install`
2. Copy `.env.example` to `.env` and fill in the values
3. `npm run dev`
4. Open http://localhost:3000

## What it does

- Add a plant with a watering interval
- See which plants are overdue
- Mark a plant as watered

09 Commit often, in small pieces

A commit per feature beats one enormous commit at the end. Small commits are easier to describe, and when something breaks you can see exactly which change did it.

A reasonable rhythm: commit whenever you have something that works, even if it is small. “It runs and does one more thing than before” is a perfectly good reason to commit.

a healthy history
$ git log --oneline
a3f21c9 Add overdue badge to plant cards
8e4b012 Store last watered date on each plant
1d93f7a Add the plant list layout
4c0a88e Add .gitignore
9fe1234 Initial commit

When it goes wrong

Undoing things, carefully.

10 Undoing

Git can undo nearly anything, but the commands differ by how far along the change is. Find your situation in the table and use that line only.

git reset --hard throws away uncommitted work permanently. Do not reach for it because a forum post said so — be sure you know what you are discarding.

undoing
SituationCommand
Changed a file, want the last committed version backgit restore file.html
Staged something by mistakegit restore --staged file.html
Commit message has a typo (not pushed yet)git commit --amend -m "Better message"
Want to undo the last commit, keep the changesgit reset --soft HEAD~1
Want to see an old versiongit log --oneline then git show <hash>
Everything is a mess and nothing is pushedCopy your folder elsewhere first, then experiment

11 Errors you will meet

Git's messages are unusually good: most of them suggest the fix on the next line.

message to cause
What you seeMeans
not a git repositoryYou are not inside a repo. cd into it, or git init.
nothing to commit, working tree cleanNothing changed since the last commit. Often an unsaved file.
Updates were rejectedGitHub has commits you do not. git pull first, then push.
Please tell me who you areRun the two git config --global lines in entry 2.
Authentication failedGitHub stopped accepting passwords. Use a personal access token, or set up SSH.
fatal: remote origin already existsAlready connected. git remote set-url origin <url> to change it.
A text editor opened and you are stuckIt wants a commit message. Write one, then Esc :wq Enter in vim.

12 The whole loop on one card

Print this, or learn it. It is most of what you need.

Terminal
# Once per project
git init
git remote add origin https://github.com/you/project.git

# Every working session
git status
git add .
git commit -m "What this change does"
git push

# Starting on another machine
git clone https://github.com/you/project.git

# Catching up before you start work
git pull