40

I'd like to improve my current aliases, most of them work over a branch. Is there a way to refer to the current branch in a git alias so I don't need to pass it each time?

like this one:

git config alias.po "push origin"

is use it like that:

git po foo_branch

I'd like to just run git po and it to take the current branch as a parameter.

plus-
  • 45,453
  • 15
  • 60
  • 73
  • 2
    git rev-parse --abbrev-ref HEAD is what i'd use to derrive the current branch. – Learath2 Feb 27 '13 at 12:46
  • 1
    Wouldn't a simple (in a alias) `git push origin HEAD` be enough? I don't get the point in all this rev-parsing... what am I missing guys ? :-) – Romain Valeri Oct 11 '18 at 15:59

4 Answers4

86
[alias]
  po = "!git push --set-upstream origin \"$(git rev-parse --abbrev-ref HEAD)\""
jmgarnier
  • 2,558
  • 2
  • 17
  • 8
  • Great answer! The only caveat is that I did this directly from the command line and it hard coded my current branch into the command line :) Direclty did it in the config file and it worked just fine. – CubanX Feb 08 '14 at 15:30
  • 2
    Why the bang in `!git`? I know what bang-anything does in bash, just not sure why you'd want it here. – Jared Beck May 22 '14 at 20:14
  • 3
    The bang allows you to execute everything that comes after it in the shell, i.e. that allows you to execute any command and not just git commands in the alias. – Patricia Garcia Mar 04 '15 at 17:37
  • 2
    Great. Also, at https://gist.github.com/robmiller/6018582 you can find a pretty collection of aliases (including one that is suitable as an answer to OP's question) – Frederik Struck-Schøning Jun 20 '16 at 10:20
  • For any windows folks wanting to do this from CMD: powershell -c "$s = git rev-parse --abbrev-ref HEAD; git push --set-upstream origin $s". Remove the beginning to execute directly from PS. Also you can do doskey po=powershell -c "$s = git rev-parse --abbrev-ref HEAD; git push --set-upstream origin $s" to get the shortcut globally. – Alex Vallejo Jun 26 '18 at 23:29
  • Thanks. This should be the accepted answer. I now have this working: `lg = "!git log --pretty='format:%C(yellow)commit %H%n%C(white)Author: %an <%ae>%nDate: %ad%n%n%B%n' --color=always develop..\"$(git rev-parse --abbrev-ref HEAD)\""`. – Mohamed Bana Nov 12 '18 at 10:15
  • This is great. Thanks. Typing `git push origin -u BranchName` each time is too much! – LeOn - Han Li Oct 08 '19 at 20:24
15

git symbolic-ref --short HEAD prints the current branch, so you can define a simple shell alias:

alias gpo='git push origin "$(git symbolic-ref --short HEAD)"'
  • Seems unnecessary to print error messages to standard error only to discard them by redirecting to /dev/null in the end. I guess this is a copy-paste scenario though. – Victor Zamanian Jun 18 '18 at 14:17
  • `git config --global alias.po "push origin '$(git symbolic-ref --short HEAD)'"` is what worked out for me – Ramon Dias Sep 23 '22 at 17:17
7

This answer will be valid starting from Git 2.0, where the default push behaviour will be simple

Unless push.default setting is set to matching, git push without specifying argument will always push the current branch, so in this case you don't need to specify it.

CharlesB
  • 86,532
  • 28
  • 194
  • 218
  • 3
    -1: I don't think that this is correct. The default action of `git push` depends on the `push.default` setting in `.gitconfig`. –  Feb 27 '13 at 12:43
  • Yes, but it's only the case if it is set to `matching` – CharlesB Feb 27 '13 at 12:57
  • Actually `matching` is the current default value for `push.default`, so I've changed my answer – CharlesB Feb 27 '13 at 13:00
  • 4
    well there is also a `current` value: `git config --global push.default current` that's how I handled it (git 1.8 for me) – plus- Sep 13 '13 at 13:23
0

It's not 100% clear from your question which of these two aliases you require.

This will push the currently checked out branch:

git config alias.po !f() { export tmp_branch=`git branch | grep '* ' | tr -d '* '` && git push origin $tmp_branch; unset $tmp_branch; }; f

This will push a given branch name (git po branchName):

git config alias.po !f() { git push origin $1; }; f
bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • 1
    Use `git symbolic-ref HEAD` to get the current branch. Should be faster. –  Feb 27 '13 at 12:45
  • 1
    @lunaryorn why not `git rev-parse --abbrev-ref HEAD`? it returns branch name without refs – vladkras Jul 06 '16 at 09:21
  • Thanks for posting the function alias, that was useful to me when creating an alias that required me to save the branch name in a variable. – James Ehly May 27 '21 at 20:06