I'm trying to parse the branch I'm currently checked out into to my bash prompt. This is quite easy using the following bash function in your PS1:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
export PS1="\[$(tput setaf 3)\]\u\[$(tput setaf 2)\]@\[$(tput setaf 4)\]\h \[$(tput setaf 5)\]\w\[$(tput setaf 7)\]\[$(tput setaf 3)\]\$(parse_git_branch) \[$(tput setaf 7)\]\\$ \[$(tput sgr0)\]"
My problem arises when the prompt still shows a 'checked out' branch from within a folder inside the .gitignore
file.
For instance:
Let's suppose /home/rbroggi/workspace/project_one
is the root of my repository tracked by git ( e.g.: the place where the .git
folder is to be found). And within this repository I have a .gitignore file /home/rbroggi/workspace/project_one/.gitignore
with the following record:
ignoredfolder/
. I would assume an empty output/result from git branch
once changing directory into the ignoredfolder
but instead it keeps outputting the branch I'm checked out in the parent folder. This is very annoying and maybe if I had visibility over how the command git branch
works I could avoid getting the output for ignored folders.
rbroggi@arch ~/workspace/project_one (master) $ pwd
/home/rbroggi/workspace/project_one
rbroggi@arch ~/workspace/project_one (master) $ cat .gitignore
ignoredfolder/
rbroggi@arch ~/workspace/project_one (master) $ cd ignoredfolder/
rbroggi@arch ~/workspace/project_one/ignoredfolder (master) $ git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
(master)
rbroggi@arch ~/workspace/project_one/ignoredfolder (master) $
Thank you for taking the time to trying and helping me.
Rodrigo Broggi