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.
First-time setup
Section titled “First-time setup”git config --global user.name "Your Name"git config --global user.email "you@example.com"git config --global init.defaultBranch maingit 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?The daily loop
Section titled “The daily loop”git status # what's changed. Run this constantly. It's free.git diff # unstaged changesgit add file.txt # stage a filegit 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 pushgit 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.
Commit messages
Section titled “Commit messages”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.
Looking at history
Section titled “Looking at history”git log --oneline --graph --all # the whole picturegit log -p path/to/file # every change to one filegit 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 fullBranches
Section titled “Branches”git switch -c fix/upstream-port # create and switch to a new branchgit switch main # switch backgit branch # list local branchesgit branch -d fix/upstream-port # delete (refuses if not merged, which is good)git merge fix/upstream-port # merge it into the current branchgit 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.
Remotes
Section titled “Remotes”git clone git@github.com:you/repo.gitgit remote -v # where push/pull gogit fetch # download changes, don't touch your filesgit pull # fetch + integrate into your branchgit push -u origin fix/upstream-port # first push of a new branchUse SSH remotes (git@github.com:...) with your SSH key and you’ll never type a password again.
.gitignore
Section titled “.gitignore”.env*.lognode_modules/dist/.DS_StoreConfigs in git: the sysadmin use case
Section titled “Configs in git: the sysadmin use case”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.