简体   繁体   English

Git 藏匿两个分支

[英]Git stash two branches

This is the situation:情况是这样的:

  1. I made changes on branch A我在分支 A 上进行了更改
  2. git stash on branch A分支 A 上的git stash
  3. git checkout B
  4. Made changes on branch B对分支 B 进行了更改
  5. git stash on branch B分支 B 上的git stash
  6. git checkout A
  7. git stash pop on branch A分支 A 上的git stash pop

After step 7 of the list above, the changes I had made on branch A before stashing did not come back but the changes I had made on branch B popped on branch A. I used cmd z on branch A and the file went to its previous state, which had the changes I had made.在上面列表的第 7 步之后,我在存储之前在分支 A 上所做的更改没有回来,但是我在分支 B 上所做的更改在分支 A 上弹出。我在分支 A 上使用了cmd z并且文件转到了之前的状态,其中包含我所做的更改。 It seemed the HEAD of branch A moved to the HEAD of branch B. The same happened when I git checkout B and git stash pop on that branch: branch B had all the changes made on branch A but not the changes I had made on branch B. I used cmd z again to revert to the branch state I needed.似乎分支 A 的 HEAD 移动到分支 B 的 HEAD。当我在该分支上git checkout Bgit stash pop时发生了同样的情况:分支 B 在分支 A 上进行了所有更改,但没有在分支上进行的更改B. 我再次使用cmd z恢复到我需要的分支状态。

