github_helper

Hello Everyone!! Hope everything is going great, I know its been sometime since I last posted anything, had a super busy semester. Anyways in todays post I thought of writing about adding git support to a terminal. This post is more of a ‘How to?’ post, and not really an introduction to git. So if you have no idea what git is all about and you program a lot, then I think you should really consider learning it, and heres why…

In a nutshell, git is a version control system that programmers use to keep track of their code.

Once you start working on big projects with potentially thousands of lines of code, it becomes quite unbearable to keep track of every change one would make. Specially when one small change could potentially break the entire program. By using git one is able to keep versions of the same code before any significant modification, so if something bad happens and the code breaks its always possible to roll back and re-try changing the code starting off from a correct version. I actually learned about git not more than a year ago and it really is a life saver. I learned about git from an online free course provided by Udacity. Click here to check this course out. In fact, the neat trick about adding git support to terminal is also something I picked from there.

If you are someone who is already familiar with git, and you only want to know how get this working then read on. But before all of that, you might wonder…

“What kind of features will this add?”

As you can see in the image above, if you follow the steps I mention in this post, you can view helpful git debugging information in the terminal itself as shown in the image above, which shows the status of my Github repo for this website. This includes,

1. The current git branch: The git branch you’re checked in is shown in green inside parenthesis. In my case, I’m checked into a branch for this post called post_10.

2. Un-committed/added changes:If there is any change that is not committed or added, it will be shown as a green * symbol right next to the branch name. In my case, the feed.xml file is modified and not committed, so there is a Asterix shown.

3. Changes added but not committed: Similar to the above case, if there are any changes that are added but not committed its shown as a + symbol. In my example, after adding the changes with git add --all the symbol changes from * to +.

4. Merge status: After pulling from a branch, if a merge is required then an additional text as | MERGE will show right next to the branch name.

So as you can see, this automatic notifications inside the terminal itself really helps out when you code. Now there are two methods of adding this functionality to you’re terminal, depending on wether you are using a mac or some linux distribution.

Steps if Mac

  1. Download this file into your home directory and name it as git-completion.bash.
  2. Download this file into your home directory and name it as git-prompt.bash.
  3. In your home directory if there is a file called .bash_profile, then copy the contents of the code below into it. If not then create a new file called .bash_profile and copy this code into it. (Note that these colors work best when the background is white or a light color, You can change the color scheme to your taste)
# Enable tab completion
source ~/git-completion.bash

# colors for the notifications
green="\[\033[0;32m\]"
blue="\[\033[0;34m\]"
purple="\[\033[0;35m\]"
reset="\[\033[0m\]"

# Change command prompt
source ~/git-prompt.sh
export GIT_PS1_SHOWDIRTYSTATE=1
export PS1="$purple\u$green\$(__git_ps1)$blue \W $ $reset"

Steps if Linux Distribution

  • If you are working on a linux distribution, then there is only one change that is required. First of all follow steps 1 and 2 as is. Afterwards, copy the above code snippet (after changing the color scheme if needed) into a file called .bashrc(not .bash_profile).

After all these steps are complete, then restart a new terminal and this should start working. Hope this makes it easy when working with git repositories.

So until next time,
Cheers!

Next: Making TA-life easier : Bash scripts for grading
Prev: Finding cycles in linked lists