Improve your Git experience

With these Git setup tips

Cut down on tedious credentials typing

When you start to push to remote repositories on a frequent basis, typing in you credentials can get tedious. For example, my GitHub user name is gilles-leblanc, which is fourteen characters and my password is around twenty characters. Combine this with two carriage return and that’s a whole lot of typing.

You can cut down on this tedious typing in two ways.

First you can store your username for a remote repository service like GitHub in your .gitconfig file. When this is done, Git will use this username and stop asking you for it altogether. I prefer not to store any passwords this way tough. This could be a security risk in case of accidental commit of the .gitconfig file or someone could get read access to the file.

storing credentials in .gitconfig file

[credential "https://github.com"]
  username = gilles-leblanc

If you really want to store your passwords investigate encrypted .netrc files.

The second thing you can do is caching your credentials. The default cache length is 15 minutes, but you can set it to what you want (mine is five minutes).

setting up credential cache in .gitconfig file

[credential]
  helper = cache --timeout=300

You can edit your .gitconfig by hand or use the git config command to do it for you.

using git config

git config --global credential.https://www.github.com.username "gilles-leblanc"

git config --global credential.helper cache
git config --global credential.helper "cache --timeout=300"

Turn on the colours for more readability

Turning on the colours makes for a big change in readability, particularly when using git diff where you can scan for added and removed lines by colour.

Git output when colours on
git diff command with colour highlighting

Colouring is also applied on other commands like git log . You can turn on the colours either by using git config or by directly editing your .gitconfig file.

using the git config command

git config --global color.ui true

resulting .gitconfig file

[color]
  ui = true

Setup Git to use your favourite editor

Git can be configured to use your favourite text editor. The standard text editor will vary depending on your OS, distribution and installation. Mine was nano. While using Git isn’t a text editor intensive operation, using a familiar editor is great for writing multi-line commit messages and rebase scripts where you have to change several pick commands.

You can configure this in your .gitconfig or in your shell’s configuration files. Here is an example of both (using Bash for the shell).

in the .gitconfig

[core]
  editor = vim

in .bash_profile or .bashrc

export GIT_EDITOR=vim

Use Git and GitHub to store your Git dot files

You use Git and GitHub to track your source files, why not your dot files? Now that you have customised your settings, keep them safe in source control. Keeping your dot files in a Git repo will allow you to easily revert changes when something bad happens or just see when you changed a particular setting.

Hosting that Git repository on a service like GitHub (or BitBucket, Gitorius or others) will allow you to easily share and sync those files across different computers from anywhere.

I use a GitHub repository to store all my dot files. This is particularly useful for Vim files.

In case of emergency break out this link…

This one isn’t related to Git configuration, but I can’t talk about Git without mentioning this page here. On undoing, fixing, or removing commits in git, is a real life-saver when something bad happens. I have used it myself a couple of times when doing my first rebases on pending pull requests. Next time you realize you have just made a big mistake, follow the instructions on the site by following the hyperlinks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s