繁体   English   中英

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

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

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

有没有办法做到这一点?

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

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"

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

运行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

在这种情况下,您无法一步将 stash 应用到您当前的更改。 如果您真的不想要两次提交,您可以提交更改,应用存储,再次提交,并使用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

你可以很容易地

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

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

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

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM