给定SHA-1哈希值,是否有方法确定提交来自哪个分支?

如果你能告诉我如何使用Ruby Grit实现这一点,那就加分。


当前回答

如果OP试图确定在创建特定提交时分支所遍历的历史(“在给定SHA-1哈希值的情况下,找出提交来自哪个分支”),那么如果没有reflog,Git对象数据库中就没有任何记录显示哪个命名的分支绑定到了什么提交历史。

(我将此作为回复评论的回复发布。)

希望这个脚本能说明我的观点:

rm -rf /tmp/r1 /tmp/r2; mkdir /tmp/r1; cd /tmp/r1
git init; git config user.name n; git config user.email e@x.io
git commit -m"empty" --allow-empty; git branch -m b1; git branch b2
git checkout b1; touch f1; git add f1; git commit -m"Add f1"
git checkout b2; touch f2; git add f2; git commit -m"Add f2"
git merge -m"merge branches" b1; git checkout b1; git merge b2
git clone /tmp/r1 /tmp/r2; cd /tmp/r2; git fetch origin b2:b2
set -x;
cd /tmp/r1; git log --oneline --graph --decorate; git reflog b1; git reflog b2;
cd /tmp/r2; git log --oneline --graph --decorate; git reflog b1; git reflog b2;

输出表明,没有任何方法知道使用“Add f1”的提交是来自远程克隆/tmp/r2的分支b1还是b2。

(此处为输出的最后一行)

+ cd /tmp/r1
+ git log --oneline --graph --decorate
*   f0c707d (HEAD, b2, b1) merge branches
|\
| * 086c9ce Add f1
* | 80c10e5 Add f2
|/
* 18feb84 empty
+ git reflog b1
f0c707d b1@{0}: merge b2: Fast-forward
086c9ce b1@{1}: commit: Add f1
18feb84 b1@{2}: Branch: renamed refs/heads/master to refs/heads/b1
18feb84 b1@{3}: commit (initial): empty
+ git reflog b2
f0c707d b2@{0}: merge b1: Merge made by the 'recursive' strategy.
80c10e5 b2@{1}: commit: Add f2
18feb84 b2@{2}: branch: Created from b1
+ cd /tmp/r2
+ git log --oneline --graph --decorate
*   f0c707d (HEAD, origin/b2, origin/b1, origin/HEAD, b2, b1) merge branches
|\
| * 086c9ce Add f1
* | 80c10e5 Add f2
|/
* 18feb84 empty
+ git reflog b1
f0c707d b1@{0}: clone: from /tmp/r1
+ git reflog b2
f0c707d b2@{0}: fetch origin b2:b2: storing head

其他回答

例如,要发现c0118fa提交来自redesign_interactions:

* ccfd449 (HEAD -> develop) Require to return undef if no digits found
*   93dd5ff Merge pull request #4 from KES777/clean_api
|\
| * 39d82d1 Fix tc0118faests for debugging debugger internals
| * ed67179 Move &push_frame out of core
| * 2fd84b5 Do not lose info about call point
| * 3ab09a2 Improve debugger output: Show info about emitted events
| *   a435005 Merge branch 'redesign_interactions' into clean_api
| |\
| | * a06cc29 Code comments
| | * d5d6266 Remove copy/paste code
| | * c0118fa Allow command to choose how continue interaction
| | * 19cb534 Emit &interact event

您应该运行:

git log c0118fa..HEAD --ancestry-path --merges

然后向下滚动以找到最后一次合并提交。即:

commit a435005445a6752dfe788b8d994e155b3cd9778f
Merge: 0953cac a06cc29
Author: Eugen Konkov
Date:   Sat Oct 1 00:54:18 2016 +0300

    Merge branch 'redesign_interactions' into clean_api

使现代化

或者只需要一个命令:

git log c0118fa..HEAD --ancestry-path --merges --oneline --color | tail -n 1

khichar.anil在回答中涵盖了大部分内容。

我只是添加了一个标志,它将从修订名称列表中删除标记。这给了我们:

git name-rev --name-only --exclude=tags/* $SHA

一个穷人的选择是在HEAD上使用工具tig1,搜索提交,然后直观地从提交开始一直到看到合并提交。默认合并消息应指定要合并到的分支位置:)

1Tig是一个基于ncurses的Git文本模式界面。它的功能主要作为Git存储库浏览器,但它也可以帮助更改块级别的提交,并充当各种Git命令。

我认为有人应该面临同样的问题,无法找到分支,尽管它实际上存在于一个分支中。

你最好先把所有东西都拔出来:

git pull --all

然后执行分支搜索:

git name-rev <SHA>

or:

git branch --contains <SHA>

作为一个实验,我制作了一个提交后挂钩,它在提交元数据中存储关于当前签出的分支的信息。我还略微修改了gitk以显示该信息。

你可以在这里查看:https://github.com/pajp/branch-info-commits