为什么隐藏表示为2次提交? [英] Why is a stash represented as 2 commits?

查看:154
本文介绍了为什么隐藏表示为2次提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 当存储一些更改时,Git会创建两个单独的提交,'WIP on branch'和'index on branch' $ git log --graph --all 

* commit 98aac13303ca086580c1ec9ccba5fe26c2a8ef3c
| \合并:7d99786 82c5c76
| |作者:Tieme< my@email.com>
| |日期:Wed Nov 19 09:58:35 2014 +0100
| |
| | WIP开发:7d99786最后真实提交
| |
| *提交82c5c763357c401135675a39bfabf9b7f6805815
| / Author:Tieme< my@email.com>
|日期:Wed Nov 19 09:58:35 2014 +0100
|
|开发索引:7d99786最后真实提交
|
|
| * commit 7d9978637a0e1ef92f2432189bdebf2317f0b2f0
|作者:Tieme< my@email.com>
|日期:二月十八日星期二17:32:33 2014 +0100
|
|最后一次提交
|

我抬头看文档,但它并没有让它更清晰:


一个存储被表示为一个提交,它的树记录了工作目录的状态,它的第一个父节点是在创建存储时在HEAD处的提交。第二个父节点的树记录了存储时的索引状态,并将其作为HEAD提交的子节点。祖先图看起来像这样:

  .---- W 
/ /
--- --H ---- I

其中H是HEAD提交,I是提交记录索引的状态,W是一个提交,它记录工作树的状态。

为什么有2个提交创建并且不仅仅是一个我改变的文件?

解决方案

简短回答




$ b

长答案



一些背景 使用git有可能您在工作树中所做的更改(直接使用的文件)与您对索引所做的更改不同。



仅添加一个索引数量有限,或者使用 git add --patch 甚至在文件中添加单个更改的行会使索引处于与工作树不同的状态。

这是一个好东西,因为它使您能够创建集中于一个特定任务/功能的提交。



甚至有可能您的索引有变化,你的工作树。您可以通过在索引中添加一些更改( git add )来测试它,手动从文件中删除它们,然后创建一个提交( git commit )。

提交将仍然包含您首先添加的更改,尽管它们不再存在于您的工作树中。



如果您无法理解工作树和索引之间的区别,可以查看这个问题

究竟发生了什么?



现在,从一开始的声明应该更有意义。一个隐藏提交包含您在工作树中所做的所有更改(您直接使用的文件),另一个包含您添加到索引中的更改。



默认情况下 git stash pop git stash apply 只会尝试恢复您在工作树中所做的更改并忽略您在索引中所做的更改。
可以通过使用 - index 标志来指示 git stash 来尝试恢复您的索引 git stash(pop | apply)--index )。


When stashing some changes, Git creates two separate commits, 'WIP on branch' and 'index on branch':

$ git log --graph --all 

*   commit 98aac13303ca086580c1ec9ccba5fe26c2a8ef3c
|\  Merge: 7d99786 82c5c76
| | Author: Tieme <my@email.com>
| | Date:   Wed Nov 19 09:58:35 2014 +0100
| |
| |     WIP on development: 7d99786 Last real commit
| |
| * commit 82c5c763357c401135675a39bfabf9b7f6805815
|/  Author: Tieme <my@email.com>
|   Date:   Wed Nov 19 09:58:35 2014 +0100
|
|       index on development: 7d99786 Last real commit
|
|
| * commit 7d9978637a0e1ef92f2432189bdebf2317f0b2f0
| Author: Tieme <my@email.com>
| Date:   Tue Nov 18 17:32:33 2014 +0100
|
|     Last real commit
|

I looked up the documentation for this but it does not make it clearer:

A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created. The tree of the second parent records the state of the index when the stash is made, and it is made a child of the HEAD commit. The ancestry graph looks like this:

         .----W
        /    /
  -----H----I

where H is the HEAD commit, I is a commit that records the state of the index, and W is a commit that records the state of the working tree.

Why are there 2 commits created and not just a single one for the files I changed ?

解决方案

Short answer

git stash differentiates between the changes you have on your index and the one you have in the working tree and creates commits for both of them.

Long answer

Some background

Using git it's possible that the changes you have in your working tree (the files your directly work with) are different from the changes you have on the index.

Adding only a limited number of files to the index, or with git add --patch even adding single changed lines in a file will leave your index in a different state as the working tree.
This is a good thing, because it enables you to create commits which focus on one specific task/feature.

It's even possible that you have changes on your index which aren't anymore part of your working tree. You can test it by adding some changes to the index (git add), manually removing them from the file and then creating a commit (git commit).
The commit will still contain the changes you added in the first place, although they don't exist in your working tree anymore.

If you have trouble understanding the difference between the working tree and the index you can take a look at this question.

What exactly is happening?

Now the statement from the beginning should make more sense. One "stash commit" contains all changes you had in your working tree (the files you directly work with) and the other contains the changes you had added to your index.

By default git stash pop or git stash apply only tries to reinstate the changes you had in your working tree and ignores the changes you had on the index. It's possible to tell git stash to try reinstating your index too by using the --index flag (git stash (pop|apply) --index).

这篇关于为什么隐藏表示为2次提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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