如何仅使用管道命令复制git-checkout?

时间:2014-07-23 21:44:50

标签: git git-checkout git-plumbing

我想避免从我的脚本中调用瓷器命令,但有没有办法只使用像git checkout <commit>这样的管道命令来获取checkout-index的某些行为?我对工作副本的影响特别感兴趣:假设一切都很干净,checkout删除在旧HEAD中跟踪但在新HEAD中不存在的文件。 checkout-index似乎没有任何删除文件的概念。我能想到的最接近的事就是打电话

git diff-tree -p <old> <new> | git apply

但计算整个差异似乎不必要的昂贵。还有更好的方法吗?

1 个答案:

答案 0 :(得分:10)

您正在寻找the two-tree git read-tree -um。它使用基础树(通常是您提供它HEAD),目标树和(隐式)索引和工作树。描述它的行为的表格对我来说很难理解,因此我有自己的备忘单,无论如何,重新格式化的对我来说更有意义。无论如何,它实现了git checkout

git read-tree -um H M  # `I` is the (implicit) index, GIT_INDEX_FILE

                    Legend

        H       Original tree (usually HEAD:)
        I       Indexed tree
        M       Merge target tree

        H->I     \
        H->M      } status in second relative to first
        I->M     /

        "-"     file exists in neither
       new      exists only in second
       deleted  exists only in first
       same     exists in both, unchanged
       changed  exists in both, different
      (blank)   irrelevant or all cases not otherwise given

        keep    keep current version
        fail    whole command fails, no changes
       delete   delete worktree file


      H->I      H->M      I->M

                          same     keep

     deleted   changed             fail
     deleted   deleted             delete
     deleted    same               delete unless Index empty, else use M
                same               keep

      same     changed             worktree clean: use M; dirty: fail
      same     deleted             worktree clean: deleted; dirty: fail

      new        -                 keep
      new       new      changed   fail
     changed   changed   changed   fail
     changed   deleted             fail


note: "index empty" identifies an initial checkout, where HEAD has been
set but never loaded.  git can't currently distinguish between a
delete-everything index and an initial-checkout index.