Desmoline Awa
Desmoline Awa

Reputation: 535

How to check for the presence of a git repository in a directory using bash script

I am trying to work with git on my project.

I want to set up the terminal such that whenever I cd into a directory that contains a git project, the terminal should indicate which git branch I am currently on.

If there is a git project, the terminal show only the name of the branch, for example

(master) $

otherwise it should show the current directory path.i.e

username@machinename:path/to/directory $

I found a similar question answered here but i don't know how to modify the answer to suit my need, because am not good with bash scripting. Any suggestion will be highly appreciated.

Upvotes: 3

Views: 1453

Answers (3)

martin.malek
martin.malek

Reputation: 2218

Have a look at this project https://github.com/jimeh/git-aware-prompt, it should solve your whole problem and when not, you can change it to meet your needs. Main logic is in prompt.sh.

To find current git branch name in directory, you can always run

git rev-parse --abbrev-ref HEAD

It will return branch name, HEAD (when detached) or nothing when directory is not par of git repository. If you need only this information, you can update your .bashrc. Edit variable PS1, which is bash prompt format.

PS1='(`git rev-parse --abbrev-ref HEAD 2> /dev/null`) \$ '

This is example how to display branch name anywhere in the prompt. The git script will help you and recognize whether to show branch or directory. It will update your PROMPT_COMMAND, which is called every time the bash prompt line is displayed, by checking for git branch name which you can then use in PS1 as a variable. You can then update your existing PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' to

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w \(\$git_branch\)\$ '

Upvotes: 3

Eugene Yarmash
Eugene Yarmash

Reputation: 149806

The Git package in Linux usually comes with a prompt script that you can use to see the repository status. To enable it, source the git-prompt.sh script in a shell startup file (e.g. ~/.bashrc), then set a custom prompt with the %s parameter. For example, in ArchLinux you could do:

[ -r /usr/share/git/completion/git-prompt.sh ] && . /usr/share/git/completion/git-prompt.sh
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

When changing to a directory of a Git repository, the prompt will change to show the branch name. Extra details can be set to be shown by the prompt (see the link above).

Upvotes: 1

CodeWizard
CodeWizard

Reputation: 142094

You have many tools which do it.

myzsh for example or just a simplt bash that you add to your bashrc

http://martinvalasek.com/blog/current-git-branch-name-in-command-prompt

example:

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

RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
GREEN="\[\033[0;32m\]"
NO_COLOR="\[\033[0m\]"

PS1="$GREEN\u@\h$NO_COLOR:\w$YELLOW\$(parse_git_branch)$NO_COLOR\$ "

Upvotes: 1

Related Questions