Skip to content

Instantly share code, notes, and snippets.

@RicardoRagel
Last active August 23, 2022 08:40
Show Gist options
  • Save RicardoRagel/f57d294222c3f6720f2fc86758087d63 to your computer and use it in GitHub Desktop.
Save RicardoRagel/f57d294222c3f6720f2fc86758087d63 to your computer and use it in GitHub Desktop.
Personal bashrc script with some git helpers for projects with too many repositories and branches
#!/bin/bash
## [PROMPT] Add git branch, git status and system time to the prompt
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
parse_git_status() {
st=$(git status 2>/dev/null | tail -n 1)
if [[ $st == "nothing to commit, working tree clean" ]]
then
echo "[clean]"
fi
}
red_txt='\[\033[31m\]'
green_txt='\[\033[32m\]'
blue_txt='\[\033[34m\]'
normal_txt='\[\033[00m\]'
time_tag="${blue_txt}[\A]${normal_txt}"
ORIGINAL_PS1=${PS1}
PS1="${time_tag}"
PS1+="${red_txt}"
PS1+='$(parse_git_branch)'
PS1+="${green_txt}"
PS1+='$(parse_git_status)'
PS1+="${normal_txt}"
PS1+=${ORIGINAL_PS1}
# [GIT COMMANDS] Check the git branch and status of every repository in the current directory using
# the command "git-status"
NOCOLOR='\033[0m'
COLOR='\033[1;34m'
alias git-status="find . -maxdepth 1 -mindepth 1 -type d -exec sh -c '(echo \"${COLOR}{}${NOCOLOR}\" && cd {} && echo -n \"branch: \" && git branch --show-current && git status -s && echo)' \;"
# [PROMPT COMMAND] Trim the bash promt to the last directory
alias trim_dir_path="PROMPT_DIRTRIM=1"

Setup

Copy this file next to your .bashrc and add the following line at the end of this:

source ~/.bashrc.personal

Usage

  1. Re-open your terminal and check the new bash prompt when you access to a repository, it should show the branch name and the tag [clean] if there is no changes.

  2. From the parent directory that contains multiple repositories, simply execute the following command to check the status of all of them:

  git-status
  1. In case you are in a location with a large directory path, execute the following command to trim this path to just the las directory name:
  trim_dir_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment