If you just started with git, you probably have no concept of the stage (or as some people confusingly call it index). In other version control systems, the only thing you can select is which files you want to commit. However, git not only allows you to do that but also which parts of a file you want to select.
Unfortunately, the built-in command line tooling for that (provided via
git add -p
) is pretty hard to use and takes way too long.
Full a quick demo, check out my video on YouTube:
When you run git istage
inside your repo, it shows you the set of current
changes, i.e. the output of git diff
. But in contrast to git diff
, you can
select individual lines and add them to the stage by hitting S. You
can also permanently remove changes from your working copy by hitting
R (be careful: there is no undo for that operation).
At any point you can view the staged changes (i.e. the output of
git diff --cached
). If you decided that there are certain portions you don't
want to commit, you simply unstage the selected lines by hitting U.
All three shortcuts can be modified using Shift. In that case it will apply to all consecutive changes, called a block.
In order to quickly navigated you can use ← and →, which will jump between files, and [ and ], which will jump from block to block.
You can also customize the way the changes are presented. By using + and - to increase or decrease the number of contextual lines being displayed. Alternatively, you can use \ to toggle between viewing entire files or changes only.
When you are done, simply return to the command line by hitting Esc
or Q. Once there, I recommend you run git stash -u -k
, which will
stash all changes you didn't stage so you can test the code you're about to
commit. Or, you can stash right from inside of git istage by hitting
Alt S. Once satisfied, run git commit
and unstash via
git stash pop
.
More keyboard shortcuts listed below. A shortcut may be preceded by a number, N. Notes in parentheses indicate the behavior if N is given.
Shortcut | Description |
---|---|
Esc | Return to command line. |
Q | Return to command line. |
C | Author commit |
Alt C | Authors commit with --amend option |
Alt S | Stashes changes from the working copy, but leaves the stage as-is. |
T | Toggle between working copy changes and staged changes. |
R | When viewing the working copy, removes the selected line from the working copy. |
S | When viewing the working copy, stages the selected line. |
U | When viewing the stage, unstages the selected line. |
Shift R | When viewing the working copy, removes the selected block from the working copy. |
Shift S | When viewing the working copy, stages the selected block. |
Shift U | When viewing the stage, unstages the selected block. |
+ | Increases the number of contextual lines. |
- | Decreases the number of contextual lines. |
\ | Toggles between viewing entire files and changes only. |
W | Toggles between showing and hiding whitespace. |
↑ | Selects the previous line. |
K | Selects the previous line. |
↓ | Selects the next line. |
J | Selects the next line. |
← | Go to the previous file. |
→ | Go to the next file. |
[ | Go to previous change block. |
] | Go to next change block. |
PgUp | Selects the line one screen above. |
PgDown | Selects the line one screen below. |
Space | Selects the line one screen below. |
Home | Selects the first line. |
G | Selects the first line (or Line N). |
End | Selects the last line. |
Shift G | Selects the last line (or Line N). |
Ctrl PgUp | Scrolls up by one screen. |
Ctrl PgDown | Scrolls down by one screen. |
Ctrl ← | Scrolls left by one character, |
Ctrl → | Scrolls right by one character, |
Ctrl ↑ | Scrolls up by one line. |
Ctrl ↓ | Scrolls down by one line. |