Git alias for the current branch name

Asked

Viewed 297 times

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 branch name even though I’m inside it. I wanted something that would act in a similar way 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 the git i had to write 6 times the branch name I’m on (in 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 only the branch name once when I concatenate.

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 on any branch, without having to write the name. There is a way?

  • 1

    see if this helps you https://www.reddit.com/r/git/comments/1v16qc/how_do_i_reference_the_current_branch_in_a_git/

  • This can help your flow a bit: if you want to go back to the previous branch, you can use git checkout -

  • Perfect, I’ll delete my comments from here so don’t leave mess in your post.

3 answers

4

Solution Found

following the steps of Diego Garcia’s reply, I arrived at the following configuration, which allows me to never need to write down the name of the current branch:

[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"

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

$ git cm stage

Amounts to:

$ git checkout stage && git pull origin stage && git merge branch-atual --no-ff --no-edit
  • 1

    Ficou show Ricardo :)

4


Ricardo you can use aliases as follows:

[alias]

#Takes your branch

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

#Publishes its 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’d wear it like 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 from @robmiller github user, who I believe you will be able to implement a very productive workflow.

  • opa, did not solve 100% no but I think it took me to the right place, I will see here and update

  • Tranquil Ricardo, I believe that in this way you can reduce the typing and change your flow to become more practical :)

  • 1

    I added the format I made it work, it got battered. Thanks!

2

You can control this by setting push.default.

The option you want is Current.

git config --global push.default current

Then you can use only git push and you’ll get this behavior.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.