Some useful Git commands
As developers we need to store our code somewhere, and Git remains the version control system. This is an updated pass over my old command reference - same essentials, plus the newer commands that have replaced some old habits.
git config
Sets configuration globally or per-project. At its most basic, the name and email attached to your commits:
git config --global user.name "[name]"
git config --global user.email "[email address]"
Worth knowing: you can set a different email per repo by dropping the --global flag inside the project - handy for separating work and personal commits.
git init / git clone
git init starts a repository from scratch; git clone https://repourl.com/repo_name.git grabs an existing one. Both generally one-time operations.
git add
Stages changes for the next commit. git add . for everything, git add [file] for one file, and the underrated git add -p to stage hunk by hunk - the single best way to keep commits small and honest.
git commit
git commit -m "message" for the quick inline version, plain git commit to open your editor. git commit --amend fixes up the last commit when you inevitably forget a file (only amend commits you haven’t pushed).
git status / git log
git status shows the state of your working tree. For history, my daily driver is:
git log --oneline --decorate --graph
git switch and git restore
The modern replacements for git checkout, which used to do two unrelated jobs. Switching branches:
git switch main
git switch -c new-feature # create and switch
Throwing away changes to a file:
git restore file.rb
git restore --staged file.rb # unstage
Same results as the old checkout incantations, far harder to fat-finger.
git branch
git branch lists local branches, -a includes remotes, -d deletes safely (refusing if unmerged), -D deletes with prejudice, -m renames.
git merge
Combines branch histories:
git switch main
git merge new-feature
git branch -d new-feature
git stash
The command I use ten times a day and somehow left out of the original post. git stash shelves your uncommitted changes, git stash pop brings them back - perfect for “hang on, I need to be on the other branch”.
git remote / push / pull
git remote -v lists your remotes with URLs. git push uploads your branch (--tags to include tags; treat --force as radioactive). git pull fetches and merges in one go.
That’s the working set. There’s plenty more, but these cover 95% of a normal day.