简体   繁体   English

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

[英]git stash -> merge stashed change with current changes

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.更多的是为了方便,我最终放弃并首先提交了我当前的更改,然后是我隐藏的更改,但我更愿意一举将它们纳入其中。

tl;dr tl;博士

Run git add first.首先运行git add


I just discovered that if your uncommitted changes are added to the index (ie "staged", using git add ... ), then git stash apply (and, presumably, git stash pop ) will actually do a proper merge.我刚刚发现,如果您未提交的更改被添加到索引中(即“暂存”,使用git add ... ),那么git stash apply (并且,大概是git stash pop )实际上会进行适当的合并。 If there are no conflicts, you're golden.如果没有冲突,你就是金子。 If not, resolve them as usual with git mergetool , or manually with an editor.如果没有,请像往常一样使用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. ......这可能是你正在寻找的。

Running git stash pop or git stash apply is essentially a merge.运行git stash popgit stash apply本质上是一个合并。 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:您应该不需要提交当前的更改,除非存储中更改的文件也在工作副本中更改,在这种情况下,您会看到以下错误消息:

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.在这种情况下,您无法一步将 stash 应用到您当前的更改。 You can commit the changes, apply the stash, commit again, and squash those two commits using git rebase if you really don't want two commits, but that may be more trouble that it's worth.如果您真的不想要两次提交,您可以提交更改,应用存储,再次提交,并使用git rebase这两个提交,但这可能会带来更多的麻烦,但值得。

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 -p will show the patch of last saved stash. git stash show -p将显示上次保存的存储的补丁。 git apply will apply it. git apply将应用它。 After the merge is done, merged stash can be dropped with git stash drop .合并完成后,可以使用git stash drop删除合并的存储。

The way I do this is to git add this first then git stash apply <stash code> .我这样做的方法是先git add这个然后git stash apply <stash code> It's the most simple way.这是最简单的方法。

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 local changes to file , ready to do further work/cleanup or make a single good commit.并且您将得到完全合并的本地更改file ,准备做进一步的工作/清理或进行一次良好的提交。 Or, if you know the merged contents of file will be correct, you could write a fitting message and skip git reset HEAD^ .或者,如果您知道file的合并内容是正确的,您可以写一条合适的消息并跳过git reset HEAD^

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

you can easily你可以很容易地

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

I found another solution.我找到了另一个解决方案。 You can commit your current open changes, then pop your stash, and then soft reset to before the last commit.您可以提交当前打开的更改,然后弹出您的存储,然后软重置到上次提交之前。

git commit -am 'Open changes'
git stash pop
git reset --soft HEAD~1

Another option is to do another "git stash" of the local uncommitted changes, then combine the two git stashes.另一种选择是对本地未提交的更改进行另一个“git stash”,然后组合两个 git stash。 Unfortunately git seems to not have a way to easily combine two stashes.不幸的是,git 似乎没有办法轻松地组合两个存储。 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 :|因此,一种选择是创建两个 .diff 文件并同时应用它们 - 至少它不是额外的提交并且不涉及十步过程:|

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

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

相关问题 Git stash:如何查看分支中是否存在隐藏的更改 - Git stash: How to see if there are stashed changes in a branch 有没有更好的方法来执行git stash pop,然后如果我不合并则丢弃隐藏的更改? - Is there a better way of doing a git stash pop and then discard the stashed changes if I don't merge? Git:在隐藏到当前版本之间手动合并? - Git: Manually merge between stashed to current version? 检查“git stash”是否藏匿了什么 - Check if “git stash” stashed anything 有没有办法存储当前的 git 索引,`git add -u`,然后将索引恢复到存储的 state? - Is there a way to stash the current git index, `git add -u`, and then restore the index to the stashed state? Git:合并2变格后,应用于两个对象的隐藏更改 - Git: After merge 2 branges the stashed changes applied to both Git在合并后恢复隐藏的更改--no-ff - Git recover stashed changes after merge --no-ff 如何在git stash pop / apply所需的任何合并中始终优先考虑隐藏版本? - How to always give preference to the stashed version in any merge required by `git stash pop/apply`? 跑了&#39;git checkout stash&#39;,现在想用我藏匿的当前未附加分支覆盖现有分支 - Ran 'git checkout stash' and now want to overwrite an existing branch with my current unattached branch that i had stashed Git stash 只有在之前成功隐藏时才会弹出 - Git stash pop only if successfully stashed before
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM