Setting Up Git

Install Git

To install Git under Ubuntu, it is as easy as running the following command:

sudo apt-get install git-core

To confirm whether it has been successfully installed, run the command below. This should return the path where git is located.

which git

To determine which version of git is installed:

git --version

Configuring Git

Git configuration is stored in three different locations which is based on the three levels of configuration.

  • System Level configuration which will be applied to every user of the system. This is stored in the /etc/gitconfig or Program Files\Git\etc\gitconfig for Windows.
  • User Level configuration which will apply to only a single user. The path for this level is ~/.gitconfig or $HOME\.gitconfig for Windows.
  • Project Level and this will be inside the project folder like my_project/.git/config and this will be only for that project.

All these files can be directly edited but we must be sure about their format and syntax. Any mistake can make big problems. Luckily we don’t have to edit them directly, Git comes with commands that will make the changes for us.

Depending on the three levels of configuration, the config command can be executed in three different ways:

  • For system level
    git config --system
  • For user level it is
    git config --global
  • For project level it is without the option parameter
    git config

Some basic and initial configurations can be:

git config --global user.name "M. Ahmad Zafar"
git config --global user.email "ahmad@someaddress.com"

To view the list of current configurations:

git config --list

To view a specific configuration setting:

git config user.name

Since this configuration is saved in the user level, file named .gitconfig in home, so we can locate the file from:

ls -la ~

To view the contents of the file:

cat ~/.gitconfig

Git can also be configured to use a particular text editor when it wants you to edit some file:

git config --global core.editor "vim -wl1"

Here -w means to wait for the editor to launch and l1 means to start at line 1.

Git can also be configured to use colours when showing text. By default, its a monochromatic text.

git config --global color.ui true

To auto correct typo (a correction after a delay of 1 second or higher, 0 for disabled):

git config --global help.autocorrect 1

Git can also be configured to automatically change the line breaks according to the settings. This is useful when team users have heterogeneous platforms (Windows, Linux and Mac):

git config --global core.autocrlf true

Just like BASH shell comes with autocomplete feature, we can optionally use the same feature for Git also by using the following steps:

  1. Move to the user home directory:
    cd ~
  2. Get the https://github.com/git/git/raw/master/contrib/completion/git-completion.bash file and place it in the home directory manually or using the wget or the curl -OL command.
  3. Rename it to make it hidden:

    mv ~/git-completion.bash ~/.git-completion.bash
  4. Edit ~/.bash_profile or ~/.bashrc file

    if [ -f ~/.git-completion.bash ]; then
       source ~/.git-completion.bash
    fi
    
  5. After the file is reloaded the autocompletion will work.

The help of Git can be invoked just like any other help in Linux:

git help
git help log

This same help can be invoked using the man command.

Configurations can be removed using the unset option.

Advertisements

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