Skip to content

Git Basics

Git is a time machine with a user interface designed by someone who hated time travelers. It’s also the reason your config files have a history instead of nginx.conf.bak.old.REAL. Learn the twenty commands below and you’ll be fine.

Terminal window
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
git config --global pull.rebase true # cleaner history on pull. Fight me.
git config --global core.editor nano # unless you know how to exit vim. Do you?
Terminal window
git status # what's changed. Run this constantly. It's free.
git diff # unstaged changes
git add file.txt # stage a file
git add -p # stage in chunks, reviewing each one. Use this. It catches junk.
git diff --staged # what you're ABOUT to commit. Look at it.
git commit -m "Fix nginx upstream port for app"
git push

git add . is how debug prints, .env files, and 400MB log files end up in history. git add -p makes you look at what you’re committing, and that’s the whole point.

Future you, at 3 AM, reading git log, wants to know why. “fix” and “stuff” and “asdf” tell them nothing.

Raise client_max_body_size to 50m for uploads
Users hit 413 errors uploading phone photos. Default is 1m.

Short summary line (under ~70 chars), blank line, then the why if it isn’t obvious.

Terminal window
git log --oneline --graph --all # the whole picture
git log -p path/to/file # every change to one file
git log -S "proxy_pass" # commits that added/removed this string. Detective mode.
git blame file # who touched each line. It was you. It's always you.
git show abc1234 # one commit in full
Terminal window
git switch -c fix/upstream-port # create and switch to a new branch
git switch main # switch back
git branch # list local branches
git branch -d fix/upstream-port # delete (refuses if not merged, which is good)
git merge fix/upstream-port # merge it into the current branch

git switch and git restore replaced the overloaded git checkout, which did about six unrelated things. You’ll still see checkout everywhere. It still works. It’s still confusing.

Terminal window
git clone git@github.com:you/repo.git
git remote -v # where push/pull go
git fetch # download changes, don't touch your files
git pull # fetch + integrate into your branch
git push -u origin fix/upstream-port # first push of a new branch

Use SSH remotes (git@github.com:...) with your SSH key and you’ll never type a password again.

.env
*.log
node_modules/
dist/
.DS_Store

Put /etc under version control with etckeeper (sudo apt install etckeeper). It auto-commits before and after every package install, so “what changed on this box last Tuesday” becomes a git log instead of a guessing game. It’s quietly saved me more times than any monitoring tool.

Recovering from Mistakes, because you will make them.