Git Interactive Add
Git's interactive add feature is a powerful tool that allows you to selectively stage
changes, crafting precise commits tailored to your needs. We'll explore
various ways to use the git add -i
or git add --interactive
command, providing you with a comprehensive guide to efficiently stage changes and enhance
your version control workflow.
1. Launching Interactive Add:
To start the interactive add mode, use:
bashgit add -i
or
bashgit add --interactive
This opens an interactive prompt, presenting you with a menu of staging options.
2. Staging Modified Files:
Choose option 2
or enter u
to stage modified files interactively.
Git will display a list of modified files, and you can select which ones to stage
individually.
plaintextstaged unstaged path 1: unchanged +1/-1 README.md 2: unchanged +3/-3 src/main.py 3: unchanged +5/-1 src/utils.py 4: unchanged +2/-2 tests/test_main.py 5: unchanged +4/-1 tests/test_utils.py Update>> 2
3. Staging All Changes:
Choose option 1
or enter a
to stage all changes, both modified and
untracked. This is a quick way to stage everything at once.
plaintext*** Commands *** 1: add untracked 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit
4. Staging Untracked Files:
Choose option 4
or enter add untracked
to interactively stage
untracked files. Git will display a list of untracked files, and you can select which ones
to stage.
plaintextstaged unstaged path 1: unchanged +1/-1 README.md 2: unchanged +3/-3 src/main.py 3: unchanged +5/-1 src/utils.py 4: unchanged +2/-2 tests/test_main.py 5: unchanged +4/-1 tests/test_utils.py Update>> 4
5. Interactive Patching:
Choose option 5
or enter p
to interactively stage changes using
patch mode. Git will prompt you with each change, allowing you to choose whether to stage it
or not.
plaintext*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>> 5
6. Reverting Changes:
Choose option 3
or enter r
to interactively revert changes. Git
will display a list of modified files, and you can select which ones to revert.
plaintextstaged unstaged path 1: unchanged +1/-1 README.md 2: unchanged +3/-3 src/main.py 3: unchanged +5/-1 src/utils.py 4: unchanged +2/-2 tests/test_main.py 5: unchanged +4/-1 tests/test_utils.py Update>> 3
7. Differential Staging:
Choose option 6
or enter d
to see the diff for each file before
deciding whether to stage it or not.
plaintext*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>> 6
8. Quitting Interactive Add:
Choose option 7
or enter q
to quit the interactive add mode. Git
will display a confirmation prompt.
plaintext*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>> q
9. Help Menu:
Choose option 8
or enter h
to display the help menu, which lists
all available interactive add commands.
plaintext*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>> h
10. Adding Files with Specific Extensions:
Use the !
command followed by a filter expression to add files matching specific
criteria. For example, to add all .js
files:
plaintext*** Commands *** 1: status 2: update 3: revert 4: add untracked 5: patch 6: diff 7: quit 8: help What now>> !(*.js)
11. Splitting Changes into Hunks:
When using patch mode (option 5
), you can further split changes into smaller
chunks (hunks) for more granular staging.
plaintext*** Commands *** 1: yes 2: no 3: edit 4: add untracked 5: patch 6: diff 7: quit 8: help What now>> e
12. Staging Lines Within a File:
When in patch mode, you can stage individual lines within a file by selecting specific lines to add.
plaintext*** Commands *** 1: yes 2: no 3: edit 4: add untracked 5: patch 6: diff 7: quit 8: help What now>> 3