Quantcast
Channel: linoxide.com
Viewing all articles
Browse latest Browse all 1447

Install and Configure Git on Ubuntu 16.04

$
0
0

Git is an open source, distributed, version control system designed to handle every type of projects from small to big with speed and efficiency. It is easy to learn and has a low memory consumption with lightning speed performance. It surpasses several other SCM tools like Subversion, CVS, Perforce and ClearCase with features like cheap local branching, convenient staging areas, and multiple workflows.

Moreover, Git 2.9.0 has a variety of features and bug fixes in comparison with rest of the versions, some of the advanced features of Git 2.9 making it prominent from rest is as below:

  • Faster and more flexible submodules : It brings support for cloning and updating submodules in parallel.
  • Beautiful diff usages : It adds a new experimental heuristics for diff handling.
  • Testing commits with Git interactive rebase

Advantages of GIT over others SCM tools

  • Branching and Merging
  • Small and Fast
  • Distributed
  • Data Assurance
  • Staging Area
  • Free and opensource

In this article, I'll demonstrate how to install the latest Git version  on an Ubuntu 16.04 server. Let's start with the installation steps.

Installing Git

On an Ubuntu server, we can install Git packages from their repositories by just running this command.

root@ubuntu:~# apt-get update
root@ubuntu:~# apt-get install git

But it's not mandatory that we get the latest Git release packages by installing this way. In such case, we prefer to install Git by downloading from their source packages. We can download our Git release packages here.

I'll explain the steps on how I installed the latest Git 2.9.0 version on my system.

Download the Git files

Step 1 : Download the Git 2.9 package from the above download link

root@ubuntu:~# wget https://github.com/git/git/archive/v2.9.0.zip
root@ubuntu:~# unzip v2.9.0.zip

Install the unzip module if it's not present in the server by just running this command "apt install unzip".

Configure and Build

Step 2 : Move to the extracted Git folder and start configuring. First, we need to make the configure and build the Git package. Inorder to make the configuration part to work, we need to install autoconf in our server.

root@ubuntu:~/git-2.9.0# apt-get install autoconf

root@ubuntu:~/git-2.9.0# make configure
GEN configure
root@ubuntu:~/git-2.9.0# ./configure --prefix=/usr/local

After installing autoconf, we can create the configure file for Git and start configuring using the above command.

But during the configuration time, if you come across a similar error, please install the following package.

Error:

