I would like to know if i could select some code in visual mode and git add
it ?
I checked with fugitive.vim but i didn't find how to do it.
Is it possible ? Or is there any other plugin to do it ?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityThere might be other ways, but this approach lets you to do more than adding a chunk, which is why I tend to use it quite a lot.
Run :Gdiff
command. It will open a split with version of current file that's currently in the index to the left/top of the original window.
While in original window (right or bottom one), perform visual selection of changes you'd like to git add
.
Run :diffput
command, which can operate on a range. (dp
key doesn't work in Visual mode.)
Close that second window saving changes to update file in the index. Here you can edit it the way you like before saving, it doesn't have to exactly match original or new version of the file.
Mind that :diffput
might not always give you what you would expect depending on the structure of changes (especially if you're committing only part of contiguous block of changes). So don't forget to validate commit. In most practical situations it works fine though.
fugitive.vim now allows for staging a selection of a hunk with visual mode.
:Git
(or :G
)>
(or =
to toggle). This will only show the changed hunks plus some extra lines of context above and below the changed lines.v
to start visual mode and select the lines you want to stage with hjkl
s
to stage the visual selection (or -
to toggle)cc
to commit:help fugitive-staging-maps
. Or more generally, :h fugitive
. It's so awesome, it should be illegal ;)
Vimcasts has a great series on Fugitive.vim. The episode Fugitive.vim - working with the git index would be the the most helpful for your question.
You can use :Gdiff
to stage only portions of a file. Running :Gdiff
will show you the current file "diff-ed" with the stage/index. You can simply use Vim's diff commands (dp
, do
, :diffput
, and :diffget
) to move the hunk to the stage/index. Then you write the stage/index file like you would any other buffer, :w
/:x
.
:Gstatus
+ D
to do the same with fugitive. This way I can also pick the file I'm operating on among the list of files that have been modified.
Jan 25, 2017 at 12:23
vim-gitgutter
Stage Complete Hunk
<Leader>hs
or:GitGutterStageHunk
Stage Part of an Additions-only Hunk (since 8/2019)
Visual mode
{Visual}<Leader>hs
:'<,'>GitGutterStageHunk
'<,'>
are as always inserted for you)Range
:42,45GitGutterStageHunk
Stage Part of any Hunk via Preview Window (since 8/2019)
<Leader>hp
:wincmd P
<Leader>hs
or :GitGutterStageHunk
Updated: 17.8.2019 due to recent commits bc1d28d and b036fcf
As you specifically asked about solutions that use other plugins, I'll note that you can also stage visual selections using vimagit, which is a Git plugin (based on the other editor's popular Magit plugin) centered around viewing, staging, and commiting changes.
To stage a visual selection using Vimagit:
Run the :Magit
command to open a new buffer that displays all the changed hunks in your repository (not just in one file),
Locate the lines you want to stage and perform your visual
selection,
Press the "stage" mapping: S
.