kan
kan

Reputation: 28951

Why git stash creates two commits

This is a history graph with a stash ref using git log --graph --all refs/stash:

*   commit e5e6f3db57db6237a4334dc69a9a229f1cd7bd82 (refs/stash)
|\  Merge: 2a1a05a e918d31
| | Author: user <[email protected]>
| | Date:   Sun Jan 12 12:27:13 2020 +0000
| | 
| |     On master: some stash comment
| | 
| * commit e918d315535c6c6ade2dfb0538039d527dd0cd6e
|/  Author: user <[email protected]>
|   Date:   Sun Jan 12 12:27:13 2020 +0000
|   
|       index on master: 2a1a05a init
| 
* commit 2a1a05a258d3877ed1f6d32dfd57ae1941530418 (HEAD -> master)
  Author: user <[email protected]>
  Date:   Sun Jan 12 12:09:51 2020 +0000

      init

Why commit e5e6 is needed?

Upvotes: 4

Views: 128

Answers (1)

VonC
VonC

Reputation: 1323393

It looks like a regular stash commit

A stash entry 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 entry was created.
The tree of the second parent records the state of the index when the entry 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.

As explained by torek in "How is a stash entry a child commit of HEAD commit and index's commit?"

the stash entry represented by commit W and commit I, but W suffices to find I.
Either of W or I suffices to find H, and git stash will find all three commits automatically.

See also this answer about restoring a stash.

Upvotes: 4

Related Questions