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 restore -p throws away only the changes you pick

Git
~/tips/tip-git-restore-patch-discard-hunks

git restore -p src/app.js

Discards selected hunks from your working tree and keeps the rest of your edits.

Debug prints, a commented-out block, a quick experiment: Sometimes you want to drop some of your edits in a file but keep others. git restore -p asks hunk by hunk.

git restore -p src/app.js

Git shows each changed hunk and asks Discard this hunk from worktree [y,n,q,a,d,e,p,P,?]?. y throws that hunk away, n keeps it, q stops, s splits a big hunk into smaller ones when that’s possible, and ? lists everything. Leave out the path and it walks through all changed files.

It’s the mirror image of git add -p. With add you pick what goes into the commit; with restore you pick what disappears from your working tree. The two go well together, in my view: stage the good parts with git add -p, then clean up the leftovers with git restore -p.

Be careful: this is destructive. Discarded hunks were never committed or staged, so git has no copy of them. There’s no undo and the reflog won’t help. Read each hunk before you press y, and avoid a (discard everything left in the file) unless you’re sure. When in doubt, run git stash first so you have a copy to go back to.

Further reading: git-restore 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