git stash -> 将隐藏的更改与当前更改合并

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

git stash -> merge stashed change with current changes

gitgit-mergegit-stash

提问by Bemis

I made some changes to my branch and realized I forgot I had stashed some other necessary changes to said branch. What I want is a way to merge my stashed changes with the current changes.

我对我的分支进行了一些更改,并意识到我忘记了我已经对所述分支进行了一些其他必要的更改。我想要的是一种将我隐藏的更改与当前更改合并的方法。

Is there a way to do this?

有没有办法做到这一点?

Its more for convenience, I eventually gave up and committed first my current changes, then my stashed changes, but I would have preferred to get them in with one fell swoop.

更多的是为了方便,我最终放弃并首先提交了我当前的更改,然后是我隐藏的更改,但我更愿意一举完成它们。

回答by Joshua Warner

I just discovered that if your uncommitted changes are added to the index (i.e. "staged", using git add ...), then git stash apply(and, presumably, git stash pop) will actually do a proper merge. If there are no conflicts, you're golden. If not, resolve them as usual with git mergetool, or manually with an editor.

我刚刚发现,如果您未提交的更改添加到索引中(即“暂存”,使用git add ...),那么git stash apply(并且,大概,git stash pop)实际上会进行适当的合并。如果没有冲突,你就是金子。如果没有,请像往常一样使用git mergetool,或使用编辑器手动解决它们。

To be clear, this is the process I'm talking about:

需要明确的是,这是我正在谈论的过程:

mkdir test-repo && cd test-repo && git init
echo test > test.txt
git add test.txt && git commit -m "Initial version"

# here's the interesting part:

# make a local change and stash it:
echo test2 > test.txt
git stash

# make a different local change:
echo test3 > test.txt

# try to apply the previous changes:
git stash apply
# git complains "Cannot apply to a dirty working tree, please stage your changes"

# add "test3" changes to the index, then re-try the stash:
git add test.txt
git stash apply
# git says: "Auto-merging test.txt"
# git says: "CONFLICT (content): Merge conflict in test.txt"

... which is probably what you're looking for.

......这可能是你正在寻找的。



tl;dr

tl;博士

Run git addfirst.

先跑git add

回答by Brandan

Running git stash popor git stash applyis essentially a merge. You shouldn't have needed to commit your current changes unless the files changed in the stash are also changed in the working copy, in which case you would've seen this error message:

运行git stash popgit stash apply本质上是一个合并。您应该不需要提交当前的更改,除非存储中更改的文件也在工作副本中更改,在这种情况下,您会看到以下错误消息:

error: Your local changes to the following files would be overwritten by merge:
       file.txt
Please, commit your changes or stash them before you can merge.
Aborting

In that case, you can't apply the stash to your current changes in one step. You can commit the changes, apply the stash, commit again, and squash those two commits using git rebaseif you really don't want two commits, but that may be more trouble that it's worth.

在这种情况下,您无法一步将 stash 应用到您当前的更改。git rebase如果您真的不想要两次提交,您可以提交更改,应用存储,再次提交并压缩这两个提交,但这可能会带来更多的麻烦,但值得。

回答by ks1322

What I want is a way to merge my stashed changes with the current changes

我想要的是一种将我隐藏的更改与当前更改合并的方法

Here is another option to do it:

这是执行此操作的另一种选择:

git stash show -p|git apply
git stash drop

git stash show -pwill show the patch of last saved stash. git applywill apply it. After the merge is done, merged stash can be dropped with git stash drop.

git stash show -p将显示最后保存的藏匿处的补丁。git apply将应用它。合并完成后,合并的存储可以用git stash drop.

回答by user3856437

The way I do this is to git addthis first then git stash apply <stash code>. It's the most simple way.

我这样做的方式是git add先这样git stash apply <stash code>。这是最简单的方法。

回答by Frank-Rene Sch?fer

May be, it is not the very worst idea to merge (via difftool) from ... yes ... a branch!

可能是,从......合并(通过difftool)并不是最糟糕的想法......是的......一个分支!

> current_branch=$(git status | head -n1 | cut -d' ' -f3)
> stash_branch="$current_branch-stash-$(date +%yy%mm%dd-%Hh%M)"
> git stash branch $stash_branch
> git checkout $current_branch
> git difftool $stash_branch

回答by Finelf

you can easily

你可以很容易地

  1. Commit your current changes
  2. Unstash your stash and resolve conflicts
  3. Commit changes from stash
  4. Soft reset to commit you are comming from (last correct commit)
  1. 提交您当前的更改
  2. 解开你的藏匿处并解决冲突
  3. 从 stash 提交更改
  4. 软重置以提交您来自(最后一次正确提交)

回答by knickum

As suggested by @Brandan, here's what I needed to do to get around

正如@Brandan 所建议的,这是我需要做的才能绕过

error: Your local changes to the following files would be overwritten by merge:
       file.txt
Please, commit your changes or stash them before you can merge.
Aborting

Follow this process:

按照这个过程:

git status  # local changes to `file`
git stash list  # further changes to `file` we want to merge
git commit -m "WIP" file
git stash pop
git commit -m "WIP2" file
git rebase -i HEAD^^  # I always use interactive rebase -- I'm sure you could do this in a single command with the simplicity of this process -- basically squash HEAD into HEAD^
# mark the second commit to squash into the first using your EDITOR
git reset HEAD^

And you'll be left with fully merged localchanges to file, ready to do further work/cleanup or make a single good commit. Or, if you know the merged contents of filewill be correct, you could write a fitting message and skip git reset HEAD^.

并且您将获得完全合并的本地更改file,准备做进一步的工作/清理或进行一次良好的提交。或者,如果您知道 的合并内容file是正确的,您可以写一条合适的消息并跳过git reset HEAD^

回答by rogerdpack

Another option is to do another "git stash" of the local uncommitted changes, then combine the two git stashes. Unfortunately git seems to not have a way to easily combine two stashes. So one option is to create two .diff files and apply them both--at lest its not an extra commit and doesn't involve a ten step process :|

另一种选择是对本地未提交的更改进行另一个“git stash”,然后组合两个 git stash。不幸的是,git 似乎没有办法轻松地组合两个存储。因此,一种选择是创建两个 .diff 文件并同时应用它们 - 至少它不是额外的提交并且不涉及十步过程:|

how to: https://stackoverflow.com/a/9658688/32453

如何:https: //stackoverflow.com/a/9658688/32453