在最近的一篇答案中,torek详细介绍了git-reset
的三个最常用选项(--hard
、--mixed
和--soft
)的典型用例,在此过程中,他提到git-reset
还提供了两个相对晦涩的标志,称为--merge
和--keep
。 git-reset
手册页如下所述:
--merge
Resets the index and updates the files in the working tree
that are different between <commit> and HEAD, but keeps
those which are different between the index and working tree
(i.e. which have changes which have not been added). If a
file that is different between <commit> and the index has
unstaged changes, reset is aborted.
In other words, --merge does something like a git read-tree
-u -m <commit>, but carries forward unmerged index entries.
--keep
Resets index entries and updates files in the working tree
that are different between <commit> and HEAD. If a file that
is different between <commit> and HEAD has local changes,
reset is aborted.
我完全理解何时使用--hard
、--mixed
或--soft
,但在阅读torek的回答时才知道--merge
和--keep
也存在,我想不到这两个标志的实际用途...你通常在什么情况下使用这两个标志?我主要寻求一个简明易懂的解释。请以VonC的这个回答为模型,其中详细说明了git reset --soft
的典型用例:
不过,我也不反对尝试一下那些标志,类似于我在我的这个回答中发表的愚蠢购物清单示例。[...] 每当:
- 你对最终结果感到满意(工作树和索引方面)
- 你对获取到这里的所有提交都不满意:
git reset --soft
就是答案。
buggy-file.c
,并且当您从git reset --keep fix-bug-1234
切换时,如果分支devel
和分支fix-buf-1234
中的buggy-file.c
不同,则重置将失败。因此,需要使用--merge
选项。 - Dietrich Epp--merge
的描述,如果情况是这样的:buggy-file.c
在索引和<commit>
之间是不同的(在devel
中与release
不同),并且它有未暂存的更改(我们修改了工作树),因此git reset --merge
也将中止重置。我感觉手册描述有些不对。源代码表明,在冲突合并的中间允许使用--merge
,而--soft
和--keep
则不行。 - torek