Hacker News new | ask | show | jobs
by kahkeng 1824 days ago
I find it pretty useful to use the `--patch` or `-p` option (also a mnemonic for "prompt") to various commands:

`git add -p`: prompt which hunks should be added to staging

`git checkout -p`: prompt which unstaged hunks should be thrown away

`git checkout HEAD^ -p`: prompt which hunks from HEAD should be discarded

`git reset -p`: prompt which currently-staged hunks should be unstaged

`git reset HEAD^ -p`: prompt which hunks from the HEAD commit should be unstaged

Nice thing is that you get interactive yes/no prompts with a preview of the change for each hunk, and you can also quit early if you realize it's not the command you want.

Doesn't fully address the potential for lost changes, but slows it down by adding an interactive roadblock for each change.

2 comments

Frustratingly `git add -p` adds a new and exciting way to accidentally lose "work": You can type `git commit -a` and all of a sudden all your manual selections of different hunks to include/exclude are irreversibly gone
hunk?
noun

1. a large piece of something, especially food, cut or broken off a larger piece.