Notes/Git

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.

MERGE_BASE="origin/BRANCH" ; git reset $(git merge-base "${MERGE_BASE}" $(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)
# Without a/ b/ prefixes
git format-patch --no-prefix -1 $(git stash create)

Apply Patch

Credit to stackoverflow.com/a/2250170/4728587 and stackoverflow.com/a/54022866/4728587 (CC BY-SA 4.0), I did not RTFM.

Get stats

git apply --stat ___patch___
# Without a/ b/ prefixes
git apply --stat -p0 ___patch___

Check errors

git apply --check ___patch___
# Without a/ b/ prefixes
git apply --check -p0 ___patch___

Apply and commit

git am --keep-cr --signoff < ___patch___ 
# Without a/ b/ prefixes
git am --keep-cr --signoff -p0 < ___patch___ 
Jordan Mele

Jordan Mele

Site owner