变量为“”的Git别名未按预期工作:pathspec与Git已知的任何文件都不匹配

变量为“”的Git别名未按预期工作:pathspec与Git已知的任何文件都不匹配,git,alias,git-bash,Git,Alias,Git Bash,我已经创建了一个别名,用于根据需要跟踪分支。下面是my.gitconfig的[alias]部分的当前行: catchup = !CURRENTBRANCH=$(git symbolic-ref --short HEAD) && echo Currently on $CURRENTBRANCH - switching to $1 && git checkout $1 && git merge origin/$1 && echo Goi

我已经创建了一个别名,用于根据需要跟踪分支。下面是my.gitconfig的
[alias]
部分的当前行:

catchup = !CURRENTBRANCH=$(git symbolic-ref --short HEAD) && echo Currently on $CURRENTBRANCH - switching to $1 && git checkout $1 && git merge origin/$1 && echo Going back to $CURRENTBRANCH && git checkout "$CURRENTBRANCH"
我使用它如下(例如):

此代码导致(例如):

我尝试了别名中的最后一个命令,使用双引号和不使用双引号,结果相同

有人知道最后如何解决这个错误吗

对于那些可能建议使用git pull的人来说,它不能解决我的问题,需要输入我的密码。如果我最近使用了
git fetch
,并且无需返回远程回购,则使用此别名


我正在Windows 7上运行git bash,仅供参考。

对别名使用shell函数:

[alias]

catchup = "!f() { CURRENTBRANCH=$(git symbolic-ref --short HEAD) && .... ;}; f"
在那里,
$n
的处理按预期工作


承包商确认以下工作正常:

catchup = "!_(){ CURRENTBRANCH=$(git symbolic-ref --short HEAD) ; echo \"Currently on \"$CURRENTBRANCH\" - switching to \"$@ ; git checkout $@ ; git merge origin/$@ ; echo \"Going back to \"$CURRENTBRANCH ; git checkout $CURRENTBRANCH; }; _"
在多行中,要获得更高的可见性,请执行以下操作:

catchup = "!_(){ 
  CURRENTBRANCH=$(git symbolic-ref --short HEAD) ; 
  echo \"Currently on \"$CURRENTBRANCH\" - switching to \"$@ ; 
  git checkout $@ ; 
  git merge origin/$@ ; 
  echo \"Going back to \"$CURRENTBRANCH ; 
  git checkout $CURRENTBRANCH; }; _"

晚会迟到了。。它失败的原因是git以

git catchup new_design
# turns
CURRENTBRANCH= ... && git checkout "$CURRENTBRANCH\" new_design
也就是说,它在命令后面附加了“new_design”。这方面的证明很容易证明:

# [alias]
#   proof = !echo fixed text

> git proof tryme
# prints "fixed text tryme", not "fixed text"
因此,另一种选择是使用注释技巧

# [alias]
#   proof = "!echo fixed text #"

> git proof tryme
# prints "fixed text", since command it ranned was
# echo fixed text #tryme

catchup = "!CURRENTBRANCH=$(git symbolic-ref --short HEAD) && echo Currently on $CURRENTBRANCH - switching to $1 && git checkout $1 && git merge origin/$1 && echo Going back to $CURRENTBRANCH && git checkout \"$CURRENTBRANCH\" #"

# multiline version:
catchup = "! \
  CURRENTBRANCH=$(git symbolic-ref --short HEAD) && \
  echo Currently on $CURRENTBRANCH - switching to $1 && \
  git checkout $1 && \
  git merge origin/$1 && \
  echo Going back to $CURRENTBRANCH && \
  git checkout \"$CURRENTBRANCH\" \
#"

注:

  • “!…”
    !“..”
    是等效的
  • 注意“\”结尾(后面有空格),它不起作用
  • 这个技巧即使在使用“所有参数”(
    $*
    )的情况下也能起作用,因为它可以防止任何附加

即使应该是这样,也可以尝试:1/添加一个
git fetch
作为第一个命令,2/
git checkout-t$1 origin/$1
当第一次切换时。您是说这是错误的原因吗?我们的想法是只在已有的跟踪分支上使用它。不管怎样,我还是试过了-它给出了
fatal:缺少分支名称;尝试-b
,然后如果我使用
git checkout-t origin/$1
(看起来更正确),我会得到
致命的:一个名为“new_design”的分支已经存在
你能在最后一次
git checkout
之前添加一个
git分支-a
吗?(为了检查合并后git认为您是哪个分支)是否git会在执行命令字符串时自动将参数(此处为
git catchup new_design
中的
new_design
)附加到命令字符串的末尾?这意味着最后一个命令实际上是git checkout integration new_design,这将给出此错误消息。@VonC:git symbolic ref--short HEAD将是一个更简洁的输出,但是是的,我已经试过了,并确认我在这一点上是在预期的分支上。这将需要比我目前拥有的更多的bash shell脚本技能。快速尝试给出:
f:-c:line 0:意外标记`{CURRENTBRANCH=$(git symbolic ref--short HEAD)附近的语法错误'f:-c:line 0:`f(){CURRENT BRANCH=$(git symbolic ref--short HEAD);echo当前在$CURRENTBRANCH上-切换到$1;git checkout$1;git merge origin/$1;echo返回$CURRENTBRANCH;git checkout$CURRENTBRANCH)};f“$@”
Nevermind,我的技能达到了最新水平。我的结果是:
Cathup=“!”{CURRENTBRANCH=$(git symbol ref——短头);echo\”当前处于启用状态\“$CURRENTBRANCH\”-切换到\“$@;git checkout$@;git merge origin/$@;echo\”返回\“$CURRENTBRANCH;git checkout$CURRENTBRANCH;}_"
@mwotton反馈很好,给Stefan+1。我冒昧地在Stefan的答案中加入了您的结论,以提高可视性。我在哪里可以获得有关“$@”和“@-1”之类的文档?不知道它们是什么?@MarkGibaud我曾经学习bash脚本编写。在我开始这项工作之前,我没有真正的bash脚本编写经验。这本指南让我通过了,但你需要耐心。
# [alias]
#   proof = !echo fixed text

> git proof tryme
# prints "fixed text tryme", not "fixed text"
# [alias]
#   proof = "!echo fixed text #"

> git proof tryme
# prints "fixed text", since command it ranned was
# echo fixed text #tryme

catchup = "!CURRENTBRANCH=$(git symbolic-ref --short HEAD) && echo Currently on $CURRENTBRANCH - switching to $1 && git checkout $1 && git merge origin/$1 && echo Going back to $CURRENTBRANCH && git checkout \"$CURRENTBRANCH\" #"

# multiline version:
catchup = "! \
  CURRENTBRANCH=$(git symbolic-ref --short HEAD) && \
  echo Currently on $CURRENTBRANCH - switching to $1 && \
  git checkout $1 && \
  git merge origin/$1 && \
  echo Going back to $CURRENTBRANCH && \
  git checkout \"$CURRENTBRANCH\" \
#"