You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/07-git-tools/sections/interactive-staging.asc
+37-37
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
1
[[_interactive_staging]]
2
-
=== Interactive Staging
2
+
=== 交互式暂存
3
3
4
-
Git comes with a couple of scripts that make some command-line tasks easier.
5
-
Here, you’ll look at a few interactive commands that can help you easily craft your commits to include only certain combinations and parts of files.
6
-
These tools are very helpful if you modify a bunch of files and then decide that you want those changes to be in several focused commits rather than one big messy commit.
7
-
This way, you can make sure your commits are logically separate changesets and can be easily reviewed by the developers working with you.
8
-
If you run `git add` with the `-i` or `--interactive` option, Git goes into an interactive shell mode, displaying something like this:
You can see that this command shows you a much different view of your staging area – basically the same information you get with `git status` but a bit more succinct and informative.
25
-
It lists the changes you’ve staged on the left and unstaged changes on the right.
Here you can do a number of things, including staging files, unstaging files, staging parts of files, adding untracked files, and seeing diffs of what has been staged.
If you type `2` or `u` at the `What now>` prompt, the script prompts you for which files you want to stage:
32
+
如果在 `What now>` 提示符后键入 `2` 或 `u`,脚本将会提示想要暂存哪个文件:
33
33
34
34
[source,console]
35
35
----
@@ -41,7 +41,7 @@ What now> 2
41
41
Update>>
42
42
----
43
43
44
-
To stage the TODO and index.html files, you can type the numbers:
44
+
要暂存 TODO 与 index.html 文件,可以输入数字:
45
45
46
46
[source,console]
47
47
----
@@ -53,8 +53,8 @@ Update>> 1,2
53
53
Update>>
54
54
----
55
55
56
-
The `*` next to each file means the file is selected to be staged.
57
-
If you press Enter after typing nothing at the `Update>>` prompt, Git takes anything selected and stages it for you:
56
+
每个文件前面的 `*` 意味着选中的文件将会被暂存。
57
+
如果在 `Update>>` 提示符后不输入任何东西并直接按回车,Git 将会暂存之前选择的文件:
58
58
59
59
[source,console]
60
60
----
@@ -71,8 +71,8 @@ What now> 1
71
71
3: unchanged +5/-1 lib/simplegit.rb
72
72
----
73
73
74
-
Now you can see that the TODO and index.html files are staged and the simplegit.rb file is still unstaged.
75
-
If you want to unstage the TODO file at this point, you use the `3` or `r` (for revert) option:
74
+
现在可以看到 TODO 与 index.html 文件已经被暂存而 simplegit.rb 文件还未被暂存。
75
+
如果这时想要取消暂存 TODO 文件,使用 `3` 或 `r`(撤消)选项:
76
76
77
77
[source,console]
78
78
----
@@ -93,7 +93,7 @@ Revert>> [enter]
93
93
reverted one path
94
94
----
95
95
96
-
Looking at your Git status again, you can see that you’ve unstaged the TODO file:
96
+
再次查看 Git 状态,可以看到已经取消暂存 TODO 文件:
97
97
98
98
[source,console]
99
99
----
@@ -107,9 +107,9 @@ What now> 1
107
107
3: unchanged +5/-1 lib/simplegit.rb
108
108
----
109
109
110
-
To see the diff of what you’ve staged, you can use the `6` or `d` (for diff) command.
111
-
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.
112
-
This is much like specifying `git diff --cached` on the command line:
110
+
如果想要查看已暂存内容的区别,可以使用 `6` 或 `d`(区别)命令。
111
+
它会显示暂存文件的一个列表,可以从中选择想要查看的暂存区别。
112
+
这跟你在命令行指定 `git diff --cached` 非常相似:
113
113
114
114
[source,console]
115
115
----
@@ -134,14 +134,14 @@ index 4d07108..4335f49 100644
134
134
<script type="text/javascript">
135
135
----
136
136
137
-
With these basic commands, you can use the interactive add mode to deal with your staging area a little more easily.
137
+
通过这些基本命令,可以使用交互式添加模式来轻松地处理暂存区。
138
138
139
-
==== Staging Patches
139
+
==== 暂存补丁
140
140
141
-
It’s also possible for Git to stage certain parts of files and not the rest.
142
-
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.
143
-
From the interactive prompt, type `5` or `p` (for patch).
144
-
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:
@@ -182,8 +182,8 @@ e - manually edit the current hunk
182
182
? - print help
183
183
----
184
184
185
-
Generally, you’ll type `y` or `n` if you want to stage each hunk, but staging all of them in certain files or skipping a hunk decision until later can be helpful too.
186
-
If you stage one part of the file and leave another part unstaged, your status output will look like this:
185
+
通常情况下可以输入 y 或 n 来选择是否要暂存每一个区块,当然,暂存特定文件中的所有部分或为之后的选择跳过一个区块也是非常有用的。
186
+
如果你只暂存文件的一部分,状态输出可能会像下面这样:
187
187
188
188
[source,console]
189
189
----
@@ -194,11 +194,11 @@ What now> 1
194
194
3: +1/-1 +4/-0 lib/simplegit.rb
195
195
----
196
196
197
-
The status of the simplegit.rb file is interesting.
198
-
It shows you that a couple of lines are staged and a couple are unstaged.
199
-
You’ve partially staged this file.
200
-
At this point, you can exit the interactive adding script and run `git commit` to commit the partially staged files.
197
+
simplegit.rb 文件的状态很有趣。
198
+
它显示出若干行被暂存与若干行未被暂存。
199
+
已经部分地暂存了这个文件。
200
+
在这时,可以退出交互式添加脚本并且运行 `git commit` 来提交部分暂存的文件。
201
201
202
-
You also don’t need to be in interactive add mode to do the partial-file staging – you can start the same script by using `git add -p` or `git add --patch` on the command line.
Furthermore, you can use patch mode for partially resetting files with the `reset --patch` command, for checking out parts of files with the `checkout --patch` command and for stashing parts of files with the `stash save --patch` command. We'll go into more details on each of these as we get to more advanced usages of these commands.
0 commit comments