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/stashing-cleaning.asc
+47-47Lines changed: 47 additions & 47 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
[[_git_stashing]]
2
-
=== Stashing and Cleaning
2
+
=== 储藏与清理
3
3
4
-
Often, when you’ve been working on part of your project, things are in a messy state and you want to switch branches for a bit to work on something else.
5
-
The problem is, you don’t want to do a commit of half-done work just so you can get back to this point later.
6
-
The answer to this issue is the `git stash` command.
Stashing takes the dirty state of your working directory – that is, your modified tracked files and staged changes – and saves it on a stack of unfinished changes that you can reapply at any time.
You can see that Git re-modifies the files you reverted when you saved the stash.
80
-
In this case, you had a clean working directory when you tried to apply the stash, and you tried to apply it on the same branch you saved it from; but having a clean working directory and applying it on the same branch aren’t necessary to successfully apply a stash.
81
-
You can save a stash on one branch, switch to another branch later, and try to reapply the changes.
82
-
You can also have modified and uncommitted files in your working directory when you apply a stash – Git gives you merge conflicts if anything no longer applies cleanly.
You can also run `git stash pop` to apply the stash and then immediately drop it from your stack.
117
+
也可以运行 `git stash pop` 来应用储藏然后立即从栈上扔掉它。
118
118
119
-
==== Creative Stashing
119
+
==== 创造性的储藏
120
120
121
-
There are a few stash variants that may also be helpful. The first option that is quite popular is the `--keep-index` option to the `stash save` command. This tells Git to not stash anything that you've already staged with the `git add` command.
This can be really helpful if you've made a number of changes but want to only commit some of them and then come back to the rest of the changes at a later time.
123
+
当你做了几个改动并只想提交其中的一部分,过一会儿再回来处理剩余改动时,这个功能会很有用。
124
124
125
125
[source,console]
126
126
----
@@ -136,7 +136,7 @@ $ git status -s
136
136
M index.html
137
137
----
138
138
139
-
Another common thing you may want to do with stash is to stash the untracked files as well as the tracked ones. By default, `git stash` will only store files that are already in the index. If you specify `--include-untracked` or `-u`, Git will also stash any untracked files you have created.
Finally, if you specify the `--patch` flag, Git will not stash everything that is modified but will instead prompt you interactively which of the changes you would like to stash and which you would like to keep in your working directly.
@@ -179,11 +179,11 @@ Saved working directory and index state WIP on master: 1b65b17 added the index f
179
179
----
180
180
181
181
182
-
==== Creating a Branch from a Stash
182
+
==== 从储藏创建一个分支
183
183
184
-
If you stash some work, leave it there for a while, and continue on the branch from which you stashed the work, you may have a problem reapplying the work.
185
-
If the apply tries to modify a file that you’ve since modified, you’ll get a merge conflict and will have to try to resolve it.
186
-
If you want an easier way to test the stashed changes again, you can run `git stash branch`, which creates a new branch for you, checks out the commit you were on when you stashed your work, reapplies your work there, and then drops the stash if it applies successfully:
This is a nice shortcut to recover stashed work easily and work on it in a new branch.
206
+
这是在新分支轻松恢复储藏工作并继续工作的一个很不错的途径。
207
207
208
208
[[_git_clean]]
209
-
==== Cleaning your Working Directory
209
+
==== 清理工作目录
210
210
211
-
Finally, you may not want to stash some work or files in your working directory, but simply get rid of them. The `git clean` command will do this for you.
Some common reasons for this might be to remove cruft that has been generated by merges or external tools or to remove build artifacts in order to run a clean build.
You'll want to be pretty careful with this command, since it's designed to remove files from your working directory that are not tracked. If you change your mind, there is often no retrieving the content of those files. A safer option is to run `git stash --all` to remove everything but save it in a stash.
Assuming you do want to remove cruft files or clean your working directory, you can do so with `git clean`. To remove all the untracked files in your working directory, you can run `git clean -f -d`, which removes any files and also any subdirectories that become empty as a result. The `-f` means 'force' or "really do this".
If you ever want to see what it would do, you can run the command with the `-n` option, which means ``do a dry run and tell me what you _would_ have removed''.
By default, the `git clean` command will only remove untracked files that are not ignored. Any file that matches a pattern in your `.gitignore` or other ignore files will not be removed. If you want to remove those files too, such as to remove all `.o` files generated from a build so you can do a fully clean build, you can add a `-x` to the clean command.
If you don't know what the `git clean` command is going to do, always run it with a `-n` first to double check before changing the `-n` to a `-f` and doing it for real. The other way you can be careful about the process is to run it with the `-i` or ``interactive'' flag.
0 commit comments