brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git apply with check tests a patch before it touches your files

Git
~/tips/tip-git-diff-apply-check-patch

git apply --check wip.patch

Saves uncommitted work as a patch file and checks that it applies cleanly elsewhere.

To move uncommitted work to another machine or keep a copy of an experiment, save it as a patch. Before applying one, I’d let git check it first.

git diff > wip.patch
git apply --check wip.patch

The first line writes your unstaged changes to a file. Add --cached for staged changes or HEAD for both. On the other side, git apply --check tests whether the patch would apply, and changes nothing. No output means it would work; otherwise git names the file and line that don’t match.

Then apply it for real and look at what it would change:

git apply --stat wip.patch
git apply wip.patch

--stat lists the files and line counts without applying anything. The plain command applies the changes to your working tree, unstaged, like you had typed them yourself.

If the code moved on, git apply -3 tries a three-way merge and leaves conflict markers instead of refusing.

A plain git diff doesn’t include new untracked files, so stage them or use git add -N first, or they’re missing from the patch. Binary files need git diff --binary, otherwise the patch only says they differ. And this saves changes, not commits; for those, git format-patch is the better tool.

Further reading: git-apply documentation, git-diff documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link