3

I have the following Git alias in my global git config.

pushnew = !f() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git push --set-upstream origin $tmp_branch; unset $tmp_branch; }; f

When running it I get the following output:

 * [new branch]      bug/graphs -> bug/graphs
Branch bug/graphs set up to track remote branch bug/graphs from origin.
f() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git push --set-upstream origin $tmp_branch; unset $tmp_branch; }; f: 1: unset: bug/graphs: bad variable name

I've seen this question but if that is the solution then how do I apply the fix within the context of a Git alias?

Community
  • 1
  • 1
bcmcfc
  • 23,143
  • 28
  • 104
  • 170
  • 2
    I guess it won't be too problematic in this context but you should always quote your shell variables, e.g. `git push --set-upstream origin "$tmp_branch"`. This prevents word splitting, when there are spaces inside the variable's value and glob expansion, when there are characters such as `*`. – Tom Fenech May 01 '15 at 09:55
  • 2
    If you want to find out the current branch name you do not want to be `grep`ping the output from `git branch`. You want to use `git symbolic-ref HEAD` (possibly with the `--short` argument as well). See https://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch . – Etan Reisner May 01 '15 at 10:22
  • Tom, Etan: thanks for those suggestions - will definitely improve it and help with future maintenance – bcmcfc May 01 '15 at 10:26
  • There's no real need to unset `tmp_branch`, since it is defined in the subshell created by Git to evaluate the alias. There's also no need to export it; you are only expanding it in the same shell where it is defined, not a child process. `f() { git push --set-upstream origin "$(git symbolic-ref HEAD)"; }` should suffice. – chepner May 01 '15 at 15:35

1 Answers1

4

It should be:

unset tmp_branch

If you try unset $tmp_branch, the shell will substitute the value of $tmp_branch, trying to unset bug/graphs.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283