如何在 git 中组合多个 stash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9143865/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:54:15  来源:igfitidea点击:

How to combine multiple stashes in git

gitgit-stash

提问by sscirrus

This is a pipeline on branch frontendover the last two weeks.

这是frontend过去两周在分支上的管道。

| Stash@{3}is all code since Stash@{1}(excluding the two tiny commits)
| Tiny Commit
| Tiny commit
| Huge bulk commit two weeks ago, now rebased and moved to Stash@{1}

| Stash@{3}是所有代码Stash@{1}(不包括两个微小的提交)
| 微小的承诺
| 微小的提交
| 两周前大量提交,现在重新定位并移至Stash@{1}

My working tree is currently clean.
Stash@{1}is the contents from a bulk commit of general development code two weeks ago (this should have been stashed in the first place). This commit was undone and moved to stash.
Stash@{3}is the newest work on that tree since Stash@{1}(minus a couple of changes that have been committed).

我的工作树目前是干净的。
Stash@{1}是两周前批量提交的通用开发代码的内容(这应该首先被藏起来)。此提交已撤消并移至 stash。
Stash@{3}是该树上的最新工作Stash@{1}(减去已提交的一些更改)。

I need to combine these two stashes together in my working tree so I can make a number of commits from this huge pool of work.

我需要在我的工作树中将这两个 stashes 组合在一起,以便我可以从这个庞大的工作池中进行多次提交。

I ran git stash apply stash@{1}then I tried:

我跑了git stash apply stash@{1}然后我试过:

git stash apply stash@{3}
git stash show -p | git stash apply stash@{3}

git stash apply stash@{3}
git stash show -p | git stash apply stash@{3}

but I get 'dirty working tree' in both cases. How can I merge this work together? Because stash@{3}is newer, I want it to supersede stash@{1}wherever there are conflicts.

但在这两种情况下我都得到了“肮脏的工作树”。我怎样才能将这项工作合并在一起?因为stash@{3}是更新的,我希望它stash@{1}在有冲突的地方取代。

采纳答案by Andrew Marshall

You can only apply a stash if there are no conflicts with modified files in the working tree, so, first, ensure there are no modified files in git status, if there are, commit them. Then do:

如果与工作树中的修改文件没有冲突,您只能应用 stash,因此,首先,确保 中没有修改的文件git status,如果有,则提交它们。然后做:

git stash apply stash@{1}
git commit -a
# Enter your commit message
git stash apply stash@{3}

Then you can either make a new commit, or amend the previous one to combine them. You may need to resolve merge conflicts after each apply.

然后,您可以进行新的提交,也可以修改前一个提交以将它们组合起来。您可能需要在每次申请后解决合并冲突。

Also, if you ever decide to use git stash poprather than apply, note that stash@{3}would become stash@{2}since the first one was popped off.

此外,如果您决定使用git stash pop而不是apply,请注意,自从第一个弹出以来,它stash@{3}就会变成stash@{2}

回答by bkeepers

It's a little involved, but this almost always works:

这有点复杂,但这几乎总是有效:

  1. Pop the first stash

    $ git stash pop
    
  2. Temporarily commit the changes from the first stash

    $ git add . && git commit -am 'WIP'
    
  3. Pop the second stash

    $ git stash pop
    
  4. Undo the temporary commit, keeping the changes it introduced

    $ git reset --soft HEAD^
    
  1. 弹出第一个藏匿处

    $ git stash pop
    
  2. 临时提交第一个存储中的更改

    $ git add . && git commit -am 'WIP'
    
  3. 弹出第二个储藏室

    $ git stash pop
    
  4. 撤消临时提交,保留它引入的更改

    $ git reset --soft HEAD^
    

回答by siride

A better way is to just use git stash show -p stash@{whatever} > stash-{whatever}.diffand then use git applyfor each one.

更好的方法是只使用git stash show -p stash@{whatever} > stash-{whatever}.diff然后git apply为每个使用。

回答by Henridv

I had a similar problem and solved it like this.

我有一个类似的问题,并像这样解决了它。

Use git stash popto apply one of the stashes. Then create a patch of this stash with git diff -p > ../stash.diff. You can then reset your working tree (or stash the changes again), and pop the other stash with git stash pop stash@{1}. If you apply your patch at this moment you can 'merge' the two different stashes.

使用git stash pop应用藏匿之一。然后使用git diff -p > ../stash.diff. 然后,您可以重置您的工作树(或再次隐藏更改),并使用git stash pop stash@{1}. 如果您此时应用您的补丁,您可以“合并”两个不同的存储。

You'll probably have some conflicts to resolve. If all goes well you can then drop the stashed changes.

您可能需要解决一些冲突。如果一切顺利,您可以删除隐藏的更改。