Wednesday, July 20, 2016

How to Install Git on CentOS/RHEL 7/6/5 & Fedora 23/22


Git is a free and open source distributed version control system . It is designed to handle a small to very large projects with speed and efficiency. To read more details and changes about Git 2.9.1 read
Git release notes. If you are using a Git repository for your development, then you must required a git client on your system and you need to install git client on your workstation system

This tutorial will help you to install git core 2.9.1 on CentOS/RHEL 7/6/5 operating systems using source code. For beginners visit Git console (http://try.github.io/levels/1/challenges/1) on your browser and learn Git uses and command line tips.

Step 1 – Install Required Packages
Before installing Git from source code, make sure you have already installed required packages on your  system. If not use following command to install required packages.

# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
# yum install gcc perl-ExtUtils-MakeMaker


Step 2 – Download and Install Git
Download latest Git source code from kernel git or simply use following command to download Git 2.9.1.

# cd /usr/src
# wget https://www.kernel.org/pub/software/scm/git/git-2.9.1.tar.gz
# tar xzf git-2.9.1.tar.gz

After downloading and extracting Git source code, Use following command to compile source code.

# cd git-2.9.1
# make prefix=/usr/local/git all
# make prefix=/usr/local/git install


Step 3 – Setup Environment

After installation of git client. Now you must need to set binary in system environment.
So edit /etc/bashrc file and add below content to it

# export PATH=$PATH:/usr/local/git/bin

Now execute below command to reload configuration in current environment.

# source /etc/bashrc

After completing above steps. Let’s use following command to check current git version.

# git --version


If it is showing older version then grep and remove it
# rpm -qa | grep git
git-1.8.3.1-6.el7_2.1.x86_64
# rpm -e --nodeps git-1.8.3.1-6.el7_2.1.x86_64

check for newer version
# git --version
git version 2.9.1

Setting up User Identity:
# git config --global user.name "John Doe"
# git config --global user.email peterdrucker@sample.com
# git config --global color.ui true
# git config --global color.status auto
# git config --global color.branch auto
# git config --global core.editor vim
# git config --global merge.tool vimdiff
# git config –list

Git – Life Cycle:





Step 4 – Create user and base repository
Create New user:
# groupadd devs
# useradd -G devs -d /home/sameer -m -s  /bin/bash sameer
# password sameer


Create a Base repository:
# pwd
/home/sameer
#  mkdir project.git
# cd project.git/
# ls
# git --bare init
Initialized empty Git repository in /home/sameer/project.git/
# ls
branches  config  description  HEAD  hooks  info  objects  refs

Step 5 – Configure git client on your local windows machine

Download and install git client and install
Open git gui  and select - > create new repository or Open existing repository.
Generate private/public keys for the SSH server
Generate key with secure phrase @ 4096 encryption
Install the new private ssh key on the remote server
private ssh key, remote server, authorized_keys
Install the new private ssh key into git client
Open -> Remote -> select Add then enter Name, location of remote server
(Location: ssh://username@192.168.1.7:22/home/user/repo/project.git)
Push your first commit to create the master remote-> push ->master

Done...


* Note: 
error: bash: git-receive-pack: command not found
fatal: Could not read from remote repository.

Adding this to my .bashrc via PATH=$PATH:/new/path/here worked for me
vi /home/user/.bashrc
PATH=$PATH:/usr/local/git/bin
source /etc/bashrc

Note:
You can easily restrict the git user to only doing Git activities with a limited shell tool called git-shell that comes with Git. If you set this as your git user’s login shell, then the git user can’t have normal shell access to your server. To use this, specify git-shell instead of bash or csh for your user’s login shell. To do so, you must first add git-shell to /etc/shells if it’s not already there:

# cat /etc/shells   # see if `git-shell` is already in there.  If not...
# which git-shell   # make sure git-shell is installed on your system.
# sudo vim /etc/shells  # and add the path to git-shell from last command

Now you can edit the shell for a user using chsh <username>:
# sudo chsh git  # and enter the path to git-shell, usually: /usr/bin/git-shell

Now, the git user can only use the SSH connection to push and pull Git repositories and can’t shell onto the machine. If you try, you’ll see a login rejection like this:

# ssh git@gitserver
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to gitserver closed.


Now Git network commands will still work just fine but the users won’t be able to get a shell. As the output states, you can also set up a directory in the git user’s home directory that customizes the git-shell command a bit. For instance, you can restrict the Git commands that the server will accept or you can customize the message that users see if they try to SSH in like that. Run git help shell for more information on customizing the shell.


Git usage and example:

To create a new git repository, create a folder and apply this command
# git init

To create a working copy to a local repository
# git clone /path/to/repository

To creat a working copy from remote repository
# git clone username@host:/path/to/repository

Workflow:
local repository consists of three trees maintained by git.
1. Working directory  (holds actual files)
2. Index   (staging area)
3. HEAD  (points to last commit)

Add and commit:
add changes to the index using
# git add <filename>
# git add *

To commit changes to HEAD
# git commit -m "commit message"

To connect repository to remote server
# git remote add origin <server>

To send changes to remote repository
# git push origin master

To create a local branch and swith to it
# git checkout -b <branch>

To switch back to master branch
# git checkout master

To delete the branch
# git branch -d <branch>

To push branch to remote repository
# git push origin <branch>

To fetch and merge remote changes to local repository
# git pull

To merge another branch into active branch
# git merge <branch>

Mark them as merged with before merging changes
# git add filename

To preview changes and differences 
# git diff <source_branch>  <target_branch>

To create a new tags for software releases with commit id
# git tag 1.0.0 1b2e1d63ff

To see only the commits of a certain author
# git log --author=bob

To see a very compressed log where each commit is one line
# git log --pretty=oneline

To see an ASCII art tree of all the branches
# git log --graph --oneline --decorate --all

To see whichfiles have changed
# git log --name-status

To replace changes in your working repository
# git checkout --<filename>

To drop all your local changes and commits, fetch the latest history from the server and point your local master branch at it
# git fetch origin
# git reset --hard origin/master