News for hackers. Stuff that matters.
Re:Show HN: `Git add -p` with multiple buckets by u801e on Saturday March 19, 2022 @11:28PM (#30739807)
I actually use the git cli by filtering text to various command std input using vim. So, for staging diffs, I'll read the output of git diff, and hand edit the diff as needed, and stage individual hunks by filtering the text through the git apply command with the appropriate parameters.

I can then run git status -v to show what's currently staged, write the commit message and filter it through the git commit command.

> there’s no way you can review (and pick or discard) your 100-lines 5-files changes effectively on the command line.

It's easy enough to find the approriate hunk and edit the diff to stage what's needed. I may go through the process a few times to create a single commit depending on how many hunks I have to deal with and how many times I need to split them though.

In fact, my normal workflow is to make the necessary changes for a particular feature I'm working on, take the diff against the base branch and then stage parts of that diff to create a series of sensible commits for the branch.

Re:Show HN: `Git add -p` with multiple buckets by 1MachineElf on Sunday March 20, 2022 @12:18PM (#30743958)
I would really like to see this workflow. Are there any articles, videos, or asciinemia recordings of how this is done?
Re:Show HN: `Git add -p` with multiple buckets by u801e on Wednesday March 23, 2022 @11:51AM (#30779747)
I don't know of any, but the key thing to know is how to edit diffs manually. If you've ever run git add or reset with the -p parameter, you may have noticed an option where you can manually edit a diff hunk. If you choose that option, it describes several rules for editing hunks:

1. Remove lines beginning with + to keep them from being added

2. Replace the beginning - with <space> for lines to keep them from being removed

But you can go further with this by adding more lines beginning with + to add lines that weren't in the original hunk, or add a - prefix to a context line (beginning with a <space>) to remove it. You can even modify the content of lines prefixed with + (i.e., to fix a syntax error you noticed) before staging them.

The other thing to note is the surrounding context lines for a given hunk. Ideally, you have at least 3 lines before and 3 lines after the hunk to ensure that when it's applied, it changes the file in the correct location.

In order to actually stage a hunk, you need to add the necessary header lines above the line that begins with @@. These lines are the ones that begin with diff, index, ---, and +++. For the first, third and fourth lines, the path and file name need to match the file you intend to apply the patch to.

When you're ready to stage your edited hunk, you just visually highlight it in vim (shift-v) and then run

    :'<,'> !git apply --cached --recount
The --cached option prevents git from trying to modify the file in the working tree and limits it to just modifying what's in the git index. The --recount option basially makes it easier to apply an edited patch by not really looking at the line that begins with @@. If your patch applies, you can view how it looks like in the index by running

    git status -v
or

    git diff --cached
You can even view the file as it is in the git index by running

    git show :0:path/to/file