0

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?

2

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

Specifically:

git branch --no-color

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

  feature/XYZ-124
* master
  release/1.10
  release/1.11
  release/1.12

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

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

* master

Then:

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

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

 master

Example

>git_branch
 master
  • 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
1

Your

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.