给定SHA-1哈希值,是否有方法确定提交来自哪个分支?
如果你能告诉我如何使用Ruby Grit实现这一点,那就加分。
给定SHA-1哈希值,是否有方法确定提交来自哪个分支?
如果你能告诉我如何使用Ruby Grit实现这一点,那就加分。
当前回答
简单的答案是Git不存储提交的分支的名称。试图重建这些信息的技巧似乎在所有情况下都不起作用。
其他回答
要查找本地分支机构,请执行以下操作:
grep -lR YOUR_COMMIT .git/refs/heads | sed 's/.git\/refs\/heads\///g'
要查找远程分支,请执行以下操作:
grep -lR $commit .git/refs/remotes | sed 's/.git\/refs\/remotes\///g'
khichar.anil在回答中涵盖了大部分内容。
我只是添加了一个标志,它将从修订名称列表中删除标记。这给了我们:
git name-rev --name-only --exclude=tags/* $SHA
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 #"
作为一个实验,我制作了一个提交后挂钩,它在提交元数据中存储关于当前签出的分支的信息。我还略微修改了gitk以显示该信息。
你可以在这里查看:https://github.com/pajp/branch-info-commits
gitbranch--contains<ref>是最明显的“瓷”命令。如果您只想使用“管道”命令执行类似的操作:
COMMIT=$(git rev-parse <ref>) # expands hash if needed
for BRANCH in $(git for-each-ref --format "%(refname)" refs/heads); do
if $(git rev-list $BRANCH | fgrep -q $COMMIT); then
echo $BRANCH
fi
done
(此SO答案的交叉点)