Git alias on current branch
Asked Answered
C

5

42

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.

Cambridge answered 27/2, 2013 at 12:35 Comment(2)
git rev-parse --abbrev-ref HEAD is what i'd use to derrive the current branch.Drupelet
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 ? :-)Donnetta
M
8

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.

Mattison answered 27/2, 2013 at 12:40 Comment(4)
-1: I don't think that this is correct. The default action of git push depends on the push.default setting in .gitconfig.Albeit
Yes, but it's only the case if it is set to matchingMattison
Actually matching is the current default value for push.default, so I've changed my answerMattison
well there is also a current value: git config --global push.default current that's how I handled it (git 1.8 for me)Cambridge
A
87
[alias]
  po = "!git push --set-upstream origin \"$(git rev-parse --abbrev-ref HEAD)\""
Algarroba answered 13/9, 2013 at 9:6 Comment(7)
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.Chamblee
Why the bang in !git? I know what bang-anything does in bash, just not sure why you'd want it here.Tieshatieup
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.Striation
Great. Also, at 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)Albumen
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.Irwinirwinn
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)\"".Snaggy
This is great. Thanks. Typing git push origin -u BranchName each time is too much!Fleece
A
16

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)"'
Albeit answered 27/2, 2013 at 12:44 Comment(2)
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.Fortson
git config --global alias.po "push origin '$(git symbolic-ref --short HEAD)'" is what worked out for meLimemann
M
8

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.

Mattison answered 27/2, 2013 at 12:40 Comment(4)
-1: I don't think that this is correct. The default action of git push depends on the push.default setting in .gitconfig.Albeit
Yes, but it's only the case if it is set to matchingMattison
Actually matching is the current default value for push.default, so I've changed my answerMattison
well there is also a current value: git config --global push.default current that's how I handled it (git 1.8 for me)Cambridge
I
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
Impeachable answered 27/2, 2013 at 12:42 Comment(3)
Use git symbolic-ref HEAD to get the current branch. Should be faster.Albeit
@lunaryorn why not git rev-parse --abbrev-ref HEAD? it returns branch name without refsOrate
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.Hardness
R
0

For me it works just simply:

git config --global alias.po "push origin HEAD"

Don't understand the complex awnsers but they work as well.

--global tag is for a all around configuration

Ricercare answered 19/7 at 1:7 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Koski

© 2022 - 2024 — McMap. All rights reserved.