将隐藏的更改与当前更改合并

问题描述 投票:0回答:10

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

有办法做到这一点吗?

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

git git-merge git-stash
10个回答
396
投票

tl;博士

先运行

git add


我刚刚发现,如果将未提交的更改添加到索引中(即“暂存”,使用

git add ...
),那么
git stash apply
(大概还有
git stash pop
)实际上会进行正确的合并。如果没有冲突,你就是黄金。如果没有,请照常使用
git mergetool
解决它们,或使用编辑器手动解决。

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

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"

...这可能就是您正在寻找的。


80
投票

运行

git stash pop
git 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

在这种情况下,您无法一步将存储应用到当前的更改。如果您确实不想要两次提交,您可以提交更改,应用存储,再次提交,然后使用

git rebase
压缩这两次提交,但这可能会带来更多麻烦。


35
投票

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

这是另一种选择:

git stash show -p|git apply
git stash drop

git stash show -p
将显示上次保存的存储的补丁。
git apply
将应用它。合并完成后,可以使用
git stash drop
删除合并的存储。


3
投票

我这样做的方法是先

git add
,然后
git stash apply <stash code>
。这是最简单的方法了


1
投票

你可以轻松

  1. 提交当前更改
  2. 解开你的藏品并解决冲突
  3. 从存储中提交更改
  4. 软重置以提交您来自的位置(最后一次正确的提交)

0
投票

根据@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

遵循此过程:

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^

您将留下完全合并的 local 更改为

file
,准备好进行进一步的工作/清理或进行一次良好的提交。或者,如果您知道
file
的合并内容是正确的,您可以编写一条合适的消息并跳过
git reset HEAD^


0
投票

也许,从……是的……分支合并(通过 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

0
投票

我找到了另一个解决方案。您可以提交当前打开的更改,然后弹出您的存储,然后软重置到上次提交之前。

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

0
投票

根据您的更改以及它们是否实际上包含真正的合并冲突或只是更改同一文件的不同部分,这种方法也可能适合您:

假设您的存储{0}对 file_a.txt 进行了更改,并且您的工作目录对同一 file_a.txt 进行了其他更改,并且您希望在同一次提交中进行这两项更改。

直接应用 stash{0} 会导致警告:

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

你能做的是:

  1. 通过
    git add file_a.txt
  2. 将本地更改添加到暂存区
  3. git stash apply stash{0}
  4. 通过
    git restore --staged
  5. 取消暂存 file_a.txt
  6. 通过
    git add file_a.txt
    git add --patch file_a.txt
  7. 重新暂存整个文件或仅部分部分
  8. 创建包含所有更改的提交

-1
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.