我想在HEAD
创建一个新分支,保持与我当前所在分支相同的远程跟踪分支,然后检查新分支。这是我正在尝试使用的命令:
$ git checkout --track @{upstream} -b feature/cleanup-gradle HEAD
fatal: 'HEAD' is not a commit and a branch 'feature/cleanup-gradle' cannot be created from it
我不确定为什么我收到上述错误。有没有办法使这项工作?
答案 0 :(得分:1)
显然(基于错误),不使用任何现有的Git命令。不过,您当然可以编写自己的Git命令。将下面的脚本放在可执行的位置(我使用$HOME/scripts/
进行此类操作),将其称为git-nbranch
,确保 可执行,然后运行git nbranch
:
$ git nbranch
usage: git nbranch [options] newname
Create new branch but set its upstream to the current branch's upstream.
If and when this succeeds, you're on the new branch.
-s, --start ... starting commit for new branch (default HEAD)
此脚本说明了可用于编写Git命令的一些技术。
#! /bin/sh
OPTIONS_KEEPDASHDASH=
OPTIONS_STUCKLONG=
OPTIONS_SPEC="git nbranch [options] newname
Create new branch but set its upstream to the current branch's upstream.
If and when this succeeds, you're on the new branch.
--
s,start= starting commit for new branch (default HEAD)
"
# parse options (defined above) and obtain "fatal" function etc.
. git-sh-setup
start=HEAD
while :; do
case "$1" in
--) shift; break;;
-s) start="$2"; shift 2;;
esac
done
case $# in 1) ;; *) usage; esac
# optional - we can just let git checkout -b check later
#ref=$(git check-ref-format --normalize "refs/heads/$1") ||
# die "fatal: $1 is not a valid branch name"
#ref=${ref#refs/heads/} # make suitable for -b
ref="$1"
# compute settings
curbranch=$(git symbolic-ref -q --short HEAD) ||
die "fatal: not on a branch"
case "$start" in
HEAD) startrev=$(git rev-parse -q --verify HEAD) ||
die "fatal: current branch does not exist yet";;
*) startrev=$(git rev-parse -q --verify "$start"^{commit}) ||
die "fatal: $start does not name an existing commit";;
esac
remote=$(git config --get branch.$curbranch.remote) ||
die "fatal: current branch $curbranch has no remote"
merge=$(git config --get branch.$curbranch.merge) ||
die "fatal: current branch $curbranch has no upstream"
git checkout -b "$ref" "$startrev" ||
die "fatal: unable to create and switch to $ref"
git config branch."$ref".remote "$remote" &&
git config branch."$ref".merge "$merge"
答案 1 :(得分:1)
HEAD
确实指向提交。混淆是由于参数中的语义错误和git bug所致,两者均在
https://marc.info/?m=159008858830234。从该消息:
[…] you can't do what you want with a single checkout command. I think:
git checkout -b work HEAD
git branch --set-upstream-to=master
答案 2 :(得分:0)
只需明确HEAD
关闭:
$ git checkout --track @{u} -b new
Branch 'new' set up to track remote branch 'next' from 'origin'.
Switched to a new branch 'new'
$
没有指定提交或路径的 git checkout
除了选项效果之外是无操作,这是你想要的。也就是说,你得到的错误肯定会更好。
答案 3 :(得分:0)
我创建了一个简单的别名来解决这个问题:
cob = "!f() { : git branch ; git checkout -b \"$@\" && git branch -u @{-1}@{upstream}; }; f"
用法(在跟踪master
的{{1}}上):
origin/master
结果:
$ git cob topic1
答案 4 :(得分:0)
discussion mentioned in the git mailing list的Dana Dahlstrom的answer导致Git 2.28(2020年第三季度)已得到修复,因为来自“ git checkout -b foo -t bar baz
”的错误消息令人困惑。
请参见commit bb2198f的commit 16ab794,René Scharfe (rscharfe
)(2020年5月24日)。
(由Junio C Hamano -- gitster
--在commit de82fb4中合并,2020年6月2日)
checkout
:使用额外的参数改进-b
的错误消息原始修补程序:杰夫·金
签名人:RenéScharfe当我们尝试基于“
foo
”创建分支“origin/master
”并为git checkout -b
提供额外的不受支持的参数“bar
”时,它会令人困惑地报告: / p>$ git checkout -b foo origin/master bar fatal: 'bar' is not a commit and a branch 'foo' cannot be created from it $ git checkout --track -b foo origin/master bar fatal: 'bar' is not a commit and a branch 'foo' cannot be created from it
那是错误的,因为它非常理解“
origin/master
”应该是新分支的起点,而不是“bar
”。检查我们是否已提交并在这种情况下显示更多合适的消息:
$ git checkout -b foo origin/master bar fatal: Cannot update paths and switch to branch 'foo' at the same time. $ git checkout --track -b foo origin/master bar fatal: '--track' cannot be used with updating paths