使用变量的Git别名没有按预期工作:路径规范与Git已知的任何文件都不匹配。

5

我创建了一个别名来随时追踪我的跟踪分支。这是我.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 catchup new_design

这段代码的结果可能是(例如):
Currently on integration
Switched to branch 'new_design'
Your branch is behind 'origin/new_design' by 1 commit, and can be fast-forwarded.
Updating c82f7db..51eea8a
Fast-forward
 themes/theme1/css/styles.less | 17 +++++++++++++++++
 themes/theme1/js/app.js       |  6 +++---
 2 files changed, 20 insertions(+), 3 deletions(-)
Going back to integration
error: pathspec 'new_design' did not match any file(s) known to git.

我已经尝试在别名中使用双引号和不使用双引号的最后一个命令,结果相同。有人知道如何解决这个错误吗?对于那些可能建议使用 "git pull" 的人来说,它不能解决我的问题,并且需要输入密码。如果我最近使用了 "git fetch",并且不需要返回到远程仓库,则可以使用此别名。值得一提的是,我在 Windows 7 上运行 git bash。

你是说这是错误的原因吗?这个想法只是在预先存在的跟踪分支上使用。我尝试了一下 - 它给出了 fatal: Missing branch name; try -b,然后如果我使用 git checkout -t origin/$1(看起来更正确),我会得到 fatal: a branch named 'new_design' already exists - mwotton
1
你能在最后一个 git checkout 前添加一个 git branch -a 吗?(以检查合并后 git 认为你在哪个分支上) - VonC
2
Git是否会在执行命令时自动将参数(此处为git catchup new_design中的new_design)附加到命令字符串的末尾?这意味着最后一个命令实际上是git checkout integration new_design,从而导致出现此错误消息。 - Philipp Wendler
@VonC:git symbolic-ref --short HEAD 可以输出更简洁的结果,但是我已经尝试过并确认在那个时刻我在预期的分支上。 - mwotton
@PhilippWendler 这正是所发生的 - 如下面所示:https://dev59.com/fGsz5IYBdhLWcg3w-85v#1qCdEYcBWogLw_1bVVYS - mwotton
显示剩余2条评论
2个回答

3
使用一个 shell 函数来创建别名:
[alias]

catchup = "!f() { CURRENTBRANCH=$(git symbolic-ref --short HEAD) && .... ;}; f"

在那里,$n 的处理按预期工作。


OP mwotton确认在评论中以下工作:

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; }; _"

这需要比我当前掌握的更多的Bash shell脚本技能。快速尝试后出现以下错误:f: -c: line 0: syntax error near unexpected token \{CURRENTBRANCH=$(git symbolic-ref --short HEAD)' f: -c: line 0: `f(){CURRENT BRANCH=$(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)}; f "$@"'` - mwotton
没关系,我已经把我的技能提升到了标准。最终我得到了这个:catchup = "!_(){ CURRENTBRANCH=$(git symbolic-ref --short HEAD) ; echo \"当前在 \"$CURRENTBRANCH\" - 切换到 \"$@ ; git checkout $@ ; git merge origin/$@ ; echo \"回到 \"$CURRENTBRANCH ; git checkout $CURRENTBRANCH; }; _" - mwotton
@mowtton 给出了好的反馈,Stefan也点赞。我为了更可见性,擅自将你的结论加入到Stefan的答案中。 - VonC
我在哪里可以找到一些关于像'$@'和'@-1'这样的东西的文档 - 完全不知道它们是什么? - Mark Gibaud
@MarkGibaud 我使用了http://tldp.org/LDP/abs/html/来学习Bash脚本编程。在开始这个过程之前,我没有任何真正的Bash脚本编程经验。这个指南帮助了我,但你需要有耐心。 - mwotton

1
迟到了..它失败的原因是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"

所以,Stefan的答案 的另一种选择是使用注释技巧。
# [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\" \
#"

注意事项:

  • "!..."!"..." 是等价的
  • 注意以空格结尾的 "\ "(反斜杠加空格),它将无法正常工作!
  • 即使使用了 "all parameters" ($*),这个技巧仍然有效,因为它完全防止了附加操作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接