Quantcast
Channel: How do I get the current branch name in Git? - Stack Overflow
Viewing all articles
Browse latest Browse all 96

Answer by okTalk for How to get the current branch name in Git?

$
0
0

I know this has been answered already, but in the most recent version of Git the command git branch opens a list of your branches in some kind of prompt that you have to quit out of. Which annoys me to no end!

Here's my fix: Open your bash profile and type this in:

#!/bin/bash
git() {
  if [[ $@ == "branch" ]] then
    command git branch -a | grep -v 'remotes'
  else
    command git "$@"
  fi
}

Now open Terminal and test it out by typing the following commands in a git repo:

source ~/.zshrc
git branch 

And voila! A list of your local branches is printed out in your terminal.

The code you're writing to your bashrc file overwrites the default function for git branch and replaces it with a much longer command that lists all local branches via the -a argument. Then we grep out the extra not needed business and print it out. If you exclude the grep command you'll still get the annoying prompt. If you're not familiar with writing bash commands checkout this explanation: About .bash_profile, .bashrc, and where should alias be written in?


Viewing all articles
Browse latest Browse all 96

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>