Git Tools-Interactive Staging
Several interactive Git commands in this section can help you combine specific parts of files into commits.These tools are very useful when you have modified a large number of files and want the changes to be split into several commits instead of being mixed together into one commit.This way you ensure that commits are logically separate sets of changes, while also making it easy for other developers to review while working with you.If you run git add with the -i or --interactive option, Git will enter an interactive terminal mode, displaying something similar to the following:
$ git add -i
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
*** 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>You can see that this command displays the staging area in a very different view than usual - basically the same information as git status, but more concisely.It lists staged modifications on the left and unstaged modifications on the right.
After this area is the "Commands" command area.Here you can do some work, including staging files, unstaging files, staging parts of files, adding untracked files, and showing differences in staging content.
Temporary and unstaged files
If you type u or 2 (update) after the What now> prompt, it will ask you which file you want to stage:
What now> u
staged unstaged path
1: unchanged +0/-1 TODO
2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
Update>>To stage the TODO and index.html files, you can enter a number:
Update>> 1,2
staged unstaged path
* 1: unchanged +0/-1 TODO
* 2: unchanged +1/-1 index.html
3: unchanged +5/-1 lib/simplegit.rb
Update>>The * in front of each file means that the selected file will be staged.If you press Enter without typing anything after the Update>> prompt, Git will stage the previously selected file:
Update>>
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> s
staged unstaged path
1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rbYou can now see that the TODO and index.html files have been staged but the simplegit.rb file has not yet been staged.If you want to unstage the TODO file at this time, use the r or 3 (undo) option:
*** 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> r
staged unstaged path
1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
Revert>> 1
staged unstaged path
* 1: +0/-1 nothing TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rb
Revert>> [enter]
reverted one pathCheck the Git status again and you can see that the TODO file has been unstaged:
*** 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> s
staged unstaged path
1: unchanged +0/-1 TODO
2: +1/-1 nothing index.html
3: unchanged +5/-1 lib/simplegit.rbIf you want to see the differences between staged content, you can use the d or 6 (diff) command.It will display a list of staging files from which you can select the staging differences you want to view.This is very similar to when you specify 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> d
staged unstaged path
1: +1/-1 nothing index.html
Review diff>> 1
diff --git a/index.html b/index.html
index 4d07108..4335f49 100644
--- a/index.html
+++ b/index.html
@@ -16,7 +16,7 @@ Date Finder
<p id="out">...</p>
-<div id="footer">contact : support@github.com</div>
+<div id="footer">contact : email.support@github.com</div>
<script type="text/javascript">These basic commands make it easy to work with staging areas using interactive add mode.
Staging patch
In order to facilitate development and synchronization on multiple machines, you can generate a patch and then apply the patch.
git diff > ../sync.patch # 生成补丁
git apply ../sync.patch # 打补丁
git apply --check ../sync.patch #测试补丁能否成功Git can also stage specific portions of files.For example, if you make two changes in the simplegit.rb file but only want to stage one but not the other, Git will make it easy for you.In the same interactive prompt as in the previous section, enter p or 5 (patch).Git will ask you which files you want to partially stage; then, for each section of the selected files, it will display the file differences one by one and ask you if you want to stage them:
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index dd5ecc4..57399e0 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -22,7 +22,7 @@ class SimpleGit
end
def log(treeish = 'master')
- command("git log -n 25 #{treeish}")
+ command("git log -n 30 #{treeish}")
end
def blame(path)
Stage this hunk [y,n,a,d,/,j,J,g,e,?]?There are many options at this point.Enter ? to display a list of all available commands:
Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print helpTypically you enter y or n to select whether to stage each chunk, although it is also useful to stage all parts of a specific file or skip a chunk for later selections.If you stage only part of the file, the status output might look like this:
What now> 1
staged unstaged path
1: unchanged +0/-1 TODO
2: +1/-1 nothing index.html
3: +1/-1 +4/-0 lib/simplegit.rbThe state of the simplegit.rb file is interesting.It shows how many rows are staged and how many lines are not staged.This file has been partially staged.At this point, you can exit the interactive add script and run git commit to commit the partially staged files.
You don't have to do partial file staging in interactive add mode - you can start the same script from the command line using git add -p or git add --patch.
Furthermore, you can use the patch mode of the git reset --patch command to partially reset files, the git checkout --patch command to partially check out files, and the git stash save --patch command to partially stage files.We'll learn more details when we get into advanced usage of these commands.