News for hackers. Stuff that matters.
Re:Show HN: `Git add -p` with multiple buckets by mushyhammer on Saturday March 19, 2022 @11:04PM (#30739689)
I find it crazy how many people like working with git from the command line and just use -a any chance they get.

I never commit from CLI and have been using GitHub Desktop for years (it works on any repo/remote). Selecting individual files, patches, or even lines is a breeze. I usually make a bunch of changes, then pick, commit, branch, repeat. Every branch is based off `main` and can be pushed independently. Any leftover will just be dropped (any logging/testing lines that I never picked, for example)

Using a UI makes it easy to review your changes before you commit them, there’s no way you can review (and pick or discard) your 100-lines 5-files changes effectively on the command line.

GitHub Desktop is quite limited and I hope it stays that way, I still use the cli for operations other than commit and checkout.

Re:Show HN: `Git add -p` with multiple buckets by weird-eye-issue on Sunday March 20, 2022 @04:14AM (#30741013)
> Using a UI makes it easy to review your changes before you commit them, there’s no way you can review (and pick or discard) your 100-lines 5-files changes effectively on the command line.

Sorry, what? A command line IS a UI. And it happens to be great for displaying text, like what is displayed with git diff...

Re:Show HN: `Git add -p` with multiple buckets by mushyhammer on Sunday March 20, 2022 @07:36AM (#30741811)
I mean GUI, a clickable interface with columns and such. With a CLI you get all the files in a single scrollable area.

Side note: there are also GUIs for the terminal, they’re fine.

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
Re:Show HN: `Git add -p` with multiple buckets by larusso on Sunday March 20, 2022 @03:16AM (#30740802)
I use the git cli exclusively. The reason for it was Sourcetree. I started with git in 2010 coming from svn and had a hard time to wrap my head around the basic concepts of staging, commit and push etc. I used sourcetree and the docs and was very confused with the UI because it sometimes contradicted the understandings I got from the docs. So started using it from the cli and never switched back. In the beginning I used the ‘-i’ flag for the ‘git add’ a lot to filter out what files to stage etc. This was mainly because I worked in an unfamiliar project and wasn’t able to write out file paths from memory.

That has changed over the years. I use a few custom created aliases + specific Workflows how I go about making changes to a project.

I’m not interested in any of the fancy git UIs (sourcetree, tower, fork, kraken) because each tries to simplify git and or would break my workflow. For instance I don’t use Explorer, Finder etc to traverse files. I open a new terminal session and cd through my file system. I have a strict setup which I replicate on the different machines/VMs I use so I know it from memory. I don’t need to switch to a UI tool that knows where all my checkouts are and klick a terminal button to start a session there. I work from the terminal and open Idea, VSCode or vim from the project. The latter is the reason I could not adopt eMacs as it didn’t really work well for me with this kind of workflow.

And last. I stopped doing multiple fancy changes all at once in a project that would warrant the use of specialized tools or UIs. If I see that a change gets out of hand I start to formulate base batches on the go. I can create a series of commits and split them up/rebase afterwards.

But I don’t Stopp anyone from doing it differently. We are all wired differently and use the given tools how we feel best.

Ah also working with a lot of remote headless machines it is nice if you know you way around the cli in general. Or my favorite issue I had the last couple of weeks switching from Nvidia to AMD (X-Server did not start) issues ;) No ui to help to fix this.

Re:Show HN: `Git add -p` with multiple buckets by sixstringtheory on Sunday March 20, 2022 @02:13AM (#30740553)
Git CLI user here. I started out using Sourcetree about 10 years ago and have slowly moved everything into the terminal after also using many other GUIs. `git add -p` was the last thing to move to terminal because it can be hard to stage individual lines in hunks that can’t be automatically split.

But after I picked up emacs again and started using magit even that became a breeze.

Also, for reviewing changes, I use the no context option `git diff -U0`and more recently use git-delta for better color highlighting and the side-by-side diff view.

I also think in addition to just having learned and practiced over a decade, the way I’ve used Git has evolved too. I keep PRs small and cohesive, always squash all their commits, and because of that I don’t have to care so much about untangling cohesive changes at commit time, although I also don’t let them pile up. I usually do a lot of `git commit --amend` and `git commit --fixup` (more and more rarely actually followed by a `git rebase --autosquash` since the PR takes care of the squash now… I don’t even have to worry about order-based conflicts anymore).

Re:Show HN: `Git add -p` with multiple buckets by austinjp on Sunday March 20, 2022 @06:40AM (#30741574)
I didn't know about git delta, thanks for this. Presumably your referring to https://github.com/dandavison/delta ?

Does anyone have any tips for integrating it into an emacs workflow? I'm currently doing `git commit` from shell which launches emacs-nox with magit.

Re:Show HN: `Git add -p` with multiple buckets by spartanatreyu on Sunday March 20, 2022 @09:44PM (#30749092)
Hmm, I haven't heard of delta before. I've been using diff-so-fancy for a while ( https://github.com/so-fancy/diff-so-fancy), but I might have to give delta a try.

Edit: It looks like this page shows the comparison between delta and diff-so-fancy: https://dandavison.github.io/delta/features.html

Re:Show HN: `Git add -p` with multiple buckets by pca006132 on Sunday March 20, 2022 @02:45AM (#30740674)
I would say vim-fugitive is great for this.
Re:Show HN: `Git add -p` with multiple buckets by u801e on Saturday March 19, 2022 @10:25PM (#30739455)
There's a set of utilities[1] in a package called patchutils that contains several commands to manipulate patches. Their version of splitdiff[2] will create a set of patch files from a single patch where each patch would only apply to a single file. It doesn't appear to have the capability of splitting out individual hunks like your version, but could be updated to do so.

[1] https://directory.fsf.org/wiki/Patchutils

[2] https://www.unix.com/man-page/suse/1/splitdiff/

Re:Show HN: `Git add -p` with multiple buckets by jayd16 on Saturday March 19, 2022 @08:35PM (#30738866)
This is the one P4 workflow feature I wish git had from the beginning. In Perforce, changes can be grouped into uncommitted "change lists." Its really helpful if you're working on multiple things especially when they're unrelated features or projects. Works very well for artists and such.

Sadly P4 fumbles at the 10 because you can't split changes by hunk, file only.

I hope something like this (or maybe just multiple named stages?) makes it into git.

Re:Show HN: `Git add -p` with multiple buckets by noitpmeder on Saturday March 19, 2022 @11:24PM (#30739789)
My company still uses p4 for most things and most developers I know HATE it, compared to the equivalent git(hub/lab).

Largest complaints currently are the infesibility of the feature-branch workflow and the terribad pre-commit code review story.

Re:Show HN: `Git add -p` with multiple buckets by jayd16 on Sunday March 20, 2022 @03:16AM (#30740807)
I much prefer git and the git ecosystem but a good feature is a good feature
Re:Show HN: `Git add -p` with multiple buckets by RyJones on Saturday March 19, 2022 @09:08PM (#30739056)
Are you ex-MSFT? I am, and I miss that workflow.
Re:Show HN: `Git add -p` with multiple buckets by vbezhenar on Sunday March 20, 2022 @03:00AM (#30740747)
Idea can do that with git.
Re:Show HN: `Git add -p` with multiple buckets by dorianmariefr on Saturday March 19, 2022 @05:28PM (#30737728)
I feel like this should be part of git, also naming it git-split-patch and having it in the PATH would allow to to do `git split-patch`
Re:Show HN: `Git add -p` with multiple buckets by pabs3 on Saturday March 19, 2022 @09:20PM (#30739124)
It is useful outside of git, so I would suggest having both the git alias and the main command.
Re:Show HN: `Git add -p` with multiple buckets by aleclm on Saturday March 19, 2022 @05:41PM (#30737821)
Yeah, that's what I want to do next.
Re:Show HN: `Git add -p` with multiple buckets by pabs3 on Saturday March 19, 2022 @09:22PM (#30739132)
Please keep the main command too, since it is useful outside of git too, like when using the quilt patch system, or other VCSen like Mercurial.
Re:Show HN: `Git add -p` with multiple buckets by almog on Saturday March 19, 2022 @06:29PM (#30738129)
Is the main use case being splitting large uncommitted change set into logical units in _one pass_ rather than few iterations of `git add -p && git commit`?
Re:Show HN: `Git add -p` with multiple buckets by teaearlgraycold on Saturday March 19, 2022 @10:15PM (#30739415)
That would be helpful for me. Sometimes I forget which pass I'm on and have to redo the commit.
Re:Show HN: `Git add -p` with multiple buckets by almog on Sunday March 20, 2022 @04:45AM (#30741134)
I don't do this often, but when I watched the demo that was the only use case that came to mind.

Perhaps the author had different motivation to start it, which is why I posted this question — the demo explain what the program does but provide little context other than extracting actual patches, and then again, it begs the question of 'why'.

Re:Show HN: `Git add -p` with multiple buckets by aleclm on Sunday March 20, 2022 @04:42AM (#30741119)
That's exactly the use case.
Re:Show HN: `Git add -p` with multiple buckets by almog on Sunday March 20, 2022 @08:36AM (#30742139)
Cool, I thought there might be some other flow I was not aware of, thanks for clarifying that!

The demo recording is good and I think it would be even better to state the motivation/use case explicitly in the README.

Re:Show HN: `Git add -p` with multiple buckets by bckr on Saturday March 19, 2022 @09:38PM (#30739215)
this is my question as well
Re:Show HN: `Git add -p` with multiple buckets by westurner on Saturday March 19, 2022 @09:26PM (#30739155)
There are probably a couple good ways to avoid trying to split `git add -p` with e.g. jujutsu `jj`? https://github.com/martinvonz/jj/blob/main/docs/git-comparis...

If CI isn't running tests for every PR, `git add -p` can create pretty patches that fail when attempting to `git bisect` later.

> Comprehensive support for rewriting history: Besides the usual rebase command, there's `jj describe` for editing the description (commit message) of an arbitrary commit. There's also `jj edit`, which lets you edit the changes in a commit without checking it out. To split a commit into two, use `jj split`. You can even move part of the changes in a commit to any other commit using `jj move`.

Re:Show HN: `Git add -p` with multiple buckets by abalaji on Saturday March 19, 2022 @03:30PM (#30736859)
This is great, I've been using Sublime Merge similarly in my workflow. I stage individual files while drafting the commit message and make sure to split lines across commits for logical rollbacks. I personally find it a lot faster than using the CLI, at least in this case.
Re:Show HN: `Git add -p` with multiple buckets by warmwaffles on Saturday March 19, 2022 @11:17PM (#30739762)
I specifically love committing individual lines in a hunk and leaving the rest not staged until I am good and ready to stage them. It's been a great addition to my workflow and makes self reviewing my stuff easier before I go to send a PR.
Re:Show HN: `Git add -p` with multiple buckets by barbazoo on Saturday March 19, 2022 @05:10PM (#30737610)
I agree. For this workflow using a GUI to stage individual lines is just a lot faster.
Re:Show HN: `Git add -p` with multiple buckets by hultner on Saturday March 19, 2022 @08:26PM (#30738822)
Same here, I’m a tmux junkie but for the particular use case of staging smaller parts I much prefer sublime. Have been meaning to give fugitive a proper try as well, looks like it’a gui allows for a similar use case as well without the context switch.
Re:Show HN: `Git add -p` with multiple buckets by fuzzy2 on Saturday March 19, 2022 @05:43PM (#30737826)
Ah, finally changelists are coming to Git. Having worked with Perforce, this certainly feels very familiar.
Re:Show HN: `Git add -p` with multiple buckets by charcircuit on Saturday March 19, 2022 @07:25PM (#30738495)
This is unrelated to git. What do you mean by change list?
Re:Show HN: `Git add -p` with multiple buckets by fuzzy2 on Sunday March 20, 2022 @11:17AM (#30743315)
Dunno what you mean by this being unrelated to Git.

In Git, you can stage files (git add). You have only one “staging area”. In Perforce, you can also stage files (of sorts). You can create as many “staging areas” (changelists) as you want. Changelists are publicly visible, if so desired, before they are even “committed” (submitted, in P4 jargon).

Changelists work on entire files, unlike what TFA proposes.

Re:Show HN: `Git add -p` with multiple buckets by charcircuit on Sunday March 20, 2022 @03:20PM (#30745699)
>Dunno what you mean by this being unrelated to Git.

This tool is for splitting patches. It's a tool for working with patches and not one for working with git. Now of course patches are used with git.

Re:Show HN: `Git add -p` with multiple buckets by ArtRichards on Sunday March 20, 2022 @04:08AM (#30740991)
I was looking a few years ago for a GUI for git, on linux. I eventually settled on SmartGit, been using it since..

https://www.syntevo.com/smartgit/

Anyone have alternatives for Linux?

Re:Show HN: `Git add -p` with multiple buckets by davvid on Sunday March 20, 2022 @04:29AM (#30741084)
http://git-cola.github.io/ has all of the nice split-patch index editing stuff (and much more) and is vimmish in its keyboard interactions.
Re:Show HN: `Git add -p` with multiple buckets by efficientsticks on Sunday March 20, 2022 @04:42AM (#30741122)
I haven’t used it in years, but magit is really great.

Nowadays I just do a few rounds of git add -p; commit, or trusty ole git commit -av

Re:Show HN: `Git add -p` with multiple buckets by renewiltord on Saturday March 19, 2022 @03:18PM (#30736785)
Oh this is interesting. Sometimes I make unrelated changes and then I want to tease the things apart at commit time. And when I do I find that I don’t just want to stage or skip staging. Worth a shot but I’d prefer if I could put it into secondary staging and post-commit the first of those becomes staged.

Probably doable and shiftable to a git-splitpatch too.

Re:Show HN: `Git add -p` with multiple buckets by Sharlin on Saturday March 19, 2022 @04:33PM (#30737349)
In modern gits you can also `git stash -p` allowing you to use (named if needed) stashes for that purpose.
Re:Show HN: `Git add -p` with multiple buckets by aleclm on Saturday March 19, 2022 @05:41PM (#30737819)
This is different.

The key value of `split-patch` is that you can create as many patches (commits) as you want by doing a single pass over the (potentially large) patchset.

If you're willing to do multiple passes over the patchset, you could `git commit` right away.

Anyway, cool, I didn't know you could name stashes!

Re:Show HN: `Git add -p` with multiple buckets by chx on Saturday March 19, 2022 @04:12PM (#30737151)
perhaps as branches? then we can cherry pick those as needed.
Re:Show HN: `Git add -p` with multiple buckets by OJFord on Saturday March 19, 2022 @05:03PM (#30737569)
Stashes are more appropriate; since they're really just handier references to commits they can be cherry-picked too, as `stash@{N}` where N is the zero-based integer for which one you want.

(You can even `git cherry-pick stash@{0}^` for the commit the stash was made at. It really is just a commit made at that point, with that parent, only without updating HEAD, or the checked-out branch.)

Re:Show HN: `Git add -p` with multiple buckets by chx on Saturday March 19, 2022 @07:54PM (#30738680)
Thanks for reminding me, I have used arbitrarily named refs before just slipped my mind. So it could be like 'refs/patches' or whatever. I used refs/backups in my safety net https://gist.github.com/chx/3a694c2a077451e3d446f85546bb9278
Re:Show HN: `Git add -p` with multiple buckets by pabs3 on Saturday March 19, 2022 @09:21PM (#30739131)
I generally just use `git gui` to make multiple commits by staging and then committing just the lines I want.
Re:Show HN: `Git add -p` with multiple buckets by spartanatreyu on Sunday March 20, 2022 @09:47PM (#30749106)
I'm pretty sure git got rid of their `git gui` command a while ago.

What version of git are you using? You probably want to check with `git --version`.

The latest version is 2.35.1.

Re:Show HN: `Git add -p` with multiple buckets by pabs3 on Sunday March 20, 2022 @10:33PM (#30749437)
I'm using 2.35.1, perhaps you haven't installed the git-gui and gitk packages for your distro?

Both git gui and gitk are still in the git repo AFAICT:

https://github.com/git/git/tree/master/git-gui https://github.com/git/git/tree/master/gitk-git

Re:Show HN: `Git add -p` with multiple buckets by alfiedotwtf on Saturday March 19, 2022 @06:27PM (#30738116)
Oh very nice! This almost seems obvious that it should be part of git itself!
Re:Show HN: `Git add -p` with multiple buckets by kasabali on Sunday March 20, 2022 @06:29AM (#30741529)
Re:Show HN: `Git add -p` with multiple buckets by junon on Sunday March 20, 2022 @02:45AM (#30740677)
I wish git supported this directly. Even if it was specific functionality to `git commit -p`.