10

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 ?

10

There 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.

  1. 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.

  2. While in original window (right or bottom one), perform visual selection of changes you'd like to git add.

  3. Run :diffput command, which can operate on a range. (dp key doesn't work in Visual mode.)

  4. 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.

4

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:

  1. Run the :Magit command to open a new buffer that displays all the changed hunks in your repository (not just in one file),

  2. Locate the lines you want to stage and perform your visual selection,

  3. Press the "stage" mapping: S.

4

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.

  • I'm using :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. – Luc Hermitte Jan 25 '17 at 12:23
3

Stage Complete or Partial Hunks with vim-gitgutter

  1. Stage Complete Hunk

    • <Leader>hs or
    • :GitGutterStageHunk
  2. 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
  3. Stage Part of any Hunk via Preview Window (since 8/2019)

    • Preview the hunk, e.g. <Leader>hp
    • Move to the preview window, e.g. :wincmd P
    • Delete the lines you do not want to stage
    • Stage the remaining lines, e.g. <Leader>hs or :GitGutterStageHunk

Updated: 17.8.2019 due to recent commits bc1d28d and b036fcf

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.