The git add -i
command enables the interactive staging mode
in Git, a free and open-source distributed version control system. In interactive mode, you can select precisely which changes and modifications you want to commit, providing a high level of control over the commit process.
Git's interactive staging mode lets you select portions of the files you've changed to add to the next commit. Unlike the typical git add <filename>
command that stages all changes in a file, the interactive mode ('-i' stands for 'interactive') allows you to stage parts of the file, even down to the lines of code.
We might use this when editing a file to satisfy multiple different tasks and want to commit the changes for each task separately. By using git add -i
, we can control which changes in the file are staged for commit, allowing us to create more structured and coherent commit histories.
A common workflow with the command might look like this:
$ git add -i
staged unstaged path
1: unchanged +1/-1 README.md
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now> 5
In this case, selecting '5' (patch) will let you stage hunks of changes from the listed files.
It's important to remember that git add -i
is a command-line feature and not a part of Git's core feature set, meaning its existence and function may vary depending on the Git client you are using.
As a best practice, it is recommended to make use of git add -i
when you want to make commits that are logically separate but touch the same file. This method of staging provides you with direct control over which modifications are included in your commit, allowing you to maintain a clean and uncluttered commit history. However, for smaller changes or less complex projects, using the traditional git add
command is typically sufficient.
In conclusion, git add -i
is a powerful tool that can help improve your commit organization and the overall legibility of your project history. Just like any tool, to make the most out of it, you need to understand when and how to use it appropriately.