0

I have this git command that I use a lot as an zsh function.

git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)

What I want to achive is create an alias and be able to call it as git upstream rather than calling just $ upstream as an zsh function. The closest I did get was this:

[alias]
    upstream = "!fn() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD) }; fn"

However, my guess is, it does fail at $(...) due to some parsing error. The error it shows me is this:

> git upstream
fn() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD) }; fn: -c: line 1: syntax error: unexpected end of file

Is what I am trying to do through aliases possible? If not, can you direct me to any kind of a source to create git upstream command?

ogirginc
  • 4,948
  • 3
  • 31
  • 45
  • 1
    create a script `git-upstream`, then when you call `git upstream` this will call `git-upstream` – Ôrel Jun 30 '20 at 17:40

2 Answers2

1

In your local bin directory for example ~/bin Create a git-upstream file:

#!/bin/sh
git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD)

Then make it executable

chmod u+x git-upstream

Now you can call git upstream

Ôrel
  • 7,044
  • 3
  • 27
  • 46
1

Turns out I was very close but just missed a semicolon! ‍♂️

[alias]
  upstream = "!f() { git push --set-upstream origin $(git rev-parse --abbrev-ref HEAD); }; f"
ogirginc
  • 4,948
  • 3
  • 31
  • 45
  • `zsh` allows `f () { cmd }`, but Git doesn't use your login shell; it uses the system default shell, which requires the last command in the body to be properly terminated. – chepner Jun 30 '20 at 17:51