I'm trying to understand what does this code means:

function git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'

but I don't get it. Someone said this code is made to make a configuration to your terminal, I don't really understand this configuration.

Could someone explain me?


This function will return a name of your current git branch.


git branch --no-color

will return the list of branches in your repository, like that:

* master

sed -e '/^[^*]/d'

Will remove any lines, except for those starting with "*" (which is a current branch)

* master


's/* (.*)/ \1/'

will extract the branch name (excluding '*' char)



  • Repository = my computer current directory, in this case? I'm not using GitHub (I don't even have an account in there), and I'm really bad at those things xD (git is relevant to github?) – xHDGML Nov 23 '16 at 20:32
  • Yep, your local Git repository (which is stored in your local folder). Git is just a version control tool, which can be used in many environments, and GitHub is a Git repository service (by far the most popular, but not the only one). – zeppelin Nov 23 '16 at 20:35
  • A different sed command could have been sed -ne 's/^* //p', which says don't print anything by default. Change lines that start "* "` to nothing and print the result. – icarus Nov 23 '16 at 20:43


function git_branch {
    git branch --no-color 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ \1/'

is the sort of text processing hack people write when they don't know things like:

git rev-parse --abbrev-ref HEAD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.