git add -i
(or --interactive
) will give you an interactive interface where you can edit the index, to prepare what you want to have in the next commit. You can add and remove changes to whole files, add untracked files and remove files from being tracked, but also select subsection of changes to put in the index, by selecting chunks of changes to be added, splitting those chunks, or even editing the diff. Many graphical commit tools for Git (like e.g. git gui
) include such feature; this might be easier to use than the command line version.
It is very useful (1) if you have entangled changes in the working directory that you want to put in separate commits, and not all in one single commit (2) if you are in the middle of an interactive rebase and want to split too large commit.
$ git add -i
staged unstaged path
1: unchanged +4/-4 index.js
2: +1/-0 nothing package.json
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
The top half of this output shows the current state of the index broken up into staged and unstaged columns:
index.js
has had 4 lines added and 4 lines removed. It is currently not staged, as the current status reports "unchanged." When this file becomes staged, the +4/-4
bit will be transferred to the staged column and the unstaged column will read "nothing."package.json
has had one line added and has been staged. There are no further changes since it has been staged as indicated by the "nothing" line under the unstaged column.The bottom half shows what you can do. Either enter a number (1-8) or a letter (s
, u
, r
, a
, p
, d
, q
, h
).
status
shows output identical to the top part of the output above.
update
allows you to make further changes to the staged commits with additional syntax.
revert
will revert the staged commit information back to HEAD.
add untracked
allows you to add filepaths previously untracked by version control.
patch
allows for one path to be selected out of an output similar to status
for further analysis.
diff
displays what will be committed.
quit
exits the command.
help
presents further help on using this command.