什么是git-reset的--merge和--keep标志的典型用例? [英] What are typical use cases of git-reset's --merge and --keep flags?

查看:481
本文介绍了什么是git-reset的--merge和--keep标志的典型用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近的答案中,他详细介绍了 git-reset 的三个最常用的选项( - hard - mixed - soft ), torek 提及 git-reset 还提供了两个相对深奥的标志,称为 - merge - keep git-reset 手册页如下描述这两个标志:


   - 合并

重置索引并更新工作树
中不同于< commit>的不同的文件。和HEAD,但是在索引和工作树
之间保持不同的
(即没有添加的变化)。如果
文件与< commit>不同,并且该索引具有
未分离的更改,则重置将中止。

换句话说,--merge做一些类似于git读取树
-u -m 的东西,但是携带未合并的索引条目。

--keep
重置索引条目并更新工作树
中不同于< commit>和HEAD。如果一个文件
在< commit>之间不同, HEAD有本地更改,
重置会中止。


我完全理解何时使用 - -hard - mixed - soft ,但我只知道在阅读torek的答案时, - merge - 保持存在,我想不出实际的用例这两个标志......在什么情况下你通常使用这两个标志?



我主要是在寻找一个简单英语的解释。请参阅以下关于 VonC回答的文章,作为一个模型: $ b


[...]每次:


  • 您对最终结果感到满意(在工作树和索引方面)

  • 您不满意所有提交给您的提交:


git reset --soft 就是答案。


然而,我并没有回避对这些标志进行一些小实验,类似于我在我的这个答案中发布的愚蠢的购物清单示例。

解决方案

坦率地说,我对此并不确定;我从来没有使用 - merge - 保持模式。但是,发行说明指出,在git版本1.6.2中添加了 git reset --merge ,注意事项如下:




  • git reset --merge 是一种类似于
    git checkout 开关分支,取得本地更改,而
    切换到另一个提交。



- keep 已添加到1.7.1中:




  • git reset learn - 保留选项,可让您放弃在提示附近提交
    ,同时以类似$ b的方式保留本地更改$ b转换为 git checkout分支



然后在1.7。 9:


  • git checkout -B<当前分支> <其他> 是一种更直观的
    拼写方式 git reset --keep<其他>



  • 它告诉我们 - keep 后面的想法是,你已经开始在某个分支上工作,然后你意识到:哦,这个分支应该从其他点(也许是其他分支的尖端)分离出来。举例来说,你可能会:

    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

    然后做一些工作来修复下一个版本的bug 1234;但后来有人说:嘿,我们需要修复旧版本的bug 1234!所以,现在你想要 fix-bug-1234 release 而不是 devel 。同时你还没有做任何事情。所以你:

      $ git checkout -B fix-bug-1234 release 

    将其移至即将发布而非即将发布。它适用于1.7.9或更高版本,但在1.7.1到1.7.8中,您必须拼写 git reset --keep



    这可能解释 - 保留 - merge 仍然有点神秘。


    In a recent answer in which he details the typical use cases of git-reset's three most commonly used options (--hard, --mixed, and --soft), torek mentions in passing that git-reset also offers two relatively esoteric flags, called --merge and --keep. The git-reset man page describes those two flags as follows:

    --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.
    

    I perfectly understand when to use --hard, --mixed, or --soft, but I only learned that --merge and --keep existed while reading torek's answer, and I can't think of practical use cases of those two flags... In what situations do you typically use those two flags?

    I'm mainly looking for a plain-English explanation. Take the following passage of this answer by VonC, which spells out a typical use case for git reset --soft, as a model:

    [...] each time:

    • you are satisfied with what you end up with (in term of working tree and index)
    • you are not satisfied with all the commits that took you to get there:

    git reset --soft is the answer.

    However, I'm not avert to a little experiment with those flags, similar in spirit to the silly shopping-list example I posted in this answer of mine.

    解决方案

    Frankly I'm not really sure about this; I've never even used the --merge and --keep modes myself. However, the release notes indicate that git reset --merge was added in git version 1.6.2, with the following note:

    • git reset --merge is a new mode that works similar to the way git checkout switches branches, taking the local changes while switching to another commit.

    and --keep was added in 1.7.1:

    • git reset learned --keep option that lets you discard commits near the tip while preserving your local changes in a way similar to how git checkout branch does.

    Then, in 1.7.9:

    • git checkout -B <current branch> <elsewhere> is a more intuitive way to spell git reset --keep <elsewhere>.

    which tells us that the idea behind --keep is, you have started work on some branch, and then you realize: oh, this branch should fork off from some other point (probably the tip of some other branch). For instance you might:

    $ git checkout devel
    $ git checkout -b fix-bug-1234
    

    and then do some work to fix bug 1234 for the next release; but then someone says: "hey, we need bug 1234 fixed for the old release version!" So now you want fix-bug-1234 to branch off from release instead of devel. Meanwhile you haven't committed anything yet. So you:

    $ git checkout -B fix-bug-1234 release
    

    to move it to "coming off release" instead of "coming off devel". Which works in 1.7.9 or later, but in 1.7.1 through 1.7.8 you have to spell it git reset --keep.

    That might explain --keep but --merge is still a bit of a mystery.

    这篇关于什么是git-reset的--merge和--keep标志的典型用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