Can we do better than "git checkout/add -p"
I am doing more "git checkout -p" (selective revert of local changes out of the working tree files) these days, as well as "git add -p" (selective adding of local changes to the index), and what I often wish is to have _both_ as possible options in a single session. That is, the local changes in my working tree often fall into three categories. (1) One that is clearly good, (2) one that is good but not yet ready, and (3) one that is bogus and should be discarded.
"git checkout -p" is a way that is very suitable for (3), while "git add -p" is a way to deal with (1). To (2), I say "no" in "git add -p", but there is no easy way from "git add -p" to say that the hunk is (3).
My current workaround is not to use "git checkout -p" and instead (e)dit an undesirable hunk into a no-op hunk. This is serviceable, but with two caveats:
- The underlying 'apply' machinery does not see a truly no-op, context-only hunk. You'd need to pretend removing an existing line and adding the same line back.
- (e)dit applies the edited hunk right away without giving the user a chance to proofread and approve or reedit.
Quoted 8 lines · Junio C Hamano
I am doing more "git checkout -p" (selective revert of local changes out of the working tree files) these days, as well as "git add -p" (selective adding of local changes to the index), and what I often wish is to have _both_ as possible options in a single session. That is, the local changes in my working tree often fall into three categories. (1) One that is clearly good, (2) one that is good but not yet ready, and (3) one that is bogus and should be discarded.
What if there is a hunk you want to squash to a previous commit like HEAD~2 or to a new commit in a separate branch?
I think that if we add a new way to handle hunks, we should consider more cases than just reverting, keeping or indexing. We could perhaps take advantage of recent developments in git history to implement the additional cases.
On the other hand, it seems to me that GitButler's but rub command could do a lot of things like that, but it looks like they recently replaced and split that command into more explicit, intent-based commands (see https://github.com/gitbutlerapp/gitbutler/releases).
So I guess the main issue in designing such a feature is to make it do many things, but not too many.
Quoted 9 lines · Junio C Hamano
I am doing more "git checkout -p" (selective revert of local changes out of the working tree files) these days, as well as "git add -p" (selective adding of local changes to the index), and what I often wish is to have _both_ as possible options in a single session. That is, the local changes in my working tree often fall into three categories. (1) One that is clearly good, (2) one that is good but not yet ready, and (3) one that is bogus and should be discarded.
Quoted 4 lines
"git checkout -p" is a way that is very suitable for (3), while "git add -p" is a way to deal with (1). To (2), I say "no" in "git add -p", but there is no easy way from "git add -p" to say that the hunk is (3).
Not a core Git solution, but the interface provided by Fugitive [1] (inspired by Magit) makes it easy to browse all hunks, with options to (e.g.) stage, unstage, or revert.
(The revert command even shows how to bring back the reverted content, in case you made a mistake.)
It's a bit hard to explain how this works over email. There's an old video [2] which doesn't reflect many newer enhancements to Fugitive (e.g., you no longer need to open a file to diff it; you can toggle the "git diff" or "git diff --cached" view from within the status buffer---from there, all the staging/unstaging/discard maps work on individual hunks or ranges of lines), but still shows the general idea.
[1]: https://github.com/tpope/vim-fugitive
[2]: http://vimcasts.org/episodes/fugitive-vim-working-with-the-git-index/
I raise this as the kind of interface we could learn from: emulating it might be a bit heavier (a full TUI?), but is certainly more convenient to use than the prompt-loop over hunks.
Best,
Signature · 1 line
D. Ben Knoble
Quoted 3 lines · D. Ben Knoble
I raise this as the kind of interface we could learn from: emulating it might be a bit heavier (a full TUI?), but is certainly more convenient to use than the prompt-loop over hunks.
Yes, the 'one hunk at a time' model was easy to implement and start using, but its limitations are apparent. Users want to be able to jump around, starting in the middle and returning to the top later, for example.
It is more or less orthogonal to the reason I started this discussion, though, which is that limiting the direction in which modifications flow restricts the workflow, burdens the user, and makes the process error-prone.
When I see a hunk, I can immediately tell if it is one of three kinds (i.e., those we want to add, those we want to leave in the working tree, and those we want to discard from the working tree). But with 'git add -p' (especially with the original version of the feature, before the 'e' (edit) command was introduced), the third kind must be treated the same way as the second. Then, after I am done with 'git add -p', I must go through the remaining hunks, sift them into two categories (those we want to keep in the working tree and those we want to discard), and run 'git checkout -p' to deal with the latter.
We should be able to improve this workflow without deviating from the 'one hunk at a time' model.
Quoted 28 lines · Junio C Hamano
Quoted 3 lines · D. Ben Knoble
I raise this as the kind of interface we could learn from: emulating it might be a bit heavier (a full TUI?), but is certainly more convenient to use than the prompt-loop over hunks.
Yes, the 'one hunk at a time' model was easy to implement and start using, but its limitations are apparent. Users want to be able to jump around, starting in the middle and returning to the top later, for example.
It is more or less orthogonal to the reason I started this discussion, though, which is that limiting the direction in which modifications flow restricts the workflow, burdens the user, and makes the process error-prone.
When I see a hunk, I can immediately tell if it is one of three kinds (i.e., those we want to add, those we want to leave in the working tree, and those we want to discard from the working tree). But with 'git add -p' (especially with the original version of the feature, before the 'e' (edit) command was introduced), the third kind must be treated the same way as the second. Then, after I am done with 'git add -p', I must go through the remaining hunks, sift them into two categories (those we want to keep in the working tree and those we want to discard), and run 'git checkout -p' to deal with the latter.
Quoted 2 lines
We should be able to improve this workflow without deviating from the 'one hunk at a time' model.
Perhaps I emphasized the wrong attributes; what initially brought Fugitive to mind is the fact that one set of commands move hunks between staged, unstaged, and reverted.
Whether that's in a more complex interface or not, that seemed like the thrust of what you were after.
Signature · 1 line
D. Ben Knoble
Quoted 5 lines · D. Ben Knoble
Quoted 2 lines
We should be able to improve this workflow without deviating from the 'one hunk at a time' model.
...
Whether that's in a more complex interface or not, that seemed like the thrust of what you were after.
It seems to me that we are saying the same thing ;-).
Quoted 27 lines · Junio C Hamano
Quoted 3 lines · D. Ben Knoble
I raise this as the kind of interface we could learn from: emulating it might be a bit heavier (a full TUI?), but is certainly more convenient to use than the prompt-loop over hunks.
Yes, the 'one hunk at a time' model was easy to implement and start using, but its limitations are apparent. Users want to be able to jump around, starting in the middle and returning to the top later, for example.
It is more or less orthogonal to the reason I started this discussion, though, which is that limiting the direction in which modifications flow restricts the workflow, burdens the user, and makes the process error-prone.
When I see a hunk, I can immediately tell if it is one of three kinds (i.e., those we want to add, those we want to leave in the working tree, and those we want to discard from the working tree). But with 'git add -p' (especially with the original version of the feature, before the 'e' (edit) command was introduced), the third kind must be treated the same way as the second. Then, after I am done with 'git add -p', I must go through the remaining hunks, sift them into two categories (those we want to keep in the working tree and those we want to discard), and run 'git checkout -p' to deal with the latter.
Quoted 2 lines
We should be able to improve this workflow without deviating from the 'one hunk at a time' model.
I wonder whether we can take JJ as inspiration. For commands like jj-split(1) it has the ability to interactively select specific hunks via jj split --interactive, too. But instead of looping through stuff, it uses a full TUI that:
- Gives you a list of all files that have changed. On this level you can select/deselect the complete file.
- Also allows you to expand files and then select/deselect individual hunks and lines.
I found that model to be quite a bit superior to Git's own interactive mode.
I've been playing around with the thought of introducing ncurses-based interfaces into Git. I've been mostly thinking about git-history(1) here so that you can just move commits around, squash them together, drop them and so on. But I think fancy stuff like TUIs can also be applied to other parts of Git, as well, to make things a bit more visual to our users and, as a consequence, easier to use.
Patrick
Quoted 6 lines · Patrick Steinhardt
I've been playing around with the thought of introducing ncurses-based interfaces into Git. I've been mostly thinking about git-history(1) here so that you can just move commits around, squash them together, drop them and so on. But I think fancy stuff like TUIs can also be applied to other parts of Git, as well, to make things a bit more visual to our users and, as a consequence, easier to use.
That sounds a whole lot like lazygit to me [1]; it does all those things in a rather intuitive way, including Junio's original use case of selecting a hunk and staging or discarding it.
Is it really worth adding such functionality to core git? I like the idea of tools specializing on what they do well; core git on providing the core functionality, GUI tools on presenting it in a UI.
Quoted 12 lines · Stefan Haller
Quoted 6 lines · Patrick Steinhardt
I've been playing around with the thought of introducing ncurses-based interfaces into Git. I've been mostly thinking about git-history(1) here so that you can just move commits around, squash them together, drop them and so on. But I think fancy stuff like TUIs can also be applied to other parts of Git, as well, to make things a bit more visual to our users and, as a consequence, easier to use.
That sounds a whole lot like lazygit to me [1]; it does all those things in a rather intuitive way, including Junio's original use case of selecting a hunk and staging or discarding it.
Quoted 5 lines
Is it really worth adding such functionality to core git? I like the idea of tools specializing on what they do well; core git on providing the core functionality, GUI tools on presenting it in a UI.
I think it depends. There are lots of users out there who use core Git, only, and we often hear complaints from this class of users that Git makes common workflows way too complex. I certainly think that we should up our game and try to make such common workflows easier to wield. Tools like JJ demonstrate that there are a bunch of improvements that we can do, and many of those aren't even that hard to implement.
So If we see that there are use cases where core Git itself is lacking and where the consequence is a bad user experience for common workflows then I think that we should plug that gap in core Git itself.
That being said, I don't think we should get into the business of building a full UI, as that feels like a can of worms indeed. Adding something like a user interface around hunk selection certainly feels like core functionality that I think should be in scope for Git. Whether a full history-editing user interface should be part of core Git may be a different question though, as this is getting significantly closer to a full UI.
Patrick
Quoted 12 lines · Stefan Haller
Quoted 6 lines · Patrick Steinhardt
I've been playing around with the thought of introducing ncurses-based interfaces into Git. I've been mostly thinking about git-history(1) here so that you can just move commits around, squash them together, drop them and so on. But I think fancy stuff like TUIs can also be applied to other parts of Git, as well, to make things a bit more visual to our users and, as a consequence, easier to use.
That sounds a whole lot like lazygit to me [1]; it does all those things in a rather intuitive way, including Junio's original use case of selecting a hunk and staging or discarding it.
Quoted 5 lines
Is it really worth adding such functionality to core git? I like the idea of tools specializing on what they do well; core git on providing the core functionality, GUI tools on presenting it in a UI.
My philosophy has always been "do not compete with your customer".
If we add an officially sanctioned XYZ to 'git-core', it would hold an undue advantage over tools built on 'git-core' that perform the same task, not because ours is implemented better but merely because it comes bundled with 'git-core'. I do not want that.
An exception is when our XYZ is truly of "we wish someone had written something better that offers functionality like this" quality, serving as a "usable but perhaps not pretty" demonstration.
Thanks.
Quoted 22 lines · Junio C Hamano
Quoted 12 lines · Stefan Haller
Quoted 6 lines · Patrick Steinhardt
I've been playing around with the thought of introducing ncurses-based interfaces into Git. I've been mostly thinking about git-history(1) here so that you can just move commits around, squash them together, drop them and so on. But I think fancy stuff like TUIs can also be applied to other parts of Git, as well, to make things a bit more visual to our users and, as a consequence, easier to use.
That sounds a whole lot like lazygit to me [1]; it does all those things in a rather intuitive way, including Junio's original use case of selecting a hunk and staging or discarding it.
Quoted 5 lines
Is it really worth adding such functionality to core git? I like the idea of tools specializing on what they do well; core git on providing the core functionality, GUI tools on presenting it in a UI.
My philosophy has always been "do not compete with your customer".
Quoted 8 lines
If we add an officially sanctioned XYZ to 'git-core', it would hold an undue advantage over tools built on 'git-core' that perform the same task, not because ours is implemented better but merely because it comes bundled with 'git-core'. I do not want that.
An exception is when our XYZ is truly of "we wish someone had written something better that offers functionality like this" quality, serving as a "usable but perhaps not pretty" demonstration.
I guess that's fair. As I said in my parallel reply to this message, I could also see that having something like a git-history(1) UI might be too much. But I think having a TUI for selecting individual hunks in a better way compared to git add -i and friends might be sensible and not significantly overlap with other projects.
Patrick
Hi Junio,
Quoted 17 lines · Junio C Hamano
I am doing more "git checkout -p" (selective revert of local changes out of the working tree files) these days, as well as "git add -p" (selective adding of local changes to the index), and what I often wish is to have _both_ as possible options in a single session. That is, the local changes in my working tree often fall into three categories. (1) One that is clearly good, (2) one that is good but not yet ready, and (3) one that is bogus and should be discarded.
"git checkout -p" is a way that is very suitable for (3), while "git add -p" is a way to deal with (1). To (2), I say "no" in "git add -p", but there is no easy way from "git add -p" to say that the hunk is (3).
My current workaround is not to use "git checkout -p" and instead (e)dit an undesirable hunk into a no-op hunk. This is serviceable, but with two caveats:
Quoted 6 lines
- The underlying 'apply' machinery does not see a truly no-op, context-only hunk. You'd need to pretend removing an existing line and adding the same line back.
- (e)dit applies the edited hunk right away without giving the user a chance to proofread and approve or reedit.
I, too, often find myself in exactly that kind of need. That's why I was *so* disappointed when
https://lore.kernel.org/git/[email protected]/
was shot down unceremoniously. I still think that would be a good addition. I even opened https://github.com/gitgitgadget/git/issues/1828
and sketched
https://github.com/git/git/compare/master...dscho:git:add-p-stash-mode to the same extent.
Maybe it is time to revisit that verdict, and see whether there is really no way to accept that clearly needed functionality.
Ciao,
Johannes
Quoted 20 lines · Johannes Schindelin
Quoted 10 lines
My current workaround is not to use "git checkout -p" and instead (e)dit an undesirable hunk into a no-op hunk. This is serviceable, but with two caveats:
- The underlying 'apply' machinery does not see a truly no-op, context-only hunk. You'd need to pretend removing an existing line and adding the same line back.
- (e)dit applies the edited hunk right away without giving the user a chance to proofread and approve or reedit.
I, too, often find myself in exactly that kind of need. That's why I was *so* disappointed when
https://lore.kernel.org/git/[email protected]/
was shot down unceremoniously. I still think that would be a good addition. I even opened https://github.com/gitgitgadget/git/issues/1828
and sketched
https://github.com/git/git/compare/master...dscho:git:add-p-stash-mode to the same extent.
Quoted 2 lines
Maybe it is time to revisit that verdict, and see whether there is really no way to accept that clearly needed functionality.
Thanks for digging up that link. After reading Junio's message that started this thread, I _thought_ we had discussed this a dozen times already, but after searching the archive could only come up with this thread:
https://lore.kernel.org/git/[email protected]/
But the one you linked is another example, and nicely links back recursively to at least two other instances. ;)
I see I am quoted in one of them as "it's a little weird for add -p to change the working tree", but I want to make clear that I _don't_ oppose a feature like this. I think it would be super useful. We may find a way to avoid that "weird" property (e.g., by putting the "combined" stash/add mode under a different command's "-p"), or we may just accept it.
-Peff
Quoted 20 lines · Johannes Schindelin
Quoted 10 lines
My current workaround is not to use "git checkout -p" and instead (e)dit an undesirable hunk into a no-op hunk. This is serviceable, but with two caveats:
- The underlying 'apply' machinery does not see a truly no-op, context-only hunk. You'd need to pretend removing an existing line and adding the same line back.
- (e)dit applies the edited hunk right away without giving the user a chance to proofread and approve or reedit.
I, too, often find myself in exactly that kind of need. That's why I was *so* disappointed when
https://lore.kernel.org/git/[email protected]/
was shot down unceremoniously. I still think that would be a good addition. I even opened https://github.com/gitgitgadget/git/issues/1828
and sketched
https://github.com/git/git/compare/master...dscho:git:add-p-stash-mode to the same extent.
Quoted 2 lines
Maybe it is time to revisit that verdict, and see whether there is really no way to accept that clearly needed functionality.
I agree that functionality to cover the "classify three kinds of changes in the working tree files, update both index and working tree files" is a good thing to have.
I did not, and still do not, think "git add -p" is a good place to add a feature to munge working tree files, though.
IOW, what I am lamenting is that we have add/checkout each having "-p" options, and as separate commands, the user cannot handle three kinds of changes in the working tree files without switching between these two commands.
- changes that we want to add to the index for the next commit (you tell [y] to add -p)
- changes that we want to leave in the working tree files (you tell [n] to add -p)
- changes that we want to get rid of from the working tree files (you tell [y] to checkout -p)
The ancient patch deserved to be discarded, simply because "if we are adding it to 'add -p', what about 'checkout p'?" is a valid question.
But the need to have a single command that can deal with the three kinds without exiting does exist. It might be beneficial to widen our horizon to also consider if it would help us to include stash into the mix. It may give two choices to handle the second class of changes, making them into four categories, i.e.
- changes that we want to add to the index for the next commit
- changes that we want to stash away from the working tree files
- changes that we want to leave in the working tree files
- changes that we want to get rid of from the working tree files
Quoted 6 lines · Jeff King
I see I am quoted in one of them as "it's a little weird for add -p to change the working tree", but I want to make clear that I _don't_ oppose a feature like this. I think it would be super useful. We may find a way to avoid that "weird" property (e.g., by putting the "combined" stash/add mode under a different command's "-p"), or we may just accept it.
I guess our messages crossed ;-) I do agree that it would be very nice to have a feature like this.