Thumbnail: jekyll

bashrc

by on
11 minute read

I have heavily modified my .bashrc configuration. This article shows how you can make better use of Git and the command line.

.bashrc file

If you are using GitBash, Cmder, ConEmu, VSCode or some other GUI client the .bashrc file is important to understand. The purpose of this file is to enable you to create aliases (naming shortcuts) and functions in Git.

The .bashrc file is located in ~/.bashrc. Make sure to backup this file and the ~/.gitconfig file before making any manual changes. Note that the PowerShell alias for ~/ on Windows 10 is the c:\Users\<your-user-name>.

As an example, instead of typing git status -s I have defined an alias that allows me to type gst to save some typing. The functions I describe below will save even more time and typing.

Another trick that I’ve added into .bashrc is color coding of the prompt to show the current checkout status and branch of the Git repo.

See the image below:

bashrc-1

The first prompt shows an example where I am not currently in a Git repo and the prompt only shows the text git /c/srce/github.

Once I have changed directories into the Pester repo (a nice PowerShell Unit Testing library), you can see that the prompt has changed to git (master) smiley /c/src/github/Pester. The green color for the (master) text and smiley signifies that nothing has been modified in this repo. The (master) text means that I am currently working on the master branch.

After modifying README.md in the vi editor, you can see that the prompt’s color changes to red around the master branch and the smiley icon changes to a check mark (meaning something is checked out).

One note of warning. This prompt change does add some overhead to each command that you do, due to the git status required to color code the branch. If you have a slow connection or a small amount of memory on your computer, you may want to consider removing this feature. To remove it, delete the PS1 export statement from the script below.

One note of warning. This prompt change does add some overhead to each command that you do, due to the “git status” required to color code the branch. If you have a slow connection or a small amount of memory on your computer, you may want to consider removing this feature. To remove it, delete the PS1 export statement from the script below.

I’ve added my .bashrc below (note that you’ll need to change the Global variables in the top of the file). These aliases coupled with the changes to the ~/.gitconfig described later saves a lot of typing and allows you to use Git with windows more naturally,

###############################################################################
# Welcome Screen
clear
echo " ___.                 .__     "
echo " \\_ |__ _____    _____|  |__  "
echo "  | __ \\\\__  \\  /  ___/  |  \\ "
echo "  | \_\ \\/ __ \\_\\___ \\|   Y  \\"
echo "  |___  (____  /____  >___|  /"
echo "      \\/     \\/     \\/     \\/ "

###############################################################################
# Global Variables
DEFAULT_FORK="REPLACE_FORK"
DEFAULT_GROUP="REPLACE_GROUP"
DEFAULT_URL="git@bitbucket.org" # or "git@github.yourcompany.com"


###############################################################################
# Functions
function cloneIt {
	local project=$1
	local fork_name=${2:-$DEFAULT_FORK}
	local group_name=${3:-$DEFAULT_GROUP}
	local git_url=${4:-$DEFAULT_URL}
	 
	echo "Cloning ${git_url}:${fork_name}/${project}.git"
	git clone ${git_url}:${fork_name}/${project}.git
	#-b develop
	cd $project
	
	echo "Adding remote upstream: ${git_url}:${group_name}/${project}.git"
	git remote add upstream ${git_url}:${group_name}/${project}.git
	git remote set-url --push upstream do-not-push
	
	git fetch --all
	git checkout -b master upstream/master
	git checkout develop
	git branch develop -u upstream/develop
	git branch -vv
}

function current_branch() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || \
  ref=$(git rev-parse --short HEAD 2> /dev/null) || return
  echo ${ref#refs/heads/}
}

function current_repository() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || \
  ref=$(git rev-parse --short HEAD 2> /dev/null) || return
  echo $(git remote -v | head -n1 | awk '{print $2}' | sed 's/\.git//' | sed 's/^.\+\://')
}

# Reset
Color_Off="\[\033[0m\]"       # Text Reset

# Regular Colors
Black="\[\033[0;30m\]"        # Black
Red="\[\033[0;31m\]"          # Red
Green="\[\033[0;32m\]"        # Green
Yellow="\[\033[0;33m\]"       # Yellow
Blue="\[\033[0;34m\]"         # Blue
Purple="\[\033[0;35m\]"       # Purple
Cyan="\[\033[0;36m\]"         # Cyan
White="\[\033[0;37m\]"        # White

# Bold
BBlack="\[\033[1;30m\]"       # Black
BRed="\[\033[1;31m\]"         # Red
BGreen="\[\033[1;32m\]"       # Green
BYellow="\[\033[1;33m\]"      # Yellow
BBlue="\[\033[1;34m\]"        # Blue
BPurple="\[\033[1;35m\]"      # Purple
BCyan="\[\033[1;36m\]"        # Cyan
BWhite="\[\033[1;37m\]"       # White

