1

I am attempting to write a new alias command that performs the following:

git push origin [current branch]

I have the following to return the current branch

git rev-parse --abbrev-ref HEAD

I cannot work out:

a) how to add two commands to one line and not have them run procedurally. E.g return the current branch but then also run it on one line. I have tried the following alias:

alias "gps cb"="git push origin \"git rev-parse --abbrev-ref HEAD\""

b) how to have an alias with a space in it e.g gps cb, I have a separate for just gps so I'd rather keep the pattern of having a space afterwards in the alias so it's not too much mental legwork to write gps master and gps cb

2
  • 1
    Why do you need the alias at all? git push defaults to both current branch and origin if you don't specify, meaning that git push should already do what you're asking for, or am I missing something? Commented Aug 25, 2017 at 13:29
  • 3
    You may want to use a git alias instead of a shell alias, which would allow you to write something like git pcb. Untested, but I think you would add alias.pcb = '!git push origin $(git rev-parse --abbrev-ref HEAD)' to your ~/.git/config file. Commented Aug 25, 2017 at 13:37

4 Answers 4

3

Define as:

push-branch() {
    git push origin "$(git rev-parse --abbrev-ref HEAD)"
}

Use as:

$ push-branch

Note that aliases cannot have spaces in them. You need a function for that. You can replace your current gps alias with a function that takes arguments:

gps() {
    case "$1" in)
    cb) push-branch;;
    *) # whatever your current gps does ;;
    esac
}
Sign up to request clarification or add additional context in comments.

Comments

2

You don't need an alias definition at all, which could be solved by a simple function call.

git-push-origin() {
    git push origin "$(git rev-parse --abbrev-ref HEAD)"
}

and just call the function git-push-origin from the command line. Also you could add the function definition to .bashrc to load them at startup.

Comments

1

git has it's own alias system, so you could do

git config --global alias.pushCurrent '!exec git push origin $(git rev-parse --abbrev-ref HEAD)'

which adds the git command

git pushCurrent

but just git push should push the head of the current branch to origin, so you probably have some special case to justify the extra alias, and you can tweak this to fit your needs

Comments

0

You can use substitution $() to use the output of one command as an argument for another command.

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

Spaces in aliases or function names are not possible, but even if they were allowed, I don't think you would want to use them, because you had to escape the space in order to call them ("gps cb" or gps\ cb).

If you already have defined your own gps script and want it to behave differently depending on the first argument, you have to use an if or case statement to check the first argument.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.