为什么每个分支的 git stash 不是唯一的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/89487/
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
Why isn't the git stash unique per branch?
提问by iros
I suppose it allows for moving changes from one branch to the next but that's what cherry picking is for and if you're not making a commit of your changes, perhaps you shouldn't be moving them around?
我想它允许将更改从一个分支移动到下一个分支,但这就是樱桃采摘的目的,如果您不提交更改,也许您不应该移动它们?
I have on occasion applied the wrong stash at the wrong branch, which left me wondering about this question.
我有时会在错误的分支上应用错误的存储,这让我对这个问题感到疑惑。
采纳答案by Aristotle Pagaltzis
As mentioned, if you want a “per-branch stash,” you really want a new branch forking off from the existing branch.
如前所述,如果您想要“每个分支的存储”,您真的希望从现有分支中分出一个新分支。
Also, besides the already mentioned fact that the stash allows you to pull into a branch that you're working on, it also allows you to switchbranches before you have committed everything. This is useful not for cherry-picking in the usual sense so much as for cherry-picking your working copy.
此外,除了已经提到的事实,即 stash 允许您进入正在处理的分支,它还允许您在提交所有内容之前切换分支。这对于通常意义上的挑选非常有用,而对于挑选您的工作副本非常有用。
F.ex., while working on a feature branch, I will often notice minor bugs or cosmetic impurities in the code that aren't relevant to that branch. Well, I just fix those right away. When time comes to commit, I selectively commit the relevant changes but not the fixes and cosmetics. Instead I stash those, which allows me to switch to my minor-fixes-on-stable branch, where I can then apply the stash and commit each minor fix separately. (Depending on the changes in question, I will also stash some of them yet again, to switch to a different feature branch, where I apply those.)
例如,在处理功能分支时,我经常会注意到代码中与该分支无关的小错误或外观杂质。好吧,我只是马上解决这些问题。当需要提交时,我有选择地提交相关更改,而不是修复和修饰。相反,我将这些存储起来,这使我可以切换到我的小修正稳定分支,然后我可以在那里应用存储并分别提交每个小修复。(根据有问题的更改,我还将再次隐藏其中的一些,以切换到不同的功能分支,在那里我应用这些。)
This allows me to go deep into programming mode when I am working, and not worry about proper librarianship of my code. Then when I take a mental break, I can go back and carefully sort my changes into all the right shelves.
这使我可以在工作时深入编程模式,而不必担心我的代码是否正确。然后,当我休息一下时,我可以回去仔细地将我的更改分类到所有正确的架子上。
If the stash weren't global, this type of workflow would be far more difficult to do.
如果 stash 不是全局的,这种类型的工作流将很难做到。
回答by iGbanam
As of Git 1.6, you can now apply stashes to branches using
从 Git 1.6 开始,您现在可以使用
git stash branch name_of_new_branch
Git will create the new branch for you, and check it out! For more information, see
Git 将为您创建新分支,并检查它!有关更多信息,请参阅
info git-stashand search on option=branch.
info git-stash并搜索 option=branch。
I'm guessing you can move stashes around using
我猜你可以使用
git stash branch <branch | new_branch> [<stash>]
and to see a list of your stashes, use
并查看您的藏匿列表,请使用
git stash list
回答by Patrick_O
if you want a "stash" that runs off a branch do something like this to store your changes on a new branch off your current branch.
如果您想要一个从分支运行的“存储”,请执行以下操作以将您的更改存储在当前分支的新分支上。
git checkout -b new_stash
git commit -a -m "stashed changes"
to undo the stash
撤消藏匿处
git reset HEAD^
git branch -d new_stash
git stash is especially useful because you can pull changes into a dirty tree, i.e. if you have outstanding edits and want to do a
git stash 特别有用,因为您可以将更改拉入脏树,即如果您有出色的编辑并想要做一个
git pull
and you can't, you can stash your changes, pull and then apply the stash
你不能,你可以隐藏你的更改,拉取然后应用存储
git stash
git pull
git stash apply
git stash clear
hope this helped!
希望这有帮助!
回答by Patrick_O
git-stash is most useful to me to move not-yet-checked-in changes off to a different branch than the one that is currently checked out.
git-stash 对我来说最有用的是将尚未签入的更改移到与当前签出的分支不同的分支。
For example - I often find myself doing simple changes on a bug-fixes branch; only to find that a change I'm working on is more complex than I first guessed. Git-stash is the easiest way to move that set of changes to a different branch.
例如 - 我经常发现自己在错误修复分支上做简单的更改;只是发现我正在进行的更改比我最初猜测的要复杂。Git-stash 是将这组更改移动到不同分支的最简单方法。

