Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if Git is installed from .bashrc

Tags:

git

bash

I'm using Git, I've changed the following line in .bashrc, To show the current checkedout branch in prompt, when pwd is a Git Repo. Operating System I'm using is: Ubuntu 32bit

# Original PS1 Line
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

I'm using this line to display current branch of git repo in shell prompt, instead of, the above line.

# PS1 Line to show current Git Branch in the Prompt
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '

The Problem is when I give it to friends, Shell gives error __git_ps1: command not found, while navigating between directories, as the script checks for git branch on changing directories. How do I check if Git is installed and perform the branch check only if git is installed?

Edit: As suggested by ayckoster, I cameup with the following lines of code:

if [ "$color_prompt" = yes ]; then
    git --version
    GIT_IS_AVAILABLE=$?
    if [ $GIT_IS_AVAILABLE -eq 0 ]; then
        # PS1 Line to show current Git Branch in the Prompt
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
    else
        # Original PS1 Line
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    fi
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

Now, Everytime I open the terminal I get the git --version outputted to screen, while Git is installed, and I get the following error, while opening terminal when Git is not installed:

The program 'git' is currently not installed.  You can install it by typing:
sudo apt-get install git

How do I clear this? Thanks.

Final Edit:

This is the code I came up with finally, Feel Free to use this code in your .bashrc to display current git branch in your shell prompt

if [ "$color_prompt" = yes ]; then
    if git --version &>/dev/null; then
        # PS1 Line to show current Git Branch in the Prompt
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\[\033[01;32m\]$(__git_ps1 " (%s)")\[\033[00m\]\$ '
    else
        # Original PS1 Line
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    fi
else
    if git --version &>/dev/null; then
        # PS1 Line to show current Git Branch in the Prompt
        PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w $(__git_ps1 "(%s)")\$ '
    else
        # Original PS1 Line
            PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    fi
fi
like image 769
Sathish Manohar Avatar asked Sep 03 '11 10:09

Sathish Manohar


People also ask

How do I find git in Linux?

Git is by default installed under /usr/bin/git directory on recent Linux systems.

How do I know if Github is installed on Linux?

Once you've opened your terminal application, type git version . The output will either tell you which version of Git is installed, or it will alert you that git is an unknown command. If it's an unknown command, read further and find out how to install Git.

Where is git installed?

The default path is “C:\Program Files\Git“. If you want the software installed in a different location click Browse and specify a different folder.

Is git Bash installed with git?

Git Bash comes included as part of the Git For Windows package. Download and install Git For Windows like other Windows applications. Once downloaded find the included .exe file and open to execute Git Bash.


2 Answers

#!/bin/bash
command -v git >/dev/null 2>&1 ||
{ echo >&2 "Git is not installed. Installing..";
  yum install git
}
like image 186
alex Avatar answered Sep 21 '22 23:09

alex


Try to execute

git --version

Depending on the return value $? you can assume git is installed or not. If you get 0 everything is fine otherwise git is not installed. You can also test this.

This assumes everything is setup correctly and git is in your $PATH and the git command is not renamed.

Use it like this

git --version 2>&1 >/dev/null # improvement by tripleee
GIT_IS_AVAILABLE=$?
# ...
if [ $GIT_IS_AVAILABLE -eq 0 ]; then #...
like image 43
ayckoster Avatar answered Sep 25 '22 23:09

ayckoster