Sometimes, the usual git add . or git commit -am commands just don’t cut it. Perhaps you need to split changes across multiple commits, or you’re not ready to add everything yet. And who wants to add files individually, one at a time? That’s tedious. That’s where interactive adding comes in handy:
git add -i
Let’s walk through an example of using interactive mode. Imagine I’ve made some changes to the README.md file in the gitready project, along with another file. When you run the above command, you’ll get an output displaying the status of the index:
staged unstaged path
1: unchanged +3/-1 README.md
2: unchanged +1/-1 _layouts/default.html
*** Commands ***
1: status 2: update 3: revert 4: add untracked
5: patch 6: diff 7: quit 8: help
What now>
As you can see, there are several commands available. If you ever want to see this screen again, you can use the status command to bring it up. The +3/-1 indicates the number of lines added/removed, similar to what you see during a pull.
Now, let’s add the changes made to the README.md file. Using the update command allows us to do this. After selecting this command, you’ll see:
staged unstaged path
1: unchanged +3/-1 README.md
2: unchanged +1/-1 _layouts/default.html
Update>>
If you choose to update file 1, you’ll be notified that the file is now staged for commit. Checking the status again will confirm that the README.md file has been properly staged:
staged unstaged path
1: +3/-1 nothing README.md
2: unchanged +1/-1 _layouts/default.html
Once you’re finished, you can exit using the quit command and proceed to commit your work. If you’re unsure about the changes added, running git status will show that only the README.md file has been staged for the commit:
bash
On branch master
Changes to be committed:
#
modified: README.md
#
Changed but not updated:
#
modified: _layouts/default.html
There are many other helpful commands available in interactive adding; this is just the beginning. You can explore more options using the help command. Stay tuned for more tips in the future covering other commands available in interactive mode.