How to install and use Git on Ubuntu

Git

What is “version control”, and why should you care? Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. For the examples in this book you will use software source code as the files being version controlled, though in reality you can do this with nearly any type of file on a computer.

— definition from Git

Now suppose you are a programmer and you want to preserve every version of your work on disc. And then version control comes in picture. There are many tools available for version control like SVN GIT etc but here we will discuss about GIT.

In this article, I will tell you “How to install and use Git on Ubuntu”. I haven’t specified Ubuntu version here because, the screenshots used here are from Ubuntu 17.04. You can use same command on Ubuntu 14.04 too but you need to use apt-get because short codes are not supported by Ubuntu 14.04.

#Installation

The easiest way to install GIT on ubuntu is using apt package. It is best practice to update apt package before any installation on Ubuntu.

$ sudo apt update

$ sudo apt install git

Config

# Setting up Global Username

Once installation has been completed you need to setup Global Configuration i.e., Name, Email etc

$ git config –global user.name “Digvijay”

$ git config –global user.email “digvijay@spokenbyyou.com

Config

You can see all the configuration by using:

$ git config –list

How to install and use Git on Ubuntu 1

# GIT Clone

First time when you are taking everything from Centralized Git Server (Some common server are GitHub, BitBucket etc, You can also setup your own), you need to use git clone command.

$ git clone <repository URL>

Git Clone

It creates a directory on your local  with same name as in repository and copies every file from Centralized Git server. E.g.,

List

# Creating Branch

Default branch in git repository is master  branch but you can create your own branch too. This can be also be done using command line.

$ git checkout -b <new branch name>

Create new branch

Once created, you need to push branch to the central server.

Now suppose, we have  already created a branch and we want to work in that branch. Just run the above command without ‘-b’. It will switch the branch.

$ git checkout <new branch name>

If you want to list all the branches and current active branch type:

$ git branch

Branch

# Commiting and pushing file

While working with files, You need to commit and push the files changed by you, so that other people can pull it on his local and work with them. Now see the number of files changed by you

$ git status

Status

Now, you need to add the red files and add them to commit list using

$ git add testpackage.php

Once added, you can commit all the added files using commit command. If you want to add commit message in multiple lines, just start single quote and after each line of commit message press enter key without closing quote. It will add new line commit message. Once completed with the message close the quote and press enter. It will finish commit all the files with multiliner comments.

$ git commit -m ‘ line 1

> line 2

> line 3

>’

Commit

Once commit done. Use push command to push files on central server.

$ git push origin newbranch

# Git pull

If someone from your team pushed something into file and you want that changes on your system use pull command

$ git pull origin master

These are some basics of git commands. You can also change the commit message after push but that is not best practice.

There are many GIT commands available but, here I am showing some basic commands only.

Please share your thoughts on this article as comment. If you have more information or any success story please write using speakup here.