3

With git add -p I can add individual hunks step by step.

In many cases I want to add exactly one hunk and then quit the interactive mode immediately. I don't want the next hunk to show up, because it will confuse me when writing the commit message, and forces me to scroll up to see the added hunk.

Is there any way to do this directly, instead of showing the next hunk and then using "q"?

I already tried typing "aq" but it does not work (and I did not really expect it to work).

donquixote
  • 4,877
  • 3
  • 31
  • 54
  • Why don’t you use a git client instead of the command line? – x squared Aug 17 '19 at 22:27
  • 1
    @xsquared Reliability? Portability? Speed? Better understanding? Maybe all of them? Just guessing. – Romain Valeri Aug 18 '19 at 00:04
  • And I’m guessing that far too many people use git on the CL without actually needing to. If it’s about understanding, this is the perfect example for the benefits of a GUI. I learn much more about git using the smartgit client than CL, because it visualizes stuff. It also shows you the git commands executed for each GUI operation if you want to know. But really, what is important to understand about git is not its palette of command names but rather the concepts behind them. Apart from that, the OP’s problem does not exist when using a client. – x squared Aug 18 '19 at 10:19
  • How do you know the first hunk is the one you want? – evolutionxbox Aug 18 '19 at 19:51
  • @evolutionxbox not the first hook,. just the first where I choose "y". Or the one where I choose "yq" which would mean "yes and quit". Doesn't exist, but the question is what would be the closest match. – donquixote Aug 18 '19 at 23:06
  • You probably know this already, but I’d press y and then q. – evolutionxbox Aug 18 '19 at 23:07
  • @xsquared I use the cli so that people like you don't tell me "why u no cli?" Or to be more serious: There are pros and cons to cli vs client. Familiarity is a big thing, doing everything with the keyboard, but then also some advanced git commands like "rebase --rebase-merges" might not be fully supported in a client. I might not have given git clients their fair chance, but I would say for this questions it is mostly a distraction. – donquixote Aug 18 '19 at 23:12
  • @evolutionxbox you mean y + Enter + q + Enter. Yes this works, but it still means the second hunk enters into my brain, and into my terminal window. – donquixote Aug 18 '19 at 23:14
  • @donquixote It might not be the answer you expected, but it is true that you are struggling with git cli’s presentation which is exactly the reason why there are gui clients. I don’t think it is wrong to offer an alternative solution to the problem in a comment… I still encourage you to have a look at smartgit, which is even more powerful than bare git. – x squared Aug 19 '19 at 10:21
  • @xsquared as a mere suggestion in a comment, sure, totally fine for me. Sometimes such arguments are framed like an attempt to shut down a question, or shift the conversation. To take this ad absurdum, you could go to a Linux forum and ask "Why don't you buy a Mac?". I might give smartgit a chance. But truth is I am already quite productive with my cli, I see this question as micro-optimization. – donquixote Aug 19 '19 at 14:23

1 Answers1

1

One workaround: Create an alias like so (in ~/.gitconfig):

[alias]
apd = "!git add -p; git diff --staged -U12"

At least this lets you see the staged diff without scrolling.

Any other answers are still welcome of course.

donquixote
  • 4,877
  • 3
  • 31
  • 54
  • Update: I've been using this new alias for a while, and I think it is a really good idea. In fact I renamed it "ap". I might even make it a bash alias, so I don't even have to type "git" anymore :) – donquixote Aug 18 '19 at 23:17
  • "don't even have to type 'git' anymore" -> this made me search, and I found this QA: https://stackoverflow.com/questions/56505000/how-do-i-avoid-typing-git-at-the-begining-of-every-git-command Now trying git-sh. – donquixote Aug 18 '19 at 23:22