Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions book/07-git-tools/sections/interactive-staging.asc
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[[_interactive_staging]]
=== Interactive Staging

In this section, you’ll look at a few interactive Git commands that can help you easily craft your commits to include only certain combinations and parts of files.
These tools are helpful if you modify a number of files and then decide that you want those changes to be in several focused commits rather than one big messy commit.
This way, you can make sure your commits are logically separate changesets and can be easily reviewed by the developers working with you.
In this section, you’ll look at a few interactive Git commands that can help you craft your commits to include only certain combinations and parts of files.
These tools are helpful if you modify a number of files extensively, then decide that you want those changes to be partitioned into several focused commits rather than one big messy commit.
This way, you can make sure your commits are logically separate changesets and can be reviewed easily by the developers working with you.

If you run `git add` with the `-i` or `--interactive` option, Git enters an interactive shell mode, displaying something like this:

Expand All @@ -28,11 +28,11 @@ After this comes a ``Commands'' section, which allows you to do a number of thin

==== Staging and Unstaging Files

If you type `2` or `u` at the `What now>` prompt, you're prompted for which files you want to stage:
If you type `u` or `2` (for update) at the `What now>` prompt, you're prompted for which files you want to stage:

[source,console]
----
What now> 2
What now> u
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
Expand Down Expand Up @@ -63,22 +63,22 @@ updated 2 paths
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> 1
What now> s
staged unstaged path
1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
----

Now you can see that the `TODO` and `index.html` files are staged and the `simplegit.rb` file is still unstaged.
If you want to unstage the `TODO` file at this point, you use the `3` or `r` (for revert) option:
If you want to unstage the `TODO` file at this point, you use the `r` or `3` (for revert) option:

[source,console]
----
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> 3
What now> r
staged unstaged path
1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
Expand All @@ -99,14 +99,14 @@ Looking at your Git status again, you can see that you’ve unstaged the `TODO`
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> 1
What now> s
staged unstaged path
1: unchanged +0/-1 TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
----

To see the diff of what you’ve staged, you can use the `6` or `d` (for diff) command.
To see the diff of what you’ve staged, you can use the `d` or `6` (for diff) command.
It shows you a list of your staged files, and you can select the ones for which you would like to see the staged diff.
This is much like specifying `git diff --cached` on the command line:

Expand All @@ -115,7 +115,7 @@ This is much like specifying `git diff --cached` on the command line:
*** Commands ***
1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
What now> 6
What now> d
staged unstaged path
1: +1/-1 nothing index.html
Review diff>> 1
Expand All @@ -139,7 +139,7 @@ With these basic commands, you can use the interactive add mode to deal with you

It’s also possible for Git to stage certain _parts_ of files and not the rest.
For example, if you make two changes to your `simplegit.rb` file and want to stage one of them and not the other, doing so is very easy in Git.
From the same interactive prompt explained in the previous section, type `5` or `p` (for patch).
From the same interactive prompt explained in the previous section, type `p` or `5` (for patch).
Git will ask you which files you would like to partially stage; then, for each section of the selected files, it will display hunks of the file diff and ask if you would like to stage them, one by one:

[source,console]
Expand Down