configure: error: in `/root/git-2.9.0':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

 Fix : 

root@ubuntu:~/git-2.9.0# apt-get install gcc

You can install gcc package to enable the C compiler on your server and complete the configuration part smoothly like this.

root@ubuntu:~/git-2.9.0# ./configure --prefix=/usr/local
checking for BSD sysctl... no
checking for POSIX Threads with ''... no
checking for POSIX Threads with '-mt'... no
checking for POSIX Threads with '-pthread'... yes
configure: creating ./config.status
config.status: creating config.mak.autogen
config.status: executing config.mak.autogen commands

Our next step is to build the Git packages. We can still building the package by running this make command.

root@ubuntu:~/git-2.9.0# make prefix=/usr/local

PS : At times, you may come across some errors while running this, due to some missing packages.

Errors :

root@ubuntu:~/git-2.9.0# make prefix=/usr/local
CC credential-store.o
In file included from credential-store.c:1:0:
cache.h:40:18: fatal error: zlib.h: No such file or directory
compilation terminated.
Makefile:1935: recipe for target 'credential-store.o' failed
make: *** [credential-store.o] Error 1

/bin/sh: 1: msgfmt: not found
Makefile:2094: recipe for target 'po/build/locale/pt_PT/LC_MESSAGES/git.mo' failed
make: *** [po/build/locale/pt_PT/LC_MESSAGES/git.mo] Error 127

In order to rectify these errors, you can install the following packages which is needed by the Git.

apt-get install zlib1g-dev
apt-get install tcl-dev
apt-get install libssl-dev
apt-get install gettext

After fixing these errors, you can re-run these make commands to complete the build process.

root@ubuntu:~/git-2.9.0# make prefix=/usr/local
root@ubuntu:~/git-2.9.0# make prefix=/usr/local install

makeinstall

Now we can confirm our Git installation. You can set the environment variable to fetch the Git libraries from /usr/local by running ldconfig.

root@ubuntu:~# git version
git version 2.9.0

Git Setup

Git comes with a tool called git config which allows you to get and set configuration variables that control all aspects of how Git works. These variables can be stored in three different places:

getconf

/etc/gitconfig file : This file contains values for every user on the system and all their repositories.

git config --system : This option will reads and writes from this file specifically.

~/.gitconfig or ~/.config/git/config file : This file is specific to each user.

git config --global : This option will reads and writes from this file specifically.

.git/config : config file in the Git directory of whatever repository you’re currently using. This file is specific to that single repository.

git config --local : This option will reads and writes from this file specifically.

Creating your Identity

First thing, which you need to do after the Git Installation is marking your identity. You need to set your username and email address. This is important why because Git commit uses this information and it's immutably attached into the commits which you're creating.

root@ubuntu:~# git config --global user.name "Saheetha Shameer"
root@ubuntu:~# git config --global user.email linoxide1@gmail.com

Checking your Git settings

You can check your current Git settings by using the command git config --list. This will list all Git settings.

root@ubuntu:~# git config --list
user.name=Saheetha Shameer
user.email=saheetha1@gmail.com

You can also check the status by typing a specific key value like this

root@ubuntu:~# git config user.name
Saheetha Shameer

Git Commands

You can get more about Git commands by running the git help command. Here are some of the common Git commands and their uses.

gitcommands

You can get the Git Manual page by running this command git help config.

 Creating and Managing a Git Repository

First of all, let's see how you can create a Git repository. You can run this command to create your git repository on the existing folder. I created a folder called gitdemo and initiated the command git init to create my repository.

root@ubuntu:~# mkdir gitdemo
root@ubuntu:~# cd gitdemo/
root@ubuntu:~/gitdemo#
root@ubuntu:~/gitdemo# git init
Initialized empty Git repository in /root/gitdemo/.git/

After execution of this command you can see it creates a folder called .git. This is where git stores everything including change sets, branches etc. Let's see the structure of this folder.

gitfilestruc

At any time you can delete this folder to destroy your repository. In short, this means git uses a local file base setup where you can take manual backup of this folder to preserve or commit to any remote repository or even sent these backup file to your friends, to give them direct access to your repository with git installed in their system.

At this moment, we don't have anything to put under version control, so let's create a file and check the git status.

root@ubuntu:~/gitdemo# touch testrepo.txt
root@ubuntu:~/gitdemo# git status
On branch master

Initial commit

Untracked files:
(use "git add <file>..." to include in what will be committed)

testrepo.txt

nothing added to commit but untracked files present (use "git add" to track)

Even though the file testrepo.txt exists, it isn't yet tracked by Git.  Git status tells us that our file is being untracked. We need to fix this by the command git add <filename>.

root@ubuntu:~/gitdemo# git add testrepo.txt
root@ubuntu:~/gitdemo# git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: testrepo.txt

Now our git status shows as our test file is ready to commit. This is called Staging. We've staged to this file to commit.

Before committing the repo, we need to initiate git add command to update all changes. For example, if we modify anything in our test file, that won't add the changes on commit until we run this git add command again. Git status will help you identify the modifications.

root@ubuntu:~/gitdemo# git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: testrepo.txt

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: testrepo.txt

Hence, we need to run the git add command to stage these changes.

root@ubuntu:~/gitdemo# git add testrepo.txt
root@ubuntu:~/gitdemo# git status
On branch master

Initial commit

Changes to be committed:
(use "git rm --cached <file>..." to unstage)

new file: testrepo.txt

Once we stage our changes, we are ready to commit them to our repository. You can use git commit command for that. By running this command "git commit testrepo.txt", it will display a vim window, where you need to type "Initial commit" on the top of the screen and save to exit.

commit

root@ubuntu:~/gitdemo# git commit testrepo.txt
[master (root-commit) 2b3f303] Initial commit
1 file changed, 2 insertions(+)
create mode 100644 testrepo.txt

PS : you can even use git commit -m "changes" instead

If we run the git status again, we can see that there are no more pending changes which means they're all committed to our repository.

root@ubuntu:~/gitdemo# git status
On branch master
nothing to commit, working directory clean

We can get the details of our commit history by running git log, which will provide with their details like authors, time, date , commit notes etc.

root@ubuntu:~/gitdemo# git log
commit 2b3f30387f3b7417acbbc5287132df7441aa0881
Author: Saheetha Shameer <linoxide1@gmail.com>
Date: Thu Jul 14 08:02:52 2016 +0000

Initial commit

You can get even more information about the git log by referring its manual with the command man git log.  I hope this article is informative and useful for you. Thank you for reading this. Have a Nice Day!

The post Install and Configure Git on Ubuntu 16.04 appeared first on LinOxide.


Viewing all articles
Browse latest Browse all 1447

Trending Articles