Git alias for the current branch name

4

In my work it is very common to switch between feature branches and stage / develop branches several times during the day. And many of these times I need to write or use a tab to complete the name of the branch even though it is inside it. I wanted something that acted similarly to a this in programming: a reference to the current branch name.

Problem:

$ git checkout -b feature/nova-feature
// aqui eu trabalho na nova feature ate ficar ok
$ git push origin feature/nova-feature (*)
$ git checkout stage && git merge feature/nova-feature (*)
$ git push origin stage (*)
$ git checkout feature/outra-feature
// trabalho em mais alguma coisa
$ git push origin feature/outra-feature (*)
$ git checkout stage && git merge feature/outra-feature (*)
$ git push origin stage (*)

 etc

In 8 interactions with git I had to write 6 times the name of the branch I'm in (on 3 different branches).

What I already have today in my .gitconfig

[alias]
    mg = merge --no-ff --no-edit
    mc = commit -a --no-edit
    df = "!git diff --color | diff-so-fancy"
    ck = "!git checkout $1 && git pull origin $1"
    gr = "!git branch | !grep $1"
    msp = "!git ck $1 && git mg $2 && git push origin $1"

ck , gr , and msp are aliases that allow me to write the branch name only once when I am concatenating.

What I wanted:

$ git checkout -b feature/nova-feature
// aqui eu trabalho na nova feature ate ficar ok
$ git push origin this
$ git checkout stage && git merge this
$ git push origin this
$ git checkout feature/outra-feature
// trabalho em mais alguma coisa
$ git push origin this
$ git checkout stage && git merge this
$ git push origin this

So that I could create alias for these most common actions that work in any branch, without typing the name. Do you have a way?

    
asked by anonymous 01.11.2016 / 03:43

3 answers

4

Ricardo you can use the alias as follows:

[alias]

#Get your branch

branch-name="! git rev-parse --abbrev-ref HEAD"

#Publish your branch

publish="! git push -u origin $ (git branch-name)"

#Delete the remote version of your branch

unpublish="! git push origin: $ (git branch-name)"

That way you would use this:

$ git checkout -b feature/nova-feature
// aqui eu trabalho na nova feature ate ficar ok
$ git publish
$ git unpublish

Take a look at the aliases used in this link of @robmiller github user, which I believe you were able to implement a workflow of productive work.

    
01.11.2016 / 12:45
4

Found Solution

Following the steps of Diego Garcia's response, I came up with the following setting, which allows me never to write the current branch name:

[alias]
    branch-name = "!git rev-parse --abbrev-ref HEAD"
    mg = merge --no-ff --no-edit
    ck = "!git checkout $1 && git pull origin $1"
    cm = "!BRANCH=$(git branch-name); git ck $1 && git mg $BRANCH"

How to use concatenating aliases (for all other commands that need this same pattern, just use the alias format cm above)

$ git cm stage

Equivalent to:

$ git checkout stage && git pull origin stage && git merge branch-atual --no-ff --no-edit
    
02.11.2016 / 02:25
2

You can control this through the push.default setting.

The option you want is current.

git config --global push.default current

So you can only use git push and you'll get this behavior.

For other possible options, see the git-config man page ( or click here )

    
01.11.2016 / 13:36