git

Easily get current branch name in git

kakty3

kakty3

Posted on July 6, 2017

Easily get current branch name in git

Sometimes I miss the features from Mercurial which Git does not have. One of them is hg outgoing which "Show changesets not found in the specified destination repository or the default push location". Similar behavior could be achieved with git log branchA..branchB, e.g to see outgoing commits: git log origin/master..feature-branch.

One day I used that command for the branch with really long name. I just typed a few fisrt letters, pressed tab and branch name was auto-completed (I personally prefer fish shell, but it should also work for all other modern shells). I asked myself: "What if I am using bash and I don't have the auto-completion, but I also don't want to use git branch and copy-and-paste branch name?".

The answer is to use git rev-parse --abbrev-ref HEAD:

> git rev-parse --abbrev-ref HEAD
feature-branch
Enter fullscreen mode Exit fullscreen mode

I've created the alias for it:

git config --global alias.cb 'rev-parse --abbrev-ref HEAD'

Enter fullscreen mode Exit fullscreen mode

where cb stands for current branch.

Now I can do following

git log origin/master..$(git cb)
Enter fullscreen mode Exit fullscreen mode

Suddenly I found out that passing remote-branch is not needed and previous command could be simplified:

git log origin/master..
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
kakty3
kakty3

Posted on July 6, 2017

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related