# Underline
UBlack="\[\033[4;30m\]"       # Black
URed="\[\033[4;31m\]"         # Red
UGreen="\[\033[4;32m\]"       # Green
UYellow="\[\033[4;33m\]"      # Yellow
UBlue="\[\033[4;34m\]"        # Blue
UPurple="\[\033[4;35m\]"      # Purple
UCyan="\[\033[4;36m\]"        # Cyan
UWhite="\[\033[4;37m\]"       # White

# Background
On_Black="\[\033[40m\]"       # Black
On_Red="\[\033[41m\]"         # Red
On_Green="\[\033[42m\]"       # Green
On_Yellow="\[\033[43m\]"      # Yellow
On_Blue="\[\033[44m\]"        # Blue
On_Purple="\[\033[45m\]"      # Purple
On_Cyan="\[\033[46m\]"        # Cyan
On_White="\[\033[47m\]"       # White

# High Intensty
IBlack="\[\033[0;90m\]"       # Black
IRed="\[\033[0;91m\]"         # Red
IGreen="\[\033[0;92m\]"       # Green
IYellow="\[\033[0;93m\]"      # Yellow
IBlue="\[\033[0;94m\]"        # Blue
IPurple="\[\033[0;95m\]"      # Purple
ICyan="\[\033[0;96m\]"        # Cyan
IWhite="\[\033[0;97m\]"       # White

# Bold High Intensty
BIBlack="\[\033[1;90m\]"      # Black
BIRed="\[\033[1;91m\]"        # Red
BIGreen="\[\033[1;92m\]"      # Green
BIYellow="\[\033[1;93m\]"     # Yellow
BIBlue="\[\033[1;94m\]"       # Blue
BIPurple="\[\033[1;95m\]"     # Purple
BICyan="\[\033[1;96m\]"       # Cyan
BIWhite="\[\033[1;97m\]"      # White

# High Intensty backgrounds
On_IBlack="\[\033[0;100m\]"   # Black
On_IRed="\[\033[0;101m\]"     # Red
On_IGreen="\[\033[0;102m\]"   # Green
On_IYellow="\[\033[0;103m\]"  # Yellow
On_IBlue="\[\033[0;104m\]"    # Blue
On_IPurple="\[\033[10;95m\]"  # Purple
On_ICyan="\[\033[0;106m\]"    # Cyan
On_IWhite="\[\033[0;107m\]"   # White

# Various variables you might want for your PS1 prompt instead
Time12h="\T"
Time12a="\@"
PathShort="\w"
PathFull="\W"
NewLine="\n"
Jobs="\j"

###############################################################################
# Windows Tools and Programs
function vs10 { /c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio\ 10.0/Common7/IDE/devenv.exe `ls *.sln | sort -n | head -1` & disown; }
function vs13 { /c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio\ 12.0/Common7/IDE/devenv.exe `ls *.sln | sort -n | head -1` & disown; }
function vs15 { /c/Program\ Files\ \(x86\)/Microsoft\ Visual\ Studio\ 14.0/Common7/IDE/devenv.exe `ls *.sln | sort -n | head -1` & disown; }
alias vs=vs15
alias n="/c/Program\ Files\ \(x86\)/Notepad\+\+/notepad\+\+.exe"
alias c="/c/Program\ Files\ \(x86\)/Microsoft\ VS\ Code/Code.exe"
alias vsc=c
alias bashrc="n ~/.bashrc"
alias gitconfig="n ~/.gitconfig"
alias e="explorer ."

###############################################################################
# Git Alias
alias gb="git branch"
alias gst="git st"
alias gdt="git dt"
alias gdts="git dts"
alias gdtc="git difftool HEAD^ HEAD"
alias gdprv="git difftool HEAD^ HEAD"
alias gdprev="git difftool HEAD^ HEAD"
alias gl="git l"
alias gaa="git aa"
alias gbr="git branch"
alias grv="git remote -v"
alias gpush='git push -u origin $(current_branch)'
alias gpull='git pull upstream $(current_branch)'
alias gpullu='git pull upstream $(current_branch)'
alias gpullo='git pull origin $(current_branch)'
alias gco="git checkout"
alias gc="git commit -m"
alias gcm="git commit -m"
alias gcam="git commit -am"
alias ggui="git gui citool &"
alias gclone=cloneIt
alias gstl="git stash list"
alias l="ls -a -l"
alias sl="ls -a -l"
alias bb='"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://bitbucket.org/$(current_repository)'
alias gk='gitkraken --path . &'
alias kl='kitchen list'
alias kcr='kitchen create'
alias kco='kitchen converge'
alias kcon='kitchen converge'
alias kcv='kitchen converge && kitchen verify'
alias kd='kitchen destroy'
alias kde='kitchen destroy'
alias kdest='kitchen destroy'
alias crspec='chef exec rspec spec/unit/recipes/default_spec.rb -c'
alias unit='chef exec rspec spec/unit/recipes/default_spec.rb -c'

###############################################################################
# Export
set EDITOR=notepad