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

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


当前回答

TL;博士:

如果您关心shell退出状态,请使用以下内容:

branch current-当前分支的名称分支名称-干净的分支名称(每行一个)分支名称-确保仅从分支名称返回一个分支

分支名称和分支名称都接受提交作为参数,如果没有给出,则默认为HEAD。


在脚本编写中有用的别名

branch-current = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
branch-names = !"[ -z \"$1\" ] && git branch-current 2>/dev/null || git branch --format='%(refname:short)' --contains \"${1:-HEAD}\" #"  # https://stackoverflow.com/a/19585361/5353461
branch-name = !"br=$(git branch-names \"$1\") && case \"$br\" in *$'\\n'*) printf \"Multiple branches:\\n%s\" \"$br\">&2; exit 1;; esac; echo \"$br\" #"

只能从一个分支进行提交

% git branch-name eae13ea
master
% echo $?
0

输出至STDOUT退出值为0。


可从多个分支访问的提交

% git branch-name 4bc6188
Multiple branches:
attempt-extract
master%
% echo $?
1

输出至STDERR退出值为1。


由于退出状态,可以安全地构建这些。例如,要获取用于获取的远程:

remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"

其他回答

TL;博士:

如果您关心shell退出状态,请使用以下内容:

branch current-当前分支的名称分支名称-干净的分支名称(每行一个)分支名称-确保仅从分支名称返回一个分支

分支名称和分支名称都接受提交作为参数,如果没有给出,则默认为HEAD。


在脚本编写中有用的别名

branch-current = "symbolic-ref --short HEAD"  # https://stackoverflow.com/a/19585361/5353461
branch-names = !"[ -z \"$1\" ] && git branch-current 2>/dev/null || git branch --format='%(refname:short)' --contains \"${1:-HEAD}\" #"  # https://stackoverflow.com/a/19585361/5353461
branch-name = !"br=$(git branch-names \"$1\") && case \"$br\" in *$'\\n'*) printf \"Multiple branches:\\n%s\" \"$br\">&2; exit 1;; esac; echo \"$br\" #"

只能从一个分支进行提交

% git branch-name eae13ea
master
% echo $?
0

输出至STDOUT退出值为0。


可从多个分支访问的提交

% git branch-name 4bc6188
Multiple branches:
attempt-extract
master%
% echo $?
1

输出至STDERR退出值为1。


由于退出状态,可以安全地构建这些。例如,要获取用于获取的远程:

remote-fetch = !"branch=$(git branch-name \"$1\") && git config branch.\"$branch\".remote || echo origin #"

例如,要发现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

gitcheckout<SHA>->将使您进入分离的HEAD状态。

git rev parse--abbrev ref HEAD->将打印HEAD,因此此命令在这种状态下不起作用。

现在我们处于分离HEAD状态,可以使用以下命令获取分支名称。PS:如果您没有处于分离的HEAD状态,这将不起作用!

git branch -a --contains HEAD | sed -n 2p | awk '{ printf $1 }'

瞧!现在您有了分支名称。

简单的答案是Git不存储提交的分支的名称。试图重建这些信息的技巧似乎在所有情况下都不起作用。

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

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

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