paskvilwiki

up: programming

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 push fails 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-credentials on the next pull/push, and reuses it:
    git config --global credential.helper store
    
    • it's stored as plain text - credential.helper cache keeps 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

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 into develop - see typical flow below
  • staging - the release candidate, under biz review
    • when the sprint's work is done, merge develop into staging - only once the previous release is live, or half-done work ends up in the review
    • development for the next sprint carries on on develop meanwhile
    • fixes from the review go on a branch off staging, merged into staging, then staging is merged back into develop
  • production - what's live
    • when the review is done, merge staging into production, 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 into production, then merged back into staging, and staging into develop
feature and fix branches merge into develop, hotfixes into production; develop goes to staging as a release candidate, staging to production as a release hotfix hotfix feature fix feature feature fix production staging develop v1.3.1 v1.4.0 v1.4.1 v1.5.0 candidate release candidate release
merge-back flows are left out for clarity, and feature/fix branches can overlap - work on develop isn't sequential

two 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-setup
    • fix/PRJ-010-ts-check-failing
    • hotfix/PRJ-213-patch-vulnerability
  • commit messages - start with the ticket id, e.g. [PRJ-001] initial setup - vite, ts, ...
    • a commit-msg hook can enforce it, or a prepare-commit-msg hook can add it from the branch name...

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 difftool now brings up your dear ol' meld - git difftool -d shows all changed files in one window
  • git mergetool opens meld with the common ancestor in the middle, non-conflicting changes already merged in, so you only resolve the actual conflicts