Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to list the parent commit of a stash in `git stash list`

Tags:

git

git-stash

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
like image 601
Edward Avatar asked Feb 18 '15 18:02

Edward


1 Answers

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

like image 92
torek Avatar answered Oct 17 '22 07:10

torek