我对我的分支进行了一些更改,并意识到我忘记了我已经对所述分支进行了一些其他必要的更改。我想要的是一种将我隐藏的更改与当前更改合并的方法。
有没有办法做到这一点?
更多的是为了方便,我最终放弃并首先提交了我当前的更改,然后是我隐藏的更改,但我更愿意一举将它们纳入其中。
先跑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"
...这可能是您正在寻找的。
运行git stash pop
orgit 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
提交,但这可能会带来更多麻烦。
我想要的是一种将我隐藏的更改与当前更改合并的方法
这是另一种选择:
git stash show -p|git apply
git stash drop
git stash show -p
将显示最后保存的存储补丁。git apply
将应用它。合并完成后,可以使用 删除合并的存储git stash drop
。
我这样做的方法是先git add
这样做git stash apply <stash code>
。这是最简单的方法。
正如@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^
并且您将得到完全合并的本地更改file
,准备进行进一步的工作/清理或进行一次良好的提交。或者,如果您知道 的合并内容file
是正确的,您可以写一条合适的消息并跳过git reset HEAD^
。
可能,合并(通过 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
你可以很容易
另一种选择是对本地未提交的更改进行另一个“git stash”,然后合并两个 git stash。不幸的是,git 似乎没有办法轻松组合两个存储。因此,一种选择是创建两个 .diff 文件并同时应用它们——至少它不是额外的提交并且不涉及十步过程:|