basforge

Search titles, text and the “If it doesn’t work” cases.

    RU
    Cheat sheetDevelopmentGit

    Everyday git: status, commits, branches, syncing

    The commands you need every day, grouped. Undoing and rescuing lost work is separate — Undo in git; finding where it broke — Find where it broke.

    What’s going on

    Command What it does
    git status -sb changes in short form, and how far behind origin you are
    git diff edits not yet added to the index
    git diff --staged what goes into the next commit
    git diff main..traefik-timeouts --stat how a branch differs from main, by file
    git log --oneline -10 the last 10 commits, one line each
    git log --oneline --graph --all -20 a graph of every branch
    git show HEAD what’s in the last commit
    git log -p --follow -- traefik/data/traefik.yml one file’s history with its edits, including before a rename

    The first line of git status -sb is ## main...origin/main [ahead 1, behind 2]: one of your commits isn’t pushed, and two from GitHub haven’t been pulled.

    Committing

    Command What it does
    git add traefik.yml a file into the index
    git add -p into the index piece by piece, with a prompt for each
    git add -A every change, including deleted and new files
    git commit -m "traefik: readTimeout 300s — uploads were cut off" a commit with a message
    git commit -v the editor shows the whole diff while you write the message
    git commit -am "…" add changed files and commit; new files won’t be included

    The message is “where: what was done — why”. The first line stays under 72 characters: log --oneline and GitHub show it. “fix”, “changes”, “wip” will say nothing a month from now.

    Branches

    Command What it does
    git branch local branches; * marks the current one
    git branch -a plus the branches on origin
    git switch main switch to a branch
    git switch -c fix-crowdsec-hooks create a branch and switch to it
    git switch - back to the previous one
    git branch -m new-name rename the current one
    git branch -d fix-crowdsec-hooks delete a merged one; -D deletes any
    git push origin --delete fix-crowdsec-hooks delete a branch on GitHub

    Syncing

    Command What it does
    git fetch get what’s new from origin, changing nothing locally
    git log --oneline HEAD..origin/main what will arrive with pull
    git pull fetch and integrate; with pull.rebase = true, no merge commit (~/.gitconfig block by block)
    git push send your commits
    git push -u origin fix-crowdsec-hooks the first push of a branch; with push.autoSetupRemote, plain git push is enough
    git remote -v where origin points
    git remote set-url origin [email protected]:example-user/notes.git change the URL, say from HTTPS to SSH

    Putting work aside

    Command What it does
    git stash -u put edits and new files aside
    git stash list what’s been put aside
    git stash show -p stash@{0} what’s inside
    git stash pop bring back the latest and remove it from the list
    git stash drop stash@{1} throw away one you don’t need

    Tags and versions

    Command What it does
    git tag -a v1.2.0 -m "LinkDeck 1.2.0" an annotated tag with a date on the current commit
    git push origin v1.2.0 send the tag; push without a name doesn’t send tags
    git tag -l 'v1.*' tags by pattern
    git describe --tags the nearest tag and how many commits since: v1.2.0-3-g9f8e7d

    Help

    Command What it does
    git help reset the full manual for a command
    git reset -h briefly — the flags
    git config --show-origin --get pull.rebase a setting’s value and where it comes from (~/.gitconfig block by block)