5

我创建了一个别名来根据需要赶上我的跟踪分支。[alias]这是我的 .gitconfig 部分的当前行:

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,仅供参考。

4

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; }; _"
于 2012-10-08T10:43:39.397 回答
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\" \
#"

笔记:

  • "!..."并且!"..."是等价的
  • 注意“\”结尾(后面有空格),它不起作用!
  • 即使使用“所有参数”($*),这个技巧也有效,因为它完全防止了追加
于 2019-11-14T06:21:10.007 回答