Skip to content

Translate 07-git-tools interactive-staging v2 #164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Aug 14, 2015
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
74 changes: 37 additions & 37 deletions book/07-git-tools/sections/interactive-staging.asc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
[[_interactive_staging]]
=== Interactive Staging
=== 交互式暂存

Git comes with a couple of scripts that make some command-line tasks easier.
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.
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.
This way, you can make sure your commits are logically separate changesets and can be easily reviewed by the developers working with you.
If you run `git add` with the `-i` or `--interactive` option, Git goes into an interactive shell mode, displaying something like this:
Git 自带的一些脚本可以使在命令行下工作更容易。
本节的几个互交命令可以帮助你将文件的特定部分组合成提交。
当你修改一组文件后,希望这些改动能放到若干提交而不是混杂在一起成为一个提交时,这几个工具会非常有用。
通过这种方式,可以确保提交是逻辑上独立的变更集,同时也会使其他开发者在与你工作时很容易地审核。
如果运行 `git adds` 时使用 `-i` 或者 `--interactive` 选项,Git 将会进入一个交互式终端模式,显示类似下面的东西:

[source,console]
----
Expand All @@ -21,15 +21,15 @@ $ git add -i
What now>
----

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.
It lists the changes you’ve staged on the left and unstaged changes on the right.
可以看到这个命令以非常不同的视图显示了暂存区 - 基本上与 `git status` 是相同的信息,但是更简明扼要一些。
它将暂存的修改列在左侧,未暂存的修改列在右侧。

After this comes a Commands section.
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.
在这块区域后是命令区域。
在这里你可以做一些工作,包括暂存文件、取消暂存文件、暂存文件的一部分、添加未被追踪的文件、查看暂存内容的区别。

==== Staging and Unstaging Files
==== 暂存与取消暂存文件

If you type `2` or `u` at the `What now>` prompt, the script prompts you for which files you want to stage:
如果在 `What now>` 提示符后键入 `2` 或 `u`,脚本将会提示想要暂存哪个文件:

[source,console]
----
Expand All @@ -41,7 +41,7 @@ What now> 2
Update>>
----

To stage the TODO and index.html files, you can type the numbers:
要暂存 TODO index.html 文件,可以输入数字:

[source,console]
----
Expand All @@ -53,8 +53,8 @@ Update>> 1,2
Update>>
----

The `*` next to each file means the file is selected to be staged.
If you press Enter after typing nothing at the `Update>>` prompt, Git takes anything selected and stages it for you:
每个文件前面的 `*` 意味着选中的文件将会被暂存。
如果在 `Update>>` 提示符后不输入任何东西并直接按回车,Git 将会暂存之前选择的文件:

[source,console]
----
Expand All @@ -71,8 +71,8 @@ What now> 1
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:
现在可以看到 TODO index.html 文件已经被暂存而 simplegit.rb 文件还未被暂存。
如果这时想要取消暂存 TODO 文件,使用 `3` `r`(撤消)选项:

[source,console]
----
Expand All @@ -93,7 +93,7 @@ Revert>> [enter]
reverted one path
----

Looking at your Git status again, you can see that you’ve unstaged the TODO file:
再次查看 Git 状态,可以看到已经取消暂存 TODO 文件:

[source,console]
----
Expand All @@ -107,9 +107,9 @@ What now> 1
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.
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:
如果想要查看已暂存内容的区别,可以使用 `6` `d`(区别)命令。
它会显示暂存文件的一个列表,可以从中选择想要查看的暂存区别。
这跟你在命令行指定 `git diff --cached` 非常相似:

[source,console]
----
Expand All @@ -134,14 +134,14 @@ index 4d07108..4335f49 100644
<script type="text/javascript">
----

With these basic commands, you can use the interactive add mode to deal with your staging area a little more easily.
通过这些基本命令,可以使用交互式添加模式来轻松地处理暂存区。

==== Staging Patches
==== 暂存补丁

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 interactive prompt, type `5` or `p` (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:
Git 也可以暂存文件的特定部分。
例如,如果在 simplegit.rb 文件中做了两处修改,但只想要暂存其中的一个而不是另一个,Git 会帮你轻松地完成。
从交互式提示符中,输入 `5` `p`(补丁)。
Git 会询问你想要部分暂存哪些文件;然后,对已选择文件的每一个部分,它都会一个个地显示文件区别并询问你是否想要暂存它们:

[source,console]
----
Expand All @@ -161,8 +161,8 @@ index dd5ecc4..57399e0 100644
Stage this hunk [y,n,a,d,/,j,J,g,e,?]?
----

You have a lot of options at this point.
Typing `?` shows a list of what you can do:
这时有很多选项。
输入 `?` 显示所有可以使用的命令列表:

[source,console]
----
Expand All @@ -182,8 +182,8 @@ e - manually edit the current hunk
? - print help
----

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.
If you stage one part of the file and leave another part unstaged, your status output will look like this:
通常情况下可以输入 y 或 n 来选择是否要暂存每一个区块,当然,暂存特定文件中的所有部分或为之后的选择跳过一个区块也是非常有用的。
如果你只暂存文件的一部分,状态输出可能会像下面这样:

[source,console]
----
Expand All @@ -194,11 +194,11 @@ What now> 1
3: +1/-1 +4/-0 lib/simplegit.rb
----

The status of the simplegit.rb file is interesting.
It shows you that a couple of lines are staged and a couple are unstaged.
You’ve partially staged this file.
At this point, you can exit the interactive adding script and run `git commit` to commit the partially staged files.
simplegit.rb 文件的状态很有趣。
它显示出若干行被暂存与若干行未被暂存。
已经部分地暂存了这个文件。
在这时,可以退出交互式添加脚本并且运行 `git commit` 来提交部分暂存的文件。

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.
也可以不必在交互式添加模式中做部分文件暂存 - 可以在命令行中使用 `git add -p` `git add --patch` 来启动同样的脚本。

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.
更进一步地,可以使用 `reset --patch` 命令的补丁模式来部分重置文件。通过 `checkout --patch` 命令来部分检出文件与 `stash save --patch` 命令来部分暂存文件。我们将会在接触这些命令的高级使用方法时了解更多详细信息。