Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git alias with variables not working as expected: pathspec did not match any file(s) known to git

I have created an alias for catching up my tracking branches as needed. Here's the current line from the [alias] section of my .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"

I use it as follows (for example):

git catchup new_design

This code results in (for example):

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.

I have tried the last command in the alias both with and without the double-quotes, with the same result.

Anyone know how to resolve that error at the end?

For those who might suggest using git pull, it doesn't resolve my problem, and would require entering my password. This alias is to be used if I have recently used git fetch, and have no need to go back to the remote repo.

I am running git bash on Windows 7, fyi.

like image 972
mwotton Avatar asked Oct 07 '22 10:10

mwotton


2 Answers

Use a shell function for the alias:

[alias]

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

There the handling of $n works as expected.


The OP mwotton confirms in the comments that the following works:

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

In multi-line, for more visibility:

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; }; _"
like image 106
Stefan Näwe Avatar answered Oct 10 '22 04:10

Stefan Näwe


Late for the party.. The reason it failed is that git runs the command as

git catchup new_design
# turns
CURRENTBRANCH= ... && git checkout "$CURRENTBRANCH\" new_design

That is, it appends "new_design" to the command. A proof of this is simple to demonstrate:

# [alias]
#   proof = !echo fixed text

> git proof tryme
# prints "fixed text tryme", not "fixed text"

So, an alternate option to stefan's answer is to use the comment trick

# [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\" \
#"

Notes:

  • "!..." and !"..." are equivalent
  • watch out for "\ " endings (with spaces after), it won't work!
  • this trick works even with use of "all parameters" ($*), since it prevents at all the append
like image 25
Bernardo Dal Corno Avatar answered Oct 10 '22 04:10

Bernardo Dal Corno