Yeah, it may not be intuitive at first... but is really useful to be able to rely on both behaviors (evaluate on definition or when run).
If you wanted to use other aliases within an alias you have to start playing around with eval (which I don't really recommend). Like:
alias testingEval="eval 'git-branch'"
But things get messy pretty quickly... I pretty much use alias for super simple things or functions when I need something a bit longer that could use some extra variables, or to use parameters, or runtime expansion.
Ok, after some digging I got an alias to work. I have no idea why it works when nothing else I tried would but here it is anyway. I think it is best to paste the following into your .git-config file directly:
up = "!git push -u origin `git symbolic-ref --short HEAD`"
or if you have git 2.22
up = "!git push -u origin `git branch --show-current`"
Assuming that you have set your
git-branch
alias already, you could simplify this as:alias git-up="git push -u origin \$(git-branch)"
EDIT: escaped $ to prevent running the alias on definition, thanks @darlanalves
the problem is that the
git-branch
got evaluated when I started my terminal. This is inzsh
. :/I'd immediately get "not a git repository" message.
maybe a function would work?
Oh, you're right... an alias would be immediately evaluated.
Using a function is the way to go.
it kind of tripped me up! I was expecting it to evaluate when invoked, just like a function.
Yeah, it may not be intuitive at first... but is really useful to be able to rely on both behaviors (evaluate on definition or when run).
If you wanted to use other aliases within an alias you have to start playing around with
eval
(which I don't really recommend). Like:alias testingEval="eval 'git-branch'"
But things get messy pretty quickly... I pretty much use alias for super simple things or functions when I need something a bit longer that could use some extra variables, or to use parameters, or runtime expansion.
btw, I use zsh too :) ... I use prezto and got some configs online if you want to take a look
github.com/ivanalejandro0/prezto/b...
I get why it works like that and I'm ok with it :) It really just surprised me but then again, I don't do a lot of bash scripting.
The key to it is just escape the
$
char in an alias.alias git-up="git push -u origin \$(git-branch)"
Oh, cool. I didn't know about that, thanks.
I've edited my comment escaping the $.
Git assumes the current branch implicitely, so the following also works:
Even better is to use a git alias for this:
so you can use it like
$ git up
.If you think you need to explicitly specify the branch anyway you can define an alias like this:
Nice! Thank you for the tips!
The
git up
alias is perfect.Not specifying the branch still gives me a
Fatal: The current branch has no upstream
. Any ideas why?Did you specify the
-u
flag?Yep, I copied and pasted this exactly
git config --global alias.up 'push -u origin'
yeah, looks like I have the same issue!
Ok, after some digging I got an alias to work. I have no idea why it works when nothing else I tried would but here it is anyway. I think it is best to paste the following into your .git-config file directly:
or if you have git 2.22
I figured this out mostly by messing around with this Stack Overflow answer: stackoverflow.com/a/30529511/9770212