Git 工具 - 贮藏与清理
<h3>原文地址</h3>
<p>[Git - 贮藏与清理 (git-scm.com)](<a href="https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E8%B4%AE%E8%97%8F%E4%B8%8E%E6%B8%85%E7%90%86">https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E8%B4%AE%E8%97%8F%E4%B8%8E%E6%B8%85%E7%90%86</a> "Git - 贮藏与清理 (git-scm.com)")</p>
<h2>贮藏与清理</h2>
<p>有时,当你在项目的一部分上已经工作一段时间后,所有东西都进入了混乱的状态, 而这时你想要切换到另一个分支做一点别的事情。 问题是,你不想仅仅因为过会儿回到这一点而为做了一半的工作创建一次提交。 针对这个问题的答案是 <code>git stash</code> 命令。</p>
<p>贮藏(stash)会处理工作目录的脏的状态——即跟踪文件的修改与暂存的改动——然后将未完成的修改保存到一个栈上, 而你可以在任何时候重新应用这些改动(甚至在不同的分支上)。</p>
<p><table style="border-collapse: collapse; border-spacing: 0px; box-sizing: border-box; margin-top: 30px; margin-bottom: 1em; border-bottom: 1px solid rgb(136, 136, 136);"><tbody style="box-sizing: border-box;"><tr style="box-sizing: border-box; border-width: 0px;"><td class="icon" style="padding: 0px 1em; box-sizing: border-box; width: 164.167px; font-family: sans-serif; font-size: 12px; line-height: 14.4px; border-right: 2px solid rgb(216, 215, 207);"><div class="title" style="box-sizing: border-box; font-weight: bold; color: rgb(241, 78, 50);">Note</div></td><td class="content" style="padding: 0px 1em; box-sizing: border-box; font-family: sans-serif; font-size: 12px; line-height: 14.4px;"><div class="title" style="box-sizing: border-box; font-weight: bold; color: rgb(241, 78, 50);">迁移到<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">git stash push</code></div><div class="paragraph" style="box-sizing: border-box;"><p style="margin: 6px 0px 11px; font-size: 12px; line-height: 14.4px; box-sizing: border-box; font-family: sans-serif; text-indent: 0px;">截至 2017 年 10 月下旬,Git 邮件列表上进行了广泛讨论,该讨论中弃用了<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">git stash save</code><span> </span>命令, 代之以现有<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">git stash push</code><span> </span>命令。主因是<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">git stash push</code><span> </span>引入了贮藏选定的<span> </span><strong style="font-weight: bold; box-sizing: border-box;">路径规范</strong><span> </span>的选项, 而有些东西<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">git stash save</code><span> </span>不支持。</p></div><div class="paragraph" style="box-sizing: border-box;"><p style="margin: 6px 0px 11px; font-size: 12px; line-height: 14.4px; box-sizing: border-box; font-family: sans-serif; text-indent: 0px;"><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">git stash save</code><span> </span>不会很快就消失,所以不用担心它突然不见。 不过你可能想要迁移到<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">push</code><span> </span>来获取新功能。</p></div></td></tr></tbody></table></p>
<h3>贮藏工作</h3>
<p>为了演示贮藏,你需要进入项目并改动几个文件,然后可以暂存其中的一个改动。 如果运行 <code>git status</code>,可以看到有改动的状态:</p>
<pre><code>$ git status
Changes to be committed:
(use &quot;git reset HEAD &lt;file&gt;...&quot; to unstage)
modified: index.html
Changes not staged for commit:
(use &quot;git add &lt;file&gt;...&quot; to update what will be committed)
(use &quot;git checkout -- &lt;file&gt;...&quot; to discard changes in working directory)
modified: lib/simplegit.rb</code></pre>
<p>现在想要切换分支,但是还不想要提交之前的工作;所以贮藏修改。 将新的贮藏推送到栈上,运行 <code>git stash</code> 或 <code>git stash push</code>:</p>
<pre><code>$ git stash
Saved working directory and index state \
&quot;WIP on master: 049d078 added the index file&quot;
HEAD is now at 049d078 added the index file
(To restore them type &quot;git stash apply&quot;)</code></pre>
<p>可以看到工作目录是干净的了:</p>
<pre><code>$ git status
# On branch master
nothing to commit, working directory clean</code></pre>
<p>此时,你可以切换分支并在其他地方工作;你的修改被存储在栈上。 要查看贮藏的东西,可以使用 <code>git stash list</code>:</p>
<pre><code>$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert &quot;added file_size&quot;
stash@{2}: WIP on master: 21d80a5 added number to log</code></pre>
<p>在本例中,有两个之前的贮藏,所以你接触到了三个不同的贮藏工作。 可以通过原来 stash 命令的帮助提示中的命令将你刚刚贮藏的工作重新应用:<code>git stash apply</code>。 如果想要应用其中一个更旧的贮藏,可以通过名字指定它,像这样:<code>git stash apply stash@{2}</code>。 如果不指定一个贮藏,Git 认为指定的是最近的贮藏:</p>
<pre><code>$ git stash apply
On branch master
Changes not staged for commit:
(use &quot;git add &lt;file&gt;...&quot; to update what will be committed)
(use &quot;git checkout -- &lt;file&gt;...&quot; to discard changes in working directory)
modified: index.html
modified: lib/simplegit.rb
no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;)</code></pre>
<p>可以看到 Git 重新修改了当你保存贮藏时撤消的文件。 在本例中,当尝试应用贮藏时有一个干净的工作目录,并且尝试将它应用在保存它时所在的分支。 并不是必须要有一个干净的工作目录,或者要应用到同一分支才能成功应用贮藏。 可以在一个分支上保存一个贮藏,切换到另一个分支,然后尝试重新应用这些修改。 当应用贮藏时工作目录中也可以有修改与未提交的文件——如果有任何东西不能干净地应用,Git 会产生合并冲突。</p>
<p>文件的改动被重新应用了,但是之前暂存的文件却没有重新暂存。 想要那样的话,必须使用 <code>--index</code> 选项来运行 <code>git stash apply</code> 命令,来尝试重新应用暂存的修改。 如果已经那样做了,那么你将回到原来的位置:</p>
<pre><code>$ git stash apply --index
On branch master
Changes to be committed:
(use &quot;git reset HEAD &lt;file&gt;...&quot; to unstage)
modified: index.html
Changes not staged for commit:
(use &quot;git add &lt;file&gt;...&quot; to update what will be committed)
(use &quot;git checkout -- &lt;file&gt;...&quot; to discard changes in working directory)
modified: lib/simplegit.rb</code></pre>
<p>应用选项只会尝试应用贮藏的工作——在堆栈上还有它。 可以运行 <code>git stash drop</code> 加上将要移除的贮藏的名字来移除它:</p>
<pre><code>$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert &quot;added file_size&quot;
stash@{2}: WIP on master: 21d80a5 added number to log
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)</code></pre>
<p>也可以运行 <code>git stash pop</code> 来应用贮藏然后立即从栈上扔掉它。</p>
<h3>贮藏的创意性使用</h3>
<p>有几个贮藏的变种可能也很有用。 第一个非常流行的选项是 <code>git stash</code> 命令的 <code>--keep-index</code> 选项。 它告诉 Git 不仅要贮藏所有已暂存的内容,同时还要将它们保留在索引中。</p>
<pre><code>$ git status -s
M index.html
M lib/simplegit.rb
$ git stash --keep-index
Saved working directory and index state WIP on master: 1b65b17 added the index file
HEAD is now at 1b65b17 added the index file
$ git status -s
M index.html</code></pre>
<p>另一个经常使用贮藏来做的事情是像贮藏跟踪文件一样贮藏未跟踪文件。 默认情况下,<code>git stash</code> 只会贮藏已修改和暂存的 <strong>已跟踪</strong> 文件。 如果指定 <code>--include-untracked</code> 或 <code>-u</code> 选项,Git 也会贮藏任何未跟踪文件。 然而,在贮藏中包含未跟踪的文件仍然不会包含明确 <strong>忽略</strong> 的文件。 要额外包含忽略的文件,请使用 <code>--all</code> 或 <code>-a</code> 选项。</p>
<pre><code>$ git status -s
M index.html
M lib/simplegit.rb
?? new-file.txt
$ git stash -u
Saved working directory and index state WIP on master: 1b65b17 added the index file
HEAD is now at 1b65b17 added the index file
$ git status -s
$</code></pre>
<p>最终,如果指定了 <code>--patch</code> 标记,Git 不会贮藏所有修改过的任何东西, 但是会交互式地提示哪些改动想要贮藏、哪些改动需要保存在工作目录中。</p>
<pre><code>$ git stash --patch
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index 66d332e..8bb5674 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -16,6 +16,10 @@ class SimpleGit
return `#{git_cmd} 2&gt;&amp;1`.chomp
end
end
+
+ def show(treeish = 'master')
+ command(&quot;git show #{treeish}&quot;)
+ end
end
test
Stash this hunk [y,n,q,a,d,/,e,?]? y
Saved working directory and index state WIP on master: 1b65b17 added the index file</code></pre>
<h3>从贮藏创建一个分支</h3>
<p>如果贮藏了一些工作,将它留在那儿了一会儿,然后继续在贮藏的分支上工作,在重新应用工作时可能会有问题。 如果应用尝试修改刚刚修改的文件,你会得到一个合并冲突并不得不解决它。 如果想要一个轻松的方式来再次测试贮藏的改动,可以运行 <code>git stash branch &lt;new branchname&gt;</code> 以你指定的分支名创建一个新分支,检出贮藏工作时所在的提交,重新在那应用工作,然后在应用成功后丢弃贮藏:</p>
<pre><code>$ git stash branch testchanges
M index.html
M lib/simplegit.rb
Switched to a new branch 'testchanges'
On branch testchanges
Changes to be committed:
(use &quot;git reset HEAD &lt;file&gt;...&quot; to unstage)
modified: index.html
Changes not staged for commit:
(use &quot;git add &lt;file&gt;...&quot; to update what will be committed)
(use &quot;git checkout -- &lt;file&gt;...&quot; to discard changes in working directory)
modified: lib/simplegit.rb
Dropped refs/stash@{0} (29d385a81d163dfd45a452a2ce816487a6b8b014)</code></pre>
<p>这是在新分支轻松恢复贮藏工作并继续工作的一个很不错的途径。</p>
<h3>清理工作目录</h3>
<p>对于工作目录中一些工作或文件,你想做的也许不是贮藏而是移除。 <code>git clean</code> 命令就是用来干这个的。</p>
<p>清理工作目录有一些常见的原因,比如说为了移除由合并或外部工具生成的东西, 或是为了运行一个干净的构建而移除之前构建的残留。</p>
<p>你需要谨慎地使用这个命令,因为它被设计为从工作目录中移除未被追踪的文件。 如果你改变主意了,你也不一定能找回来那些文件的内容。 一个更安全的选项是运行 <code>git stash --all</code> 来移除每一样东西并存放在栈中。</p>
<p>你可以使用 <code>git clean</code> 命令去除冗余文件或者清理工作目录。 使用 <code>git clean -f -d</code> 命令来移除工作目录中所有未追踪的文件以及空的子目录。 <code>-f</code> 意味着“强制(force)”或“确定要移除”,使用它需要 Git 配置变量 <code>clean.requireForce</code> 没有显式设置为 <code>false</code>。</p>
<p>如果只是想要看看它会做什么,可以使用 <code>--dry-run</code> 或 <code>-n</code> 选项来运行命令, 这意味着“做一次演习然后告诉你 <strong>将要</strong> 移除什么”。</p>
<pre><code>$ git clean -d -n
Would remove test.o
Would remove tmp/</code></pre>
<p>默认情况下,<code>git clean</code> 命令只会移除没有忽略的未跟踪文件。 任何与 <code>.gitignore</code> 或其他忽略文件中的模式匹配的文件都不会被移除。 如果你也想要移除那些文件,例如为了做一次完全干净的构建而移除所有由构建生成的 <code>.o</code> 文件, 可以给 clean 命令增加一个 <code>-x</code> 选项。</p>
<pre><code>$ git status -s
M lib/simplegit.rb
?? build.TMP
?? tmp/
$ git clean -n -d
Would remove build.TMP
Would remove tmp/
$ git clean -n -d -x
Would remove build.TMP
Would remove test.o
Would remove tmp/</code></pre>
<p>如果不知道 <code>git clean</code> 命令将会做什么,在将 <code>-n</code> 改为 <code>-f</code> 来真正做之前总是先用 <code>-n</code> 来运行它做双重检查。 另一个小心处理过程的方式是使用 <code>-i</code> 或 “interactive” 标记来运行它。</p>
<p>这将会以交互模式运行 clean 命令。</p>
<pre><code>$ git clean -x -i
Would remove the following items:
build.TMP test.o
*** Commands ***
1: clean 2: filter by pattern 3: select by numbers 4: ask each 5: quit
6: help
What now&gt;</code></pre>
<p>这种方式下可以分别地检查每一个文件或者交互地指定删除的模式。</p>
<p><table style="border-collapse: collapse; border-spacing: 0px; box-sizing: border-box; margin-top: 30px; margin-bottom: 1em; border-bottom: 1px solid rgb(136, 136, 136);"><tbody style="box-sizing: border-box;"><tr style="box-sizing: border-box; border-width: 0px;"><td class="icon" style="padding: 0px 1em; box-sizing: border-box; width: 164.167px; font-family: sans-serif; font-size: 12px; line-height: 14.4px; border-right: 2px solid rgb(216, 215, 207);"><div class="title" style="box-sizing: border-box; font-weight: bold; color: rgb(241, 78, 50);">Note</div></td><td class="content" style="padding: 0px 1em; box-sizing: border-box; font-family: sans-serif; font-size: 12px; line-height: 14.4px;"><div class="paragraph" style="box-sizing: border-box;"><p style="margin: 6px 0px 11px; font-size: 12px; line-height: 14.4px; box-sizing: border-box; font-family: sans-serif; text-indent: 0px;">在一种奇怪的情况下,可能需要格外用力才能让 Git 清理你的工作目录。 如果你恰好在工作目录中复制或克隆了其他 Git 仓库(可能是子模块),那么即便是<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">git clean -fd</code><span> </span>都会拒绝删除这些目录。这种情况下,你需要加上第二个<span> </span><code style="font-family: monospace; font-size: inherit; border-radius: 3px; display: inline; padding: 0px; margin-bottom: 1em; overflow: auto; line-height: inherit; color: rgb(51, 51, 51); background: rgb(238, 238, 238); border: 1px solid rgb(245, 245, 245); box-sizing: border-box; overflow-wrap: break-word; white-space: pre-wrap;">-f</code><span> </span>选项来强调。</p></div></td></tr></tbody></table></p>