在 git 变基期间查找分支名称
find branch name during git rebase
什么?
有没有办法在交互式变基过程中找到正在变基的分支的名称,这比解析更好.git/rebase-merge/head-name
?
详情
通常我使用git rev-parse --abbrev-ref HEAD
来获取分支名称。但是在变基期间,分支处于分离的头部状态和 rev-parse returns 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
挂钩中获取它以附加到我的提交消息中。我希望在交互式变基期间 reword
ing 或 edit
ing 我的提交消息时它能工作。
代码:
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
Is there a way to find the name of the branch-being-rebased during an interactive rebase, that is better than parsing .git/rebase-merge/head-name?
请注意,在 Git 2.18(2018 年第 2 季度)中,中断“rebase -i
”期间的“git branch --list
”现在可以让用户区分分离 HEAD
的情况正在重新设置基础并且正在重新设置普通分支。
因此您仍然需要解析,但命令本身的输出现在很有用。
参见 commit 1f537be (03 Apr 2018) by Eric Sunshine (sunshineco
)。
参见 commit a236f90 (03 Apr 2018) by Kaartic Sivaraam (sivaraam
)。
(由 Junio C Hamano -- gitster
-- in commit 4cbaa6b 合并,2018 年 4 月 25 日)
branch --list
: print useful info whilst interactive rebasing a detached HEAD
When rebasing interactively (rebase -i
), "git branch --list
" prints
a line indicating the current branch being rebased.
This works well when the interactive rebase is initiated when a local branch is
checked out.
This doesn't play well when the rebase is initiated on a detached HEAD.
When "git branch --list
" tries to print information related
to the interactive rebase in this case it tries to print the name
of a branch using an uninitialized variable and thus tries to
print a "null pointer string
".
As a consequence, it does not provide useful information while also inducing undefined behaviour.
So, print the point from which the rebase was started when interactive
rebasing a detached HEAD.
尽管 git branch --list
显示了一个分支名称(有关详细信息,请参阅 ),但它在脚本编写期间没有用,因为您需要解析输出。
我使用以下函数获取分支名称(基于 head-name
文件):
rebasing-branch() {
for location in rebase-merge rebase-apply; do
path=$(git rev-parse --git-path ${location})
if test -d ${path}; then
revision=$(<${path}/head-name)
echo ${revision##refs/heads/}
return 0
fi
done
}
它在常规或交互式 rebase 期间显示分支名称。
什么?
有没有办法在交互式变基过程中找到正在变基的分支的名称,这比解析更好.git/rebase-merge/head-name
?
详情
通常我使用git rev-parse --abbrev-ref HEAD
来获取分支名称。但是在变基期间,分支处于分离的头部状态和 rev-parse returns 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
挂钩中获取它以附加到我的提交消息中。我希望在交互式变基期间 reword
ing 或 edit
ing 我的提交消息时它能工作。
代码:
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
Is there a way to find the name of the branch-being-rebased during an interactive rebase, that is better than parsing .git/rebase-merge/head-name?
请注意,在 Git 2.18(2018 年第 2 季度)中,中断“rebase -i
”期间的“git branch --list
”现在可以让用户区分分离 HEAD
的情况正在重新设置基础并且正在重新设置普通分支。
因此您仍然需要解析,但命令本身的输出现在很有用。
参见 commit 1f537be (03 Apr 2018) by Eric Sunshine (sunshineco
)。
参见 commit a236f90 (03 Apr 2018) by Kaartic Sivaraam (sivaraam
)。
(由 Junio C Hamano -- gitster
-- in commit 4cbaa6b 合并,2018 年 4 月 25 日)
branch --list
: print useful info whilst interactive rebasing a detached HEADWhen rebasing interactively (
rebase -i
), "git branch --list
" prints a line indicating the current branch being rebased.
This works well when the interactive rebase is initiated when a local branch is checked out.This doesn't play well when the rebase is initiated on a detached HEAD.
When "git branch --list
" tries to print information related to the interactive rebase in this case it tries to print the name of a branch using an uninitialized variable and thus tries to print a "null pointer string
".
As a consequence, it does not provide useful information while also inducing undefined behaviour.So, print the point from which the rebase was started when interactive rebasing a detached HEAD.
尽管 git branch --list
显示了一个分支名称(有关详细信息,请参阅
我使用以下函数获取分支名称(基于 head-name
文件):
rebasing-branch() {
for location in rebase-merge rebase-apply; do
path=$(git rev-parse --git-path ${location})
if test -d ${path}; then
revision=$(<${path}/head-name)
echo ${revision##refs/heads/}
return 0
fi
done
}
它在常规或交互式 rebase 期间显示分支名称。