Handy Git Commands

A few git commands I often find myself reaching for.

Reset Against Merge Base

Removes commits not present in the target branch (merge base), leaving the changes in the working directory where they can then be committed in a branch new commit. Basically a naive way to squash commits.

git reset $(git merge-base {{target-branch}} $(git branch --show-current))

List Conflicts

When working in a monorepo rebasing while there are merge conflicts can at times result in very noisy output. No doubt there are many different ways to extract this information, but here is what I've landed on.

git diff --name-only --diff-filter=U

Delete Ignores Files and Directories

Take care to backup any configuration customisations you've made beforehand. Depending on the repo, you may also need to go through first time setup again.

Note that -i (for interactive mode) is included, and recommended unless you need automation (e.g. for CI environments).

git clean -d -i -X

Delete Untracked and Ignored

Like above, but untracked changes will be expunged as well.

git clean -d -i -x

Create Patch From Current Changes

git format-patch -1 $(git stash create)
Jordan Mele

Jordan Mele

Site owner