Monday, April 26, 2021

Configure your bash profile for git

To change the bash prompt on your AWS EC2 instance from the default of 


[ec2-user@ip-10-1-1-1 ~]$
 

to
 

ec2-user@ec2-name /data/cool-app (development=)
└─►

 

read and apply the following bash_profile update:

Note, By default, AWS EC2 does not set /etc/hosts, so use a fixed string in your bash prompt,
or follow the AWS documentation:
https://aws.amazon.com/premiumsupport/knowledge-center/linux-static-hostname/


Create a temp file with the contents:
> echo '

# change me vars
ec2_name="ec2namechangeme"

# use vim
export EDITOR=vim
alias vi="vim"

# allow tab auto complete w/ sudo
if [[ "$HOME" == "/home/ec2-user" ]]; then
  complete -cf sudo
fi

# git status
git_status="n"
# existing git file in most distros
git_prompt=/usr/share/git-core/contrib/completion/git-prompt.sh
if [[ -f $git_prompt ]]; then
    source $git_prompt
    export GIT_PS1_SHOWDIRTYSTATE=true      # + staged, * unstaged
    export GIT_PS1_SHOWUNTRACKEDFILES=true  # % untracked files
    export GIT_PS1_SHOWUPSTREAM="auto"      # < behind, > ahead, <> diverged, = no difference
    export GIT_PS1_SHOWSTASHSTATE=true      # $ something is stashed
    git_status="y"
fi
unset git_prompt

# export PS1="[\u@\h \W]\$" # default
PS1="\u@${ec2_name} "
PS1+="\[\033[01;34m\]\${PWD#\$HOME/}\[\033[00m\]"
if [ $git_status = "y" ]; then
    PS1+=" \[\033[0;32m\]\$(__git_ps1 '\''(%s)'\'')\[\033[00m\]"
fi
PS1+="\n\[\033[0;32m\]└─►\[\033[00m\] "
export PS1

PATH=$PATH:$HOME/bin:/usr/bin:/usr/local/bin

export PATH
unset USERNAME

alias git-log="git log --pretty=oneline"
alias git-preview-files="git fetch; git diff --name-only HEAD..@{u}"
alias git-preview-diff="git fetch; git diff HEAD..@{u}"

' > temp_bash_profile.sh


Note, '\'' = '

Update ec2_name="ec2name" to the name of your ec2 ie super-cool-service
> vim temp_bash_profile.sh

Append to existing bash profile
# using sed
> sed -i '/# User specific environment and startup programs/ r temp_bash_profile.sh' ~/.bash_profile

# or edit and add to the end
> vim ~/.bash_profile


Test and see results
> source ~/.bash_profile


ec2-user@ec2-name /data/cool-app (development=)
└─►

Source: GitHub gist
 
-End of Document-
Thanks for reading