什么意思?
在交互式 rebase 过程中,有没有比解析 .git/rebase-merge/head-name
更好的方法来查找正在被 rebase 的分支名称?
细节
通常我使用 git rev-parse --abbrev-ref HEAD
来获取分支名称。但在 rebase 过程中,该分支处于分离头状态,rev-parse 返回 HEAD
。
所以现在我正在解析 .git/rebase-merge/head-name
文件(如果存在)以提取分支名称。是否有一种方法(瓷器或其他方式)可以获取此数据?
用法:
git checkout "the_branch_name_I_want"
git rebase -i "some_other_branch_sha_etc"
# mark commit for edit ...
git magic-command # I'd like to get back "the_branch_name_I_want"
git rebase --continue
为什么?
我为什么想要做这个呢?
我将关于分支的元数据储存起来,并在commit-msg
钩子中获取它,以便将其附加到我的提交信息中。我希望在交互式rebase期间使用reword
或edit
来修改提交信息时它也能正常工作。
代码:
branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ "$?" -ne 0 ]; then
echo "not in a git repo!"
exit 2
fi
if [ "$branch" = "HEAD" ]; then
# check if we are in a rebase
head_name_file="$(git rev-parse --git-dir)/rebase-merge/head-name"
if [ -f "${head_name_file}" ]; then
branch=$(cut -f3- -d/ $head_name_file)
else
# ignore DETACHED HEAD state.
exit 1
fi
fi
## do something with branch
git status
(非--porcelain
)会告诉你正在进行 rebase,并在可用时显示分支名称。它从rebase-merge/head-name
或rebase-apply/head-name
或BISECT_START
中获取。这个status
输出的格式没有文档说明,但与其使用git status
读取相同的文件,最好还是阅读它,因为至少你只需要一个集中的地方而不是三个。 (顺便说一句,你应该使用git rev-parse --git-dir
而不是假设.git
。) - torek--git-dir
和--show-toplevel
+ `/.git/',我给出了 ++。谢谢。 - spazmgit branch --edit-description
在分离头状态下无法使用。 - spazm