git-stash 与 git-branch
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git-stash vs. git-branch
提问by Will Robertson
In a previous Git question, Daniel Benamy was talking about a workflow in Git:
在之前的 Git 问题中,Daniel Benamy 谈到了 Git 中的工作流程:
I was working on master and committed some stuff and then decided I wanted to put that work on hold. I backed up a few commits and then branched from before I started my crap work.
我在 master 上工作并提交了一些东西,然后决定我想搁置这项工作。我备份了一些提交,然后在开始我的垃圾工作之前分支。
He wanted to restore his working state to a previous point in time without losing his current changes. All of the answers revolved around, in various ways, something like
他想在不丢失当前更改的情况下将他的工作状态恢复到之前的时间点。所有的答案都以各种方式围绕着,比如
git branch -m master crap_work
git branch -m previous_master master
How does this compare to git stash
? I'm a bit confused trying to see what the different use case here when it seemslike everything git stash
does is already handled by branching…
这与 相比如何git stash
?当似乎一切git stash
都已经通过分支处理时,我试图看看这里的不同用例是什么,我有点困惑......
@Jordi Bunster: Thanks, that clears things up. I guess I'd kind of consider "stashing" to be like a lightweight, nameless, branch. So anything stash can do, branch can as well but with more words. Nice!
@ Jordi Bunster:谢谢,这让事情变得清晰。我想我会认为“藏匿”就像一个轻量级的、无名的分支。所以 stash 可以做的任何事情,分支也可以,但需要更多的词。好的!
回答by Jordi Bunster
'stash' takes the uncommitted, "dirty" stuff on your working copy, and stashes it away, leaving you with a clean working copy.
'stash' 将未提交的、“脏”的东西放在你的工作副本上,然后把它藏起来,给你留下一个干净的工作副本。
It doesn't really branch at all. You can then apply the stash on top of any other branch. Or, as of Git 1.6, you can do:
它根本没有真正的分支。然后,您可以将 stash 应用到任何其他分支的顶部。或者,从 Git 1.6 开始,您可以执行以下操作:
git stash branch <branchname> [<stash>]
to apply the stash on top of a new branch, all in one command.
将 stash 应用到一个新分支的顶部,所有这些都在一个命令中。
So, stash works great if you have not committed to the "wrong" branch yet.
因此,如果您还没有提交到“错误的”分支,那么 stash 效果很好。
If you've already committed, then the workflow you describe in your question is a better alternative. And by the way, you're right: Git is very flexible, and with that flexibility comes overlapping functionality.
如果您已经提交,那么您在问题中描述的工作流程是更好的选择。顺便说一句,你是对的:Git 非常灵活,伴随着这种灵活性而来的是重叠的功能。
回答by Ariejan
When you restore your stash, your changes are reapplied and you continue working on your code.
当您恢复存储时,您的更改将重新应用,您将继续处理您的代码。
To stash your current changes
隐藏您当前的更改
$ git stash save
Saved "WIP on master: e71813e..."
You can also have more than one stash. The stash works like a stack. Every time you save a new stash, it's put on top of the stack.
您也可以拥有多个藏匿处。存储就像一个堆栈。每次保存新的存储时,它都会放在堆栈的顶部。
$ git stash list
stash@{0}: WIP on master: e71813e..."
Note the stash@{0}
part? That's your stash ID. You'll need it to restore it later on. Let's do that right now. The stash ID changes with every stash you make. stash@{0} refers to the last stash you made.
注意stash@{0}
部分?那是你的藏匿ID。稍后您将需要它来恢复它。让我们现在就这样做。存储 ID 会随着您进行的每次存储而更改。stash@{0} 是指您最后一次进行的存储。
To apply a stash
应用存储
$ git stash apply stash@{0}
You may notice the stash is still there after you have applied it. You can drop it if you don't need it any more.
您可能会注意到应用程序后存储仍然存在。如果你不再需要它,你可以放弃它。
$ git stash drop stash@{0}
Or, because the stash acts like a stack, you can pop off the last stash you saved:
或者,因为 stash 就像一个堆栈,你可以弹出你保存的最后一个 stash:
$ git stash pop
If you want to wipe all your stashes away, run the 'clear' command:
如果你想清除所有的隐藏信息,请运行“clear”命令:
$ git stash clear
It may very well be that you don't use stashes that often. If you just want to quickly stash your changes to restore them later, you can leave out the stash ID.
很可能您不经常使用 stash。如果您只想快速存储更改以在以后恢复它们,则可以省略存储 ID。
$ git stash
...
$ git stash pop
Feel free to experiment with the stash before using it on some really important work.
在将它用于一些非常重要的工作之前,请随意尝试使用 stash。
I also have a more in-depth version of this posted on my blog.
回答by webmat
I'm always wary of git stash. If you stash a few times, things tend to get messy. git stash list will display a numbered list of stashes you created, with messages if you provided them... But the problem lies in the fact that you can't clean up stashes except with a brutal git stash clear (which removes them all). So unless you're always anal in giving super-descriptive messages for your stashes (kinda goes against stash's philosophy), you end up with an incomprehensible bunch of stashes.
我总是对 git stash 保持警惕。如果你藏了几次,事情往往会变得一团糟。git stash list 将显示您创建的存储的编号列表,如果您提供消息,则会显示消息......但问题在于您无法清理存储,除非使用残酷的 git stash clear (将它们全部删除) . 因此,除非你总是为你的藏匿处提供超级描述性的信息(有点违背藏匿处的哲学),否则你最终会得到一堆难以理解的藏匿处。
The only way I know of to figure out which one's which is to use gitk --all and spot the stashes. At least this lets you see what commit the stash was created on, as well as the diff of everything included in that stash.
我知道找出哪个是使用 gitk --all 并发现隐藏的唯一方法。至少这可以让您看到创建存储的提交,以及该存储中包含的所有内容的差异。
Note that I'm using git 1.5.4.3, and I think 1.6 adds git stash pop, which I guess would apply the selected stash andremove it from the list. Which seems a lot cleaner.
请注意,我使用的是 git 1.5.4.3,我认为 1.6 添加了 git stash pop,我想这会应用选定的 stash并将其从列表中删除。这看起来干净多了。
For now, I always try to branch unless I'm absolutely positive I'm gonna get back to that stash in the same day, even within the hour.
现在,我总是尝试分支,除非我绝对肯定我会在同一天,甚至在一小时内回到那个藏匿处。
回答by Dan Aloni
If you look for a workflow that may be more fitting than git stash, you may want to look at git-bottle. It's a utility for the purpose of saving and restoring the various git working states as normal git commits, effectively snapshotting the current and pertinent state of your working tree and allvarious file states shown under git status.
如果您正在寻找可能比 git stash 更合适的工作流程,您可能需要查看git-bottle。它是一个实用程序,用于在正常 git 提交时保存和恢复各种 git 工作状态,有效地快照工作树的当前和相关状态以及git 状态下显示的所有各种文件状态。
Key differences from git stash
:
主要区别git stash
:
git stash
saves the dirty git state narrowly (modified files, and added files in the index), whereasgit-bottle
is designed to save everythingthat is different fromHEAD
, and it differentiates in a preserving way between modified, modified and not added, not added, unmerged paths, and the complete rebase/merge states (only paths under.gitignore
are not saved).git stash
saves to stash objects that you need to keep track separately. If I stashed something 2 weeks ago I might not remember it, whereasgit-bottle
saves as tentative commits to the current branch. The reverse action isgit-unbottle
which is the equivalent of thegit stash
pop. It is possible to push and share these commits among repositories. This can be useful for remote builds, where you have another repository in a remote server just for building, or for collaborating with other people on conflict resolution.
git stash
狭保存脏git的状态(修改的文件,并在索引添加的文件),而git-bottle
被设计为保存的一切就是从不同HEAD
,并且它在修改之间的保持方式区分,改性和未添加,不添加,未合并的路径,以及完整的变基/合并状态(仅.gitignore
不保存下的路径)。git stash
保存到您需要单独跟踪的藏匿对象。如果我在 2 周前藏了一些东西,我可能不会记得它,而git-bottle
将暂定提交保存到当前分支。反向动作是git-unbottle
相当于git stash
弹出。可以在存储库之间推送和共享这些提交。这对于远程构建很有用,您在远程服务器中有另一个存储库仅用于构建或与其他人协作解决冲突。