Getting Started with GIT

GIT Installation

  1. GIT can be installed on many platforms like many other versioning tools available.
  2. We will be installing GIT on Windows 10,Mac OS High Sierra and Linux Centos 6.9 here.
  3. Windows installation comes with an executable installer which can be used to install git on the platform.
    1. Depending upon your operating system(32/64 bit) you can download the executable.
    2. The latest version for 64 bit available is 2.16.2.
    3. Windows users who use cygwin can install it via commandline or cygwin's setup executable.
  4. Linux has a GIT package which can be installed depending upon the version.
  5. OS X users can install either using .dmg image or use Macports or Homebrew for compiling and installing GIT code.
  6. Git for windows as stated earlier easy to install we just need to execute the GIT setup available at https://git-scm.com/downloads
  7. For OS X download the dmg image from https://git-scm.com/downloads
    1. However if you want to work on latest version of GIT you need to download the source code from GitHub and compile the same using Macports or Homebrew.
  8. To Install in Linux use the following commands
    1. yum install git
      1. This will install GIT
    2. git --version
      1. This will show GIT version.
GIT Configurations

  1. Our name and email both are used to generate hash on SHA-1 that identifies each commit.
    1. Following commands are needed to set these
      1. git config --global user.name "YourName".
      2. git config --global user.email "user@domain.com".
  2. Commands with --global specify that their configuration values are for every repository we interact with on the machine.
  3. GIT global configuration file is "~/.gitconfig".We can edit this file directly even instead of using config commands.
  4. We can also set settings per repository basis by leaving out --global command.These settings are stored in .git/config file.
    1. Set the GIT user for specific repository
      1. cd /path/to/repository
      2. git config user.name "Your Name".
      3. git config user.email "user@domain.com".
  5. Changing "colour.ui" to "auto" makes command line interface colorised.
    1. Turn colours on where ever possible in GIT UI
      1. git config --global color.ui auto.
  6. Changing core.editor changes the default GIT editor to our particular editor.
    1. GIT launches an editor whenever we want to create a commit message, edit patches, and do a few other tasks.
      1. Configure GIT editor
        1. git config --global cor.editor /path/to/editor
    2. Following values are checked in order for editor to be used
      1. GIT_EDITOR environment variable 
      2. core.editor configuration value   
      3. VISUAL environment variable
      4. Editor environment variable
      5. plain vi
Lets try these commands

Creating a new repository
  1. Repositories are stored in local file system.
  2. To create a repository type "git init" in the directory you want to start tracking files.
    1. This creates a .git directory in your current directory and initializes a .git repository inside.
    2. You have to initialize the repository only once.
    3. This will create a private repository for you.
  3. There are 2 types of repositories in GIT
    1. private : is created on our local system and where all work is done.
    2. public : shared with all others.
  4. This 2 tier system gives you the ability to track local experimental changes.
    1. And share changes via public repository that are ready for others to work with.
  5. Once initialized a repository we need to add and commit the files using "git add" and "git commit" respectively, but both of these require an initialized repository first.
  6. Once initialized we have a working tree that we can interact with.
Lets try out this

Creating a local copy of existing repository

  1. "git clone" command initializes a new repository on our computer fetches entire history from an existing public repository.
    1. git clone some-repository
    2. Clone repository to a specific path
      1. git clone some-repository some-path.
  2. If we don't require entire history and only need up till a specific revision.We can use -depth parameter to limit how many revisions you fetch.This is called a shallow repository.
    1. There are a few limitations of this type of repository clone.
      1. We can't create another clone from it.
    2. Create a shallow clone with the last fifty commits
      1. git clone --depth 50 some-repository
  3. GIT uses SSH by default to transfer changes over the network but it also uses the GIT protocol on port 9418.
Lets try cloning a repository
I need to clone following repository in folder I just created

It is cloned as follows

Comments