My git settings

A good practice to use git in command line

I wish I could port my git settings from one computer to another. But how ? If only there was a guide I could follow for that. So I’ve decided to document it here so my future self can check it anytime.

Here are the settings I use for git.

Benefits

If you were wondering how some people can actually use git in command line (isn’t that dangerous ?!), well with the following settings, you’ll see a good practice that eases the git workflow and makes using git in command line a pure delight.

By following this quick setup guide, you will be able to use GIT enhanced with features that help you visualize and understand what you’re doing. These are the settings I use and it works well and it’s very practical. It works on Ubuntu and Mac OS.

Demo

Take a look at all the nice features I just promised :

> autocomplete :

You will be able to autocomplete on branch names local and remote + on git commands. Woa.

> result of git diff :

Result of git diff

As you can see, added lines are in green, removed lines are in red. The rest helps the visualisation.

> branch status :

Branch status

The current branch is displayed and highlighted in the prompt, and there are %,=,> signs indicating if there are changes (%), if current branch is ahead of remote branch (>), if current branch is up-to-date with remote branch (=).

In next section, you’ll see that this is taken care of by .git-prompt.sh.

> result of git push :

Result of git push

It colors the changes.

> prompt :

Customized shell prompt

As you can see, the prompt is customized and you can see where you are and the name of the working branch.

Also taken care of by .git-prompt.sh.

So all these features exist to help you visualize better what you’re doing in git, it’s very handy. Honestly without these settings I find it difficult and rather dangerous to work with git because you just see nothing.

Guide

Here what you need to do to make this work :

#1 Go to https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

> Download the script and save it in your home with the name .git-prompt.sh OR

> If you want to go really fast cd to your home and run this command :

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-prompt.sh -so .git-prompt.sh
It will fetch the script directly and put it in your home directory.

This script will be used to customize your prompt.

#2 Go to https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

> Download the script and save it to your home with the name .git-completion.sh OR

> In the same manner as before, if you want to go really fast cd to your home and run this command :

curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -so .git-completion.sh

It will fetch the script directly and put it in your home directory.

This script will let you autocomplete on the branch names local and remote (once you fetched the changes), and on the git commands. Fantastic, I know.

Note :
Those links are for the bash shell. If you don’t use bash, simply browse the repository to find the right script for your shell.

#3 Now that you have those two files, you need to change the way your shell will look. You do this in your .bashrc file in your home. The changes to make are in the following snippet.

Note :
Depending on your system, the file used to customize the shell can be .bashrc, .bash_profile, or .profile. Just find out which file customizes your shell, and apply the same changes described here.For mac os, you will have to edit .profile in your home, and if it doesn’t exist, just create it.

#4 Once this is done, to get all the git colors simply edit your .gitconfig file in your home.

> To do #3 and #4 go to your home and edit the .bashrc and .gitconfig files like in these snippets (for .bashrc, only append those lines do NOT replace all the content with those lines):

 

And now normally, you’re done ! You’ve got all the nice effects mentioned earlier. If you don’t see the effects in your shell right away, try to relaunch your terminal.  You can enjoy GIT !

***

If you need to customize this further, or understand better what is going on, you can see it all documented on the github pages previously cited :

https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh

https://github.com/git/git/blob/master/contrib/completion/git-completion.bash

The lines added in .bashrc result from following the documentation for those scripts actually embedded right in the script comments.

As a bonus, I recommend you do this little game to strengthen your mind in GIT mastery. It’s really fun to use – finally someone managed to explain GIT with visuals, thank God (or Git ?) ! => http://pcottle.github.io/learnGitBranching/ (tip : if you’re stuck use the show solution command, the website doesn’t make it obvious I only discovered it at the end when I finally solved the levels)

If you have other nice features for git that sweeten the git workflow, please share with me !

 

Posted in Technical, Tools & System

Tagged