git
last updated 2026-11
basic commands
you edit the working copy, add changes to the index (staging), commit them locally, then push to the remote
git clone user@host:/path/to/repo # get a working copy of a repo
git checkout -b <branch> # create a branch and switch to it
git status # current branch, changed and staged files
git mv <old> <new> # rename or move a file
git rm <file> # delete a file
git add <file> # stage changes
git restore --staged <file> # unstage, keep the changes
git restore <file> # drop unstaged changes to the file
git commit -m 'message' # commit the staged changes
git pull # fetch and merge the remote changes
git push [-u origin <branch>] # push; -u the first time, to set the upstream
git merge <branch> # merge another branch into the current one
git diff [<from> <to>] # unstaged changes, or between branches/commits
git difftool [<from> <to>] # the same in an external tool, see meld below
tips and tricks
- a new branch's first
git pushfails with "has no upstream branch" - let git set the upstream on the first push:git config --global push.autoSetupRemote true - skip retyping your password - git saves it to
~/.git-credentialson the nextpull/push, and reuses it:git config --global credential.helper store- it's stored as plain text -
credential.helper cachekeeps it in memory only (15 min by default), or use SSH keys - GitHub and GitLab want a personal access token here, not your account password
- it's stored as plain text -
branching model for agile
my preferred model follows the usual environments - one long-lived branch for each:
develop- ongoing development- new work goes on a branch off
develop, merged back intodevelop- see typical flow below
- new work goes on a branch off
staging- the release candidate, under biz review- when the sprint's work is done, merge
developintostaging- only once the previous release is live, or half-done work ends up in the review - development for the next sprint carries on on
developmeanwhile - fixes from the review go on a branch off
staging, merged intostaging, thenstagingis merged back intodevelop
- when the sprint's work is done, merge
production- what's live- when the review is done, merge
stagingintoproduction, release, and tag it (git tag v1.4.0) - so you always know what was live when - hotfixes go on a branch off
production, merged intoproduction, then merged back intostaging, andstagingintodevelop
- when the review is done, merge
feature/fix branches can overlap - work on develop isn't sequentialtwo rules keep it working:
- code only moves towards production (
develop->staging->production), fixes made further along are merged back, so nothing gets lost - plain merges between these three branches, no squash or rebase - otherwise every back-merge conflicts over the same changes again
naming
- branches called
<task type>/<ticket id>-<short description>, e.g.:feature/PRJ-001-initial-setupfix/PRJ-010-ts-check-failinghotfix/PRJ-213-patch-vulnerability
- commit messages - start with the ticket id, e.g.
[PRJ-001] initial setup - vite, ts, ...- a
commit-msghook can enforce it, or aprepare-commit-msghook can add it from the branch name...
- a
typical flow
# only once, when starting
git clone <repo> # get a local copy of the repo
# developing a feature or fix
git checkout develop # start from `develop`
git pull # make sure it's fresh
git checkout -b <branch> # start a new branch
# do your changes...
git add <files> # register changes for commit
git commit -m 'message'
git push # uses the `autoSetupRemote` config
# repeat as needed...
# once done with the branch, all changes pushed, make sure you are
# up to date with `develop` before handing the branch over for testing
git fetch
git merge origin/develop # merge in the latest `develop`
# conflicts? `git mergetool` (or edit), `git add`, `git commit` - or `git merge --abort`
git push
then open a pull/merge request into develop on your git host - it handles review and the merge…
note - once the PR/MR is merged, the host typically deletes the remote branch, but your local one stays - clean up all such branches with:
git checkout develop && git pull -p && git branch -v | awk '$3 == "[gone]" {print $1}' | xargs -r git branch -D
-D, not -d - hosts often squash-merge, and -d refuses then; but it also drops local commits you never pushed
small tips
ignoring files and folders
list what git should leave alone in .gitignore in the repo root, one pattern per line, and commit it with the rest:
# comments go on their own line - a `#` after a pattern is part of the pattern
# a trailing `/` matches folders only
node_modules/
dist/
*.log
.env
- files already in the repo stay tracked -
git rm --cached <file>stops tracking one, and keeps it on disk - your own editor and OS junk (
.idea/,.DS_Store) belongs in the global~/.config/git/ignore, not in every repo's.gitignore
set meld as your difftool and mergetool
git config --global diff.tool meld
git config --global difftool.prompt false
git config --global merge.tool meld
git config --global mergetool.prompt false
git config --global mergetool.keepBackup false # no `*.orig` files left behind
git config --global mergetool.meld.useAutoMerge true
git difftoolnow brings up your dear ol' meld -git difftool -dshows all changed files in one windowgit mergetoolopens meld with the common ancestor in the middle, non-conflicting changes already merged in, so you only resolve the actual conflicts