git add --patch
is an integral part of my Git workflow. The only thing which irritates me about it is constantly having to press the Enter key after each y
or n
. Is there any way to make Git accept the answer to a question with just a single y
or n
keystroke?
What is “git add -patch” git add -p is short for git add --patch and it is a git option that allows you to make more specific commits. How it works is that it will go through all new changes in your code and display hunks of them at a time for you to decide what you would like to stage or not stage.
git add -p is basically "git add partial (or patch)" Patch mode allows you to stage parts of a changed file, instead of the entire file. This allows you to make concise, well-crafted commits that make for an easier to read history. This feature can improve the quality of the commits.
This opens an interactive prompt that allows you to look at the diffs and let you decide whether you want to include them or not. Stage this hunk [y,n,q,a,d,/,s,e,?]? y stage this hunk for the next commit. n do not stage this hunk for the next commit. q quit; do not stage this hunk or any of the remaining hunks.
git add –patch. With Git, on the other hand, you first add all the changes you want to be in the next commit to the index via git add (or remove a file withgit rm). Normally, calling git add <file> will add all the changes in that file to the index, but add supports an interesting option: --patch, or -p for short.
git add –interactive --interactive (or -i) is the big brother of --patch. --patch only lets you decide about the individual hunks in files. --interactive enters the interactive mode, and is a bit more powerful. So powerful that it has its own little submenu:
We can create a patch of uncommitted changes by using the Git Diff command. We need to use the Git Format-Patch command to create a patch that contains committed changes. The format-patch command is the preferred way of creating patches.
The git add command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add will fail with a list of ignored files.
That would be the Git configuration option interactive.singleKey
.
interactive.singleKey
In interactive commands, allow the user to provide one-letter input with a single key (i.e., without hitting enter). Currently this is used by the
--patch
mode ofgit-add(1)
,git-checkout(1)
,git-commit(1)
,git-reset(1)
, andgit-stash(1)
. Note
that this setting is silently ignored if portable keystroke input is not available; requires the Perl moduleTerm::ReadKey
.
That is, in your .gitconfig
or equivalent file, add:
[interactive]
singleKey = true
Or run git config [--global] interactive.singleKey yes
, if you like yes
better than true
and commands better than editors.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With