tips
144 tips Git
One command, setting or habit at a time about Git: what it does, the line to copy and a caveat or two.
History and debugging 48
-
List commits together with a per-file summary of what changed
git log --stat -5 -
Opens your visual diff tool once with two folders to compare, instead of once per changed file.
git difftool --dir-diff -
Lists every file in a commit or branch as plain paths, without touching your working folder.
git ls-tree -r --name-only main -
Searches all versions of all files in history, not just the current checkout.
git grep -n 'API_KEY' $(git rev-list --all) -
Leaves out indentation and spacing changes so you can read the real edits.
git diff -w -
Shows exactly which commits a merge would bring in from your branch.
git log --oneline main..feature -
Builds your own log line with placeholders for hash, author, date and subject.
git log --format='%h %an %ar %s' -10 -
Prints a single number of commits, useful for build numbers and quick checks.
git rev-list --count HEAD -
Gives you git's colored diff for any two files or folders, even outside a repository.
git diff --no-index old.conf new.conf -
Lists every tag and branch that includes a given commit.
git tag --contains a1b2c3d -
Hides vendored or generated folders from log and diff with a pathspec.
git diff main -- . ':(exclude)vendor' -
Prints the root commit of a repository, without scrolling to the end of the log.
git rev-list --max-parents=0 HEAD -
Lists only your own commits from a chosen period, ready to read out in a standup.
git log --since="1 week ago" --author="$(git config user.email)" --oneline -
Compares your branch with the point where it left main, so other people's work stays out.
git diff main...feature -
Prints the raw content of a commit, tree or blob so you see git's own data.
git cat-file -p HEAD -
Gives a percentage per folder, so you see where a big change really happened.
git diff --dirstat main...HEAD -
Uses the reflog to compare a branch with its state at an earlier time on your machine.
git diff main@{yesterday} main -
Compares commits by their patch, so cherry-picked copies count as already merged.
git cherry -v main -
Draws every branch and merge as a small text graph so you see how they relate.
git log --graph --oneline --all -
Uses bisect to find the commit that introduced any change, even a good one.
git bisect new HEAD -
Recovers a dropped or cleared stash by looking for dangling commits in the object store.
git fsck --no-reflog | awk '/dangling commit/ {print $3}' -
Maps old names and email addresses to one identity in shortlog and log output.
Alice <[email protected]> <[email protected]> -
Finds commits by words in their message instead of by the code they changed.
git log --grep="login" -i --oneline -
Finds the merge that brought a specific commit into a branch.
git log --oneline --ancestry-path --merges abc1234..main -
Prints the common ancestor of two branches, handy for diffs, rebases and scripts.
git merge-base main feature -
Prints two numbers: commits only on one branch and commits only on the other.
git rev-list --left-right --count main...HEAD -
Prints a file as it was at any commit or tag, without touching your working tree.
git show v1.0:config/app.php -
Refers to a commit by words from its message instead of by its hash.
git show ':/fix login' -
Shows one entry per merge by following only the first parent through the history.
git log --first-parent --oneline main -
Lists only the commits that deleted files, so you can find when a file disappeared.
git log --diff-filter=D --summary -- path/to/old-file.php -
Finds commits whose added or removed lines match a regular expression.
git log -G'timeout\s*=' --oneline -p -- config/ -
Attributes lines to the commit that wrote them, ignoring whitespace changes and code moved between files.
git blame -w -C -C -L 40,60 src/config.py -
Runs a test command on every commit of a branch during a rebase and stops at the first failure.
git rebase --exec "make test" origin/main -
Colors moved lines differently in a diff, so real edits stand out during a review.
git diff --color-moved -
Attaches extra text to an existing commit, shown in git log, without rewriting history.
git notes add -m 'Tested on Debian 13' 72a144e2 -
Searches only the files git tracks and names the function around every match.
git grep -n -p TODO -
Compares two versions of a branch commit by commit, for example before and after a rebase.
git range-diff @{u} @{1} @ -
Lists only the files that changed, either as plain names or with a status letter for added, modified or deleted.
git diff --name-only main...HEAD -
Gives a commit a readable name made of the closest tag and the number of commits since it.
git describe --tags -
Shows a commit message and a list of the changed files with line counts, without the full diff.
git show --stat -
Lists tags together with the first line of their annotation, so the list explains itself.
git tag -n -
You spot a typo in a commit from ten minutes ago, and the lazy move is a new commit called “oops” or “fix”. Do that a few times and your…
git commit --fixup a1b2c3d -
Shows only the commits that changed one function or line range in a file.
git log -L :parseConfig:src/config.js -
Shows changes word by word instead of line by line.
git diff --word-diff -
Makes git remember how you resolved a merge conflict and reuse it next time.
git config --global rerere.enabled true -
Finds the commit that introduced a bug by running your test script on commits automatically.
git bisect run ./test.sh -
Makes git blame skip commits like mass reformatting so it shows who really wrote a line.
0123456789abcdef0123456789abcdef01234567 -
Lists where your branch tips have been so you can bring back commits after a reset.
git reflog
Staging and cleanup 31
-
Prints only the new files Git doesn't track yet, skipping everything in .gitignore, as plain paths.
git ls-files --others --exclude-standard -
Prints the full diff of a stash, so you know what you are about to bring back.
git stash show -p stash@{1} -
Lists ignored files next to the usual status, so you see what git is hiding.
git status --ignored -s -
Adds trailer lines like Co-authored-by to a commit message in the format tools expect.
git commit -m "Add export button" \ -
Deletes build output and other ignored files while leaving your untracked work alone.
git clean -n -d -X -
Rewrites the author of your last commit with your current name and email.
git commit --amend --no-edit --reset-author -
Discards selected hunks from your working tree and keeps the rest of your edits.
git restore -p src/app.js -
Registers a new file without staging its content, so diffs and add -p see it.
git add -N src/export.py -
Compares your working tree with the last commit, staged or not.
git diff HEAD -
Hides unstaged edits so you can run tests on exactly what you are about to commit.
git stash push --keep-index -
Stages every change to files git already knows, without picking up untracked files.
git add -u -
Creates a branch at the commit where you stashed and applies the stash there.
git stash branch login-form stash@{0} -
Re-applies your line ending rules to files that are already in the repository.
git add .gitattributes -
Commits the current version of the files you name, while other staged work stays staged.
git commit -m "Fix typo in readme" README.md -
Puts changes to selected files aside and leaves your other edits in place.
git stash push -m "config experiment" -- src/config.js -
Removes a committed file from the repository while leaving your local copy in place.
git commit -m "Stop tracking .env" -
Removes leftover bookkeeping for worktree folders that no longer exist.
git worktree prune -n -v -
Tells git to treat a tracked file as unchanged so your local edits stop showing up.
git update-index --skip-worktree config/local.php -
Replaces one file in your working tree with its version from any other commit.
git restore --source=HEAD~3 -- src/settings.php -
Records a commit that changes no files, for example to restart a CI run.
git commit --allow-empty -m "Trigger CI" -
Undoes a merge commit by telling git which parent counts as the main line.
git revert -m 1 <merge-commit> -
Warns about trailing whitespace and leftover conflict markers in your changes.
git diff --check -
Shows which .gitignore file, line and pattern makes git ignore a file.
git check-ignore -v path/to/file -
Moves the branch back one commit while the changes stay staged, so you can recommit them differently.
git reset --soft HEAD~1 -
Prints the changes as short two-letter codes and adds one line with the branch and its upstream.
git status -sb -
Saves your unfinished changes under a name, so git stash list tells you what each stash holds.
git stash push -m "login form, half done" -
Shows which untracked files git clean would remove, without deleting anything.
git clean -n -d -
Shows the changes that are staged and will go into your next commit.
git diff --staged -
Lets you choose which parts of a file go into the next commit.
git add -p -
Takes a file out of the staging area while keeping your edits on disk.
git restore --staged notes.txt -
Stashes your changes together with new files that git does not track yet, and labels the stash.
git stash push -u -m "login form, half done"
Git settings and fixes 29
-
Cancels a merge that stopped on conflicts and puts the branch back the way it was before.
git merge --abort -
Makes a GitHub Actions workflow cancel its older run on the same branch when a new one starts.
concurrency: -
Prints the absolute path of the top folder of the current Git repository, from any subfolder.
git rev-parse --show-toplevel -
Lists every Git setting whose name matches a pattern, with its value.
git config --get-regexp '^user\.' -
Gets a capitalization change into git on a case insensitive file system like macOS.
git mv readme.md README.md -
Prints short git output straight to the terminal instead of in less.
git config --global pager.branch false -
Changes where origin points after a rename, a move or a switch to SSH.
git remote set-url origin [email protected]:owner/new-name.git -
Pushes annotated tags on the commits you push, so release tags never stay local.
git config --global push.followTags true -
Defines your own git subcommands for commands you type often.
git config --global alias.last "log -1 --stat" -
Stops git from printing names like café as octal escape codes.
git config --global core.quotePath false -
Makes git init start on main, or any name you like, without the hint every time.
git config --global init.defaultBranch main -
Reports how much disk space the repository's objects take, in readable units.
git count-objects -vH -
Sorts tags as versions, so v1.10 comes after v1.9 instead of before it.
git config --global tag.sort version:refname -
Signs commits with an SSH key instead of GPG, with three config lines.
git config --global gpg.format ssh -
Resolves a whole conflicted file by taking your version or the incoming one.
git checkout --theirs package-lock.json -
Shows the file behind every git setting, so you know where to change it.
git config --show-origin --list -
Jumps back to the commit you were on before the last merge, rebase or reset.
git reset --hard ORIG_HEAD -
Makes git pull refuse to create a merge commit unless it can simply fast-forward.
git config --global pull.ff only -
Prints git branch and similar lists in columns when the output goes to a terminal.
git config --global column.ui auto -
Ignores files like .DS_Store in every repository from one personal file.
git config --global core.excludesFile ~/.gitignore_global -
Lists local branches with the upstream branch each one tracks and the last commit.
git branch -vv -
Rewrites https GitHub URLs to ssh automatically for clone, fetch and submodules.
git config --global url."[email protected]:".insteadOf "https://github.com/" -
Puts GitHub's noreply address in your commits instead of your real email address.
git config --global user.email "[email protected]" -
Stashes uncommitted changes before a rebase and restores them afterwards.
git rebase --autostash origin/main -
Shows the staged diff in your editor while you write the commit message.
git commit -v -
Gives every repository under one folder its own git settings, like a work email address.
name = Your Name -
Checks out only chosen folders of a large Git repository, so the working tree stays small.
git sparse-checkout set docs tools/scripts -
Makes git ask for confirmation when it guesses what you meant after a typo in a command name.
git config --global help.autocorrect prompt -
Registers the repository for scheduled background upkeep like prefetching and commit-graph updates, so it stays fast.
git maintenance start
Branches 23
-
Shows in one table which commits are on which branch since they split.
git show-branch main feature -
Always creates a merge commit, so a finished feature branch stays visible as a group in the history.
git merge --no-ff feature -m "Merge feature" -
Applies all changes of a branch to your working tree without committing, so you can record them as one commit.
git merge --squash feature -
Saves a free-text description for a branch, so you remember later what it was meant to do.
git branch --edit-description [<branch-name>] -
Creates a branch for an issue on GitHub, linked to the issue, and can check it out locally.
gh issue develop 123 --checkout -
Answers yes or no through the exit code whether a commit is part of another commit's history.
git merge-base --is-ancestor main feat -
Tells a script by exit code whether a branch or tag with an exact name exists.
git show-ref --verify --quiet refs/heads/feature -
Replants only your own commits onto main when you branched from the wrong branch.
git rebase --onto main feature-a feature-b -
Deletes a branch on the remote after it has been merged.
git push origin --delete feature/login -
Checks out a colleague's branch from the remote with one short command.
git switch feature/export -
Fast-forwards your local main from the server while you stay on your feature branch.
git fetch origin main:main -
Connects an existing local branch to a remote branch without pushing anything.
git branch -u origin/main -
Checks out any tag or commit to test it, without creating or moving a branch.
git switch --detach v1.4.0 -
Renames a branch locally and on the server without losing its commits.
git branch -m old-name new-name -
Makes a copy of a branch that keeps its upstream and other branch config.
git branch -c feature feature-backup -
Creates an empty branch that shares no commits with the rest of the repository.
git switch --orphan gh-pages -
Rebases a branch and also moves the branches stacked underneath it.
git rebase --update-refs main -
Lists local branches already merged into main so you can delete them safely.
git branch --merged main -
Prints the name of the current branch and nothing else, which is handy in scripts and prompts.
git branch --show-current -
Jumps back to the previous Git branch without typing its name.
git switch - # back on your feature branch -
Checks out a second branch in its own folder without stashing or cloning.
git worktree add ../hotfix hotfix -
Forces a push only if nobody else has updated the remote branch since you last fetched.
git push --force-with-lease -
Jumps back to the branch you were on before your last branch switch.
git switch feature/login
Export and backup 4
-
Exports commits as patch files that keep author, date and message, and applies them elsewhere.
git format-patch origin/main..HEAD -o patches/ -
Saves uncommitted work as a patch file and checks that it applies cleanly elsewhere.
git apply --check wip.patch -
Packs one commit or tag of a repository into a tar.gz or zip file, without history.
git archive --format=tar.gz --prefix=project/ -o project.tar.gz HEAD -
Packs all branches and tags of a repo into one file for offline transfer or backup.
git bundle create backup.bundle --all
Git in Portainer 4
-
Explains when Portainer redeploys a Git stack and what Force redeployment adds.
Force redeployment: on -
Loads a second compose file from the same repository on top of the main one.
Compose path: docker-compose.yml -
Turns a Git-based Portainer stack into a normal editable stack, with a few files left behind.
-
Makes a Git-based Portainer stack pull fresh images whenever it redeploys.
Hooks 3
-
Bypasses the pre-commit and commit-msg hooks for one commit.
git commit --no-verify -m "WIP: half done export" -
Points git at a hooks folder you commit, so the whole team can share the same hooks.
git config core.hooksPath .githooks -
Feeds your branch, changed files and last commits to Claude Code when a session starts.
"command": "git status --short --branch && git log --oneline -5"
Settings and permissions 2
-
Stops git from reporting files as changed when only their executable bit changed.
git config core.fileMode false -
Keeps your own project notes for Claude Code out of the shared CLAUDE.md and out of git.
echo "CLAUDE.local.md" >> .gitignore
Nothing in this filter yet.