Question

I'd like to create an alias that runs git pull origin <current branch name>

I can get the current branch name with git rev-parse --abbrev-ref HEAD.

But how do I put that 2nd command within the first in a bash alias?

I've tried

alias gup="git pull origin `git rev-parse --abbrev-ref HEAD`"

and

alias gup="git pull origin $(git rev-parse --abbrev-ref HEAD)"

but both result in the initial branch name in the alias, not the branch name at the time the command is run.

Was it helpful?

Solution

The immediate problem is that the command substations are expanded inside the double quotes when the alias is defined. Using single quotes will fix that by deferring the command substitution until you actually use the alias.

alias gup='git pull origin $(git rev-parse --abbrev-ref HEAD)'

However, you can save yourself the trouble of trying to get the quoting just right by using a shell function instead.

gup () {
    git pull origin $(git rev-parse --abbrev-ref HEAD)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top