Never Mix-up Your Git Branches Again!
Ever finished working on that shiny new feature or making that little code update only to commit your code and discover you did so in the…
Ever finished working on that shiny new feature or making that little code update only to commit your code and discover you did so in the wrong git branch?
Whether you are an experienced developer or a newbie, you must’ve had times like this. And if you haven’t, you will; soon enough. Well, not if you do what I’m about to show you below or decide to go with a GUI based version control system (personally, I dislike them ).
The image above is a screenshot of how my terminal looks when I’m working on any git-based project. Take note of the “master” indicating the git branch i’m on. Below i show you how to achieve this.
- Edit bash profile
Type in the following in the terminal and hit Enternano ~/.bash_profile
This opens up your bash profile using nano. nano is a default terminal text editor that comes with most Linux based operating systems and mac OSX.
Paste the following at the bottom of the file.# Show Git branch in prompt.show_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ [\1]/' }export PS1="\u@\h \W\[\033[00;34m\]\$(show_git_branch)\[\033[00m\] $ "
Type Ctrl O to save the file
and Ctrl x to close the file.
- Refresh terminal
You can easily achieve this by sourcing the bash profile as shown below or restart your terminal.source ~/.bash_profile
You should immediately see the change if you are in a git-based project repository. Otherwise, open a new tab and navigate to a git-based project folder to see the changes.
- Changing color of branch name
If you are like me, you may want to customize the color of the displayed branch text. That is what the [\033[00;34m\] parts of the script above does.
The numbers before ;
determines the appearance; 00
means normal,01
means bold text and04
means to underline text. Other parts such as the34m
determines the color, which is blue in the above case.
Below is a list of terminal colors to choose from.Here are some other terminal colour codes you can use:Regular colours:
\[\033[00;30m\] — Black
\[\033[00;31m\] — Red
\[\033[00;32m\] — Green
\[\033[00;33m\] — Yellow
\[\033[00;34m\] — Blue
\[\033[00;35m\] — Purple
\[\033[00;36m\] — Cyan
\[\033[00;37m\] — WhiteHigh intensity:
\[\033[00;90m\] — Black
\[\033[00;91m\] — Red
\[\033[00;92m\] — Green
\[\033[00;93m\] — Yellow
\[\033[00;94m\] — Blue
\[\033[00;95m\] — Purple
\[\033[00;96m\] — Cyan
\[\033[00;97m\] — WhiteBackground:
\[\033[40m\] — Black
\[\033[41m\] — Red
\[\033[42m\] — Green
\[\033[43m\] — Yellow
\[\033[44m\] — Blue
\[\033[45m\] — Purple
\[\033[46m\] — Cyan
\[\033[47m\] — WhiteBackgrounds with high intensity:
\[\033[00;100m\] — Black
\[\033[00;101m\] — Red
\[\033[00;102m\] — Green
\[\033[00;103m\] — Yellow
\[\033[00;104m\] — Blue
\[\033[10;95m\] — Purple
\[\033[00;106m\] — Cyan
\[\033[00;107m\] — White
The above colors were sourced from here.
If you have followed through, I’m glad your terminal is more awesome!
Happy coding!
adiós!