7

When I generate a stash in git, there is a "parent" (the last commit before I stashed my changes).

When I use git stash to stash my changes, an ID to that parent-commit is added to the message describing my stash. calling git stash list can e.g. show:

stash@{0}: WIP on master: c09a3fc second commit
stash@{1}: WIP on master: 063b893 first commit
stash@{2}: WIP on master: 063b893 first commit

But when I run git stash save "My own message" the ID of the parent-commit is not added (git stash list):

stash@{0}: On master: My own message
stash@{1}: WIP on master: c09a3fc second commit
stash@{2}: WIP on master: 063b893 first commit
stash@{3}: WIP on master: 063b893 first commit

It there a way to show the ID of the parent-commit to the list of stashes?

I tried: git stash list --oneline --parents, which gave me:

1b5dfb1 4efd3e0 refs/stash@{0}: On master: My own message
4efd3e0 1e9b384 refs/stash@{1}: WIP on master: c09a3fc second commit
1e9b384 51eb834 refs/stash@{2}: WIP on master: 063b893 first commit
51eb834 refs/stash@{3}: WIP on master: 063b893 first commit

But here the wrong IDs are shown. I expected (the first line beeing the ID of the parent-commit which is the same for groups of two commits in this example):

c09a3fc 1b5dfb1 refs/stash@{0}: On master: My own message
c09a3fc 4efd3e0 refs/stash@{1}: WIP on master: c09a3fc second commit
063b893 1e9b384 refs/stash@{2}: WIP on master: 063b893 first commit
063b893 51eb834 refs/stash@{3}: WIP on master: 063b893 first commit
SQB
  • 3,926
  • 2
  • 28
  • 49
Edward
  • 4,453
  • 8
  • 44
  • 82
  • If you want to do it for a single stash you can do `git show stash@{0}^`, as `stash@{0}` is a valid refname. You could build a small script around that to get the output you want - but that's an exercise for the reader, so this is just a comment :-) – Jan Fabry Feb 18 '15 at 19:36
  • @JanFabry Thanks for your comment. Is `git show stash@{0}^` the answer to my question here? https://stackoverflow.com/questions/62840077/how-to-recover-from-git-added-by-us-conflict-after-applying-stash-after-renami/62842712#comment111147132_62842712 – Ryan Jul 12 '20 at 22:07
  • Also, for people interested in this topic, I noticed that this question is quite similar: https://stackoverflow.com/q/16071720/470749 – Ryan Jul 12 '20 at 22:11

1 Answers1

6

If you want the ID included in the message you supply, you can supply the ID as part of the message. That is, instead of:

$ git stash save "My own message"

you might run:

$ git stash save "[$(git rev-parse --short HEAD)] My own message"

(you might turn this into an alias—either a shell alias, or a git alias that invokes the shell).

If you want to make use of the parent IDs actually stored in the tree, you must delve into the implementation of git stash. See this answer for lots of detail, but in short, the first parent of the work-tree commit w (refs/stash, or the reflog entry, points to this w commit) is the commit that was HEAD at the time the stash was made.

The git stash list sub-command simply passes additional arguments directly to git log, so --oneline --parents does what it does with git log—except that git stash list does it like this:

git log --format="%gd: %gs" -g --first-parent -m "$@" $ref_stash --

(where "$@" are your additional arguments). Unfortunately, --parents uses history simplification and parent rewriting (see the documentation for git rev-list), which with -g, turns into "replace parentage with result of reflog walk", which totally destroys the original parentage information.

(Incidentally, the only reason I can see for the explicit --first-parent here is to make --parents hide the index and optional extra commit. Since --parents get clobbered anyway by the reflog walk, it seems to be pointless. Not sure if the git folks might intend reflog walks not to clobber parent information, and if it didn't clobber, you'd have seen what you wanted. So this might be a git bug, although there is a lot of guessing-at-intentions here.)

You can (sort of) get what you want by going back to the original commit IDs (the w commits) and using git rev-parse to find the first-parent of each w commit:

git log -g --format="%gd %H" refs/stash |
while read name hash; do
    printf "%s %s " $name $(git rev-parse --short $name^)
    git log -1 --format=%s $hash
done

(there may be ways to shorten this, the above is pretty straightforward though).

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
  • In mac os 10.12.6 with git version 2.17.1,I found out the result is printed in a series of less screen, append `| cat` to above command fix the problem. – cmicat Feb 25 '19 at 04:04
  • @cmicat: `git log` uses your pager, or doesn't, based on your configuration settings. Set `core.pager` to control which pager is used; use `git --no-pager` to inhibit the pager for one command; set `pager.` (e.g., `pager.log`) to `true` or `false`. Since `git log` is used for a lot of other things, you might not want to change its defaults here. Note that if you use `less` as your pager, you can set `LESS` options in your environment. If you don't, Git will set them for you, to `FRX`, by default (this is a Git build-time option). – torek Feb 25 '19 at 05:51
  • Thank you for this answer. Would you mind seeing if you can clarify on my similar question here? https://stackoverflow.com/q/62867041/470749 – Ryan Jul 12 '20 at 22:26