Using an Editor To Stage Files In Git

This post describes script git-edit-index that allows you to open an editor to stage or unstage files in Git, just like when you perform an interactive rebase. It thus represents a faster alternative to git add -i or git gui.

Motivation

Lets assume you have the following three modified files (git status --short):

M path/to/file1
M another/path/to/file2
M yet/another/path/to/file3

Now, you would like to stage (add) the first two so you can commit them. Git gives you the following options:

  • Give them as arguments to git add. This is tedious because you have to copy and paste them after git add. Or you can type them, but this is error-prone.
  • Use interactive staging (git add -i). After you run the command, Git opens an interactive shell mode, in which you can stage or unstage files. This way, you do not have to perform any copy and pastes. Still, it is not as fast as you would like because you have to press a lot of keys to stage the files.
  • Use a GUI. Of course, you can use a GUI (such as git gui). However, you have to usually use a mouse to stage the files, which slows you down.

Is there a faster way? Recall that when you perform interactive rebasing, Git opens up an editor and lets you specify the changes in there. This is very fast and convenient. What if there was a command that does the same thing for staging and unstaging of files? Sadly, there appears to be no such standard command.

git edit-index

Here comes git-edit-index, a script that I have written. It allows you to perform staging in an editor, just like when you perform an interactive rebase. The script works as follows.

After running git edit-index (notice the space after git), an editor will show up with the following text:

M path/to/file1
M another/path/to/file2
M yet/another/path/to/file3

For example, to stage (add) the first two files, simply change the first two Ms to A:

A path/to/file1
A another/path/to/file2
M yet/another/path/to/file3

And that is it. After you save and close the editor, the files are staged. Nice and fast.

To unstage a file, change A to M. It should be noted that the status is case-insensitive, e.g. both A and a stage the given file (lower-case letters are easier to type).

Selecting an Editor

The editor can be specified either by setting core.editor in your Git config:

git config --global core.editor "gvim -f"

or by setting the EDITOR environment variable in your shell:

export EDITOR="gvim -f"

Adding an Alias to .gitconfig

Of course, instead of typing git edit-index, you can setup a git alias:

git config --global alias.ei edit-index

Then, all you have to do is to type git ei.

More Details

For more details, see the script’s homepage.

2 Comments

  1. As you’re a vim user already, you might enjoy fugitive.vim [1] instead. In :Gstatus, a single ‘-‘ on the line with the file of interest turns the file from changed to staged or back. Even easier than changing “M” to “a”.

    And it allows you to do range-based staging from editor through :Gdiff :-)

    [1] https://github.com/tpope/vim-fugitive

    Reply

Leave a Reply to Petr Zemek Cancel reply