如何撤销部分的git储藏?

3

我有一个脏工作目录,使用git stash save -p来选择性地存储一些更改。我的意图是将以前的一个大提交拆分成两个较小的提交。现在的问题是我不小心存储了错误的内容,所以我想重新做一遍。我尝试过执行git stash pop,如这个问题中建议的那样,但执行时却出现了以下错误:

error: Your local changes to the following files would be overwritten by merge:
    my_file.js
Please, commit your changes or stash them before you can merge.
Aborting
1个回答

6
对于我来说,以下内容就足够了:
% git add myfile.js
% git stash pop
% git reset myfile.js

或者,提交myfile.js,然后弹出stash,解决任何冲突。要摆脱虚拟提交,使用git reset --soft HEAD^。类似这样:

% git add myfile.js
% git commit
% git stash pop
    # resolve conflicts if needed
% git reset --soft HEAD^

--soft 表示不要更改工作树或索引; HEAD^ 表示上一次提交之前的提交。

手册实际上提到了这种行为,尽管我直到现在才注意到它:

pop [--index] [-q|--quiet] [<stash>]

Remove a single stashed state from the stash list and apply it on top of the current working tree state, i.e., do the inverse operation of git stash save. The working directory must match the index.

当我们执行git add命令时,我们会使索引和工作目录匹配(它们都包含了未存储的更改)。同样地,当我们创建一个提交时,它们也会匹配(它们都不包含任何更改)。(强调是我的)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接