如何在`git stash list`中列出存储的父提交 [英] How to list the parent commit of a stash in `git stash list`

查看:99
本文介绍了如何在`git stash list`中列出存储的父提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在 git 中生成存储时,会有一个父项"(在我保存更改之前的最后一次提交).

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

当我使用 git stash 存储我的更改时,该父提交的ID将添加到描述我的存储的消息中.调用 git stash list 可以例如显示:

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

但是当我运行 git stash保存我自己的消息" 时,未添加上级提交的ID( git stash列表):

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

是否有一种方法可以将父提交的ID显示到存储列表中?

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

我尝试过: git stash list --oneline --parents ,这给了我:

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

但是这里显示了错误的ID.我期望(第一行是parent-commit的ID,在此示例中,这对于两个提交的组是相同的):

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

推荐答案

如果您希望在提供的消息中包含ID,则可以将ID作为消息的一部分提供.也就是说,而不是:

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"

您可以运行:

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

(您可以将其转换为别名-Shell别名或调用Shell的git别名).

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

如果要使用实际存储在树中的父ID,则必须深入研究 git stash 的实现.有关许多详细信息,请参见此答案,但总之,工作树的第一父提交 w ( refs/stash 或reflog条目,指向此 w 提交)是在提交时为 HEAD 的提交藏起来了.

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.

git stash list 子命令只是直接将其他参数直接传递给 git log ,因此-oneline --parents 会执行此操作使用 git log 进行操作-除了 git stash list 这样做:

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

(其中"$ @" 是您的附加参数).不幸的是,-parents 使用了历史记录简化和父级重写功能(请参见

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

(顺便说一句,我在这里看到显式的-first-parent 的唯一原因是使-parents 隐藏索引和可选的额外提交.无论如何,-parents 都会被reflog程序所破坏,这似乎是没有意义的.d看到了您想要的东西.所以这可能是一个git bug,尽管这里有很多意想不到的猜测.)

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

您可以(通过某种方式)返回到原始提交ID( w 提交)并使用 git rev-parse 查找第一个提交ID-每个 w 提交的父项:

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

这篇关于如何在`git stash list`中列出存储的父提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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