219

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

有没有办法做到这一点?

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

4

8 回答 8

332

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"

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

于 2013-05-17T16:25:02.120 回答
74

运行git stash poporgit 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提交,但这可能会带来更多麻烦。

于 2012-07-26T18:45:13.540 回答
29

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

这是另一种选择:

git stash show -p|git apply
git stash drop

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

于 2016-12-29T15:22:17.130 回答
1

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

于 2019-03-14T08:56:44.807 回答
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^

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

于 2016-01-13T16:15:59.223 回答
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
于 2019-07-19T12:22:00.877 回答
0

你可以很容易

  1. 提交您当前的更改
  2. 解开你的藏匿处并解决冲突
  3. 从 stash 提交更改
  4. 软重置以提交您来自(最后一次正确的提交)
于 2020-02-14T10:04:39.673 回答
-1

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

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

于 2016-05-13T20:52:23.040 回答