This caused me a lot of problems for sometime, until I was allowed to commit code again on the project (nobody could commit for a while because there was an automated push to the server on commits and the manager didn't want new code in the server until they ran some tests).这在一段时间内给我带来了很多问题,直到我被允许再次在项目上提交代码(暂时没有人可以提交,因为在提交时自动推送到服务器并且经理不希望在项目中添加新代码)服务器,直到他们运行了一些测试)。 How can I pop only the changes made on the branches themselves and not changes made on other branches?我怎样才能只弹出在分支上所做的更改而不是在其他分支上所做的更改?

What git stash does is make a commit. git stash所做的是提交。 1 1

Of course, what git commit does is make a commit.当然, git commit所做的就是进行一次提交。 So why do we have git stash at all?那么为什么我们有git stash呢?

A significant difference between these commands is that the commits git stash makes are not on any branches .这些命令之间的一个显着区别是git stash所做的提交不在任何分支上 This allows you to stash when on one branch, then move to another branch and apply the stash there.这允许您在一个分支上stash ,然后移动到另一个分支并在那里应用存储。 In other words, it lets you move in-progress work.换句话说,它可以让您移动正在进行的工作。

You can often, but not always, move in-progress work anyway.无论如何,您通常可以(但并非总是)移动正在进行的工作。 See Git - checkout another branch when there are uncommitted changes on the current branch .当当前分支上有未提交的更改时,请参阅Git-checkout another branch When you can't , though, you can use git stash to deal with this.但是,当你不能时,你可以使用git stash来处理这个问题。

On the other hand, if you want to "stash on a branch", as in your case, you are probably better off just making a regular commit, rather than a special stash commit.另一方面,如果你想“藏在一个分支上”,就像你的情况一样,你可能最好只进行常规提交,而不是特殊的藏匿提交。 Such commits are easier to deal with, and also do not have a bug that git stash has .这样的提交更容易处理,而且也没有git stash的错误 You are not likely to hit this bug but "regular commits are simpler and easier to deal with" (commits on branches, vs stashes) is a pretty good reason to avoid git stash .您不太可能遇到此错误,但“定期提交更简单,更容易处理”(在分支上提交,与隐藏)是避免git stash一个很好的理由。

If you prefer to use git stash anyway, note that each new stash "pushes" the previous ones up higher in a "stash stack".如果你更喜欢使用git stash ,请注意每个新的 stash 都会在“stash stack”中“推动”之前的。 The old stash becomes stash@{1} , and what was stash@{1} becomes stash@{2} , and so on.旧的 stash 变为stash@{1} ,而stash@{1}变为stash@{2} ,依此类推。 When you drop (discard) or pop (attempt to apply, then discard) a stash, the stacked ones above it move back down—so if you were to git stash drop stash@{3} when you had stash@{4} and stash@{5} as well, you would be left with stash@{3} and stash@{4} now.当您drop (丢弃)或pop (尝试应用,然后丢弃)一个藏匿处时,其上方堆叠的那些会移回原处——因此,如果您在拥有stash@{4}时使用git stash drop stash@{3} stash@{4}并且stash@{5}也是如此,您现在将剩下stash@{3}stash@{4}

You can name any stash, including the most recent one, this way: stash@{0} means the same thing as stash .您可以这样命名任何 stash,包括最近的一个: stash@{0}stash含义相同。 (Git actually implements all this using the reflog for stash .) (Git 实际上使用stashreflog实现了所有这些。)


1 In fact, it makes at least two commits, and sometimes three. 1事实上,它至少进行两次提交,有时会进行三次。 The two commits store the index and work-tree state;两次提交存储索引和工作树状态; the third commit, if present, is from -u or -a and stores the unstaged ( -u ) or all ( -a ) files.第三次提交(如果存在)来自-u-a并存储未暂存 ( -u ) 或所有 ( -a ) 文件。 The work-tree commit is a very odd merge commit, with the index commit as its second parent and the third commit, if present, as its third parent.工作树提交是一个非常奇怪的合并提交,索引提交作为它的第二个父级,第三个提交(如果存在)作为它的第三个父级。 The first parent of the work-tree commit, and the sole parent of the index commit, is the commit that was current when you ran git stash .工作树提交的第一个父级以及索引提交的唯一父级是您运行git stash时当前的提交。

If you draw commit graph fragments—which, any time you are doing anything complicated in Git, is a good idea—the index-and-work-tree commit pair kind of dangles off the original commit, with the refs/stash reference pointing to the pair, rather than the branch name.如果你绘制提交图片段——每当你在 Git 中做任何复杂的事情时,这是一个好主意——索引和工作树提交对有点悬在原始提交之外, refs/stash引用指向对,而不是分支名称。 It looks almost like a small handbag, or a food cache hung from a tree branch to keep it away from bears, or some such, so I like to refer to this as a "stash bag".它看起来几乎像一个小手提包,或者挂在树枝上以防止熊进入的食物储藏室,或者类似的东西,所以我喜欢把它称为“藏匿袋”。

Torek (as usual) provides an excellent answer, but I believe the succinct answer here is just to note that the stash contains data as a stack (LIFO). Torek(像往常一样)提供了一个很好的答案,但我相信这里的简洁答案只是要注意存储包含作为堆栈的数据 (LIFO)。 So when you pushed A's work and then B's work, it first pops B then A. So when you went back to A and then did the first pop, you got the saved B work.所以当你推送A的工作然后B的工作时,它首先弹出B然后A。所以当你回到A然后做第一个弹出时,你得到了保存的B工作。

Popping a stash re-applies that last stash regardless from which branch you stashed!无论您从哪个分支藏匿,弹出一个藏匿处都会重新应用最后一个藏匿处! So, the problem you are posting is that last stash that was created on branch B was re-applied on branch A. Next time, You have to specify which stash you want to pop.因此,您发布的问题是在分支 B 上创建的最后一个存储被重新应用于分支 A。下次,您必须指定要弹出的存储。

Solution to the given problem:给定问题的解决方案:

  1. Stash again the current status再次隐藏当前状态
  2. Pop the stash that is before the last弹出最后一个之前的藏匿处

That's all.仅此而已。

if you type git stash list something like this will be displayed based on your given situation如果您输入git stash list则会根据您的给定情况显示类似的内容

stash@{0}: WIP on branch-b
stash@{1}: WIP on branch-a

typing git stash pop will always get the stash@{0} node.输入git stash pop将始终获得stash@{0}节点。

So, if you're in branch a and you want to apply your progress in that branch.因此,如果您在branch a并且想要在该分支中应用您的进度。 Basing on the list, you should type:根据列表,您应该键入:

git stash pop stash@{1}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM