32

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 ?

5 Answers 5

28

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.

37

fugitive.vim now allows for staging a selection of a hunk with visual mode.

  1. Open the git summary with :Git (or :G)
  2. Expand the file which contains the lines you want to stage with > (or = to toggle). This will only show the changed hunks plus some extra lines of context above and below the changed lines.
  3. v to start visual mode and select the lines you want to stage with hjkl
  4. s to stage the visual selection (or - to toggle)
  5. Repeat 2-4 as needed
  6. cc to commit
2
  • Where did you find this? Dec 15, 2021 at 2:18
  • 3
    @WongJiaHau in the fugitive help files: :help fugitive-staging-maps. Or more generally, :h fugitive. It's so awesome, it should be illegal ;)
    – Kaio
    Dec 15, 2021 at 15:34
13

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.

1
  • 1
    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. Jan 25, 2017 at 12:23
12

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

1
  • 2
    It asked for vim-fugitive, not vim-gitgutter
    – giggi__
    May 12, 2020 at 15:37
7

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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