Friday 20 November 2015

Git tutorial in linux for beginners Part 1





Introduction

Git is one of the modern version control systems, which has multiple advantages over CVS, Mercurial, etc.

It is distributed version control system, employing totally new concept for version control. It has very good features when multiple developers are working on the same project.

Git uses file system like structure, where it takes snapshot of file system of each version. It only tracks the changes in the files between each versions. Every thing is being stored in the simple text files. This new concept is space efficient compared to older version control systems.

Git is faster as it stores every thing on the local computer and then it pushes the changes to the server at the end. The change files are protected by check-sums, so, you don't need to worry about data integrity.


Git provides two type of accounts free and paid one. The free version allows you to store unlimited number of repositories with unlimited number of contributors. But all the repositories are publicly accessible. The paid version charges you some amount and provide you fixed number of repositories which are closed and have controlled access. In case of paid version also, you are free to create unlimited number of open repositories with unlimited contributors.


Installing Git for Linux:


On debian style linux you can install Git by following command.

:~$ sudo apt-get install git
The command above will ask you the user password which you need to enter.


Configuration of GitHub


You need to create account in GitHub website if you don't have it already. If you don't have one, you can do it from here. Once you install git in linux, next you need to do one time configuration for GitHub user in the local machine. This configuration is to be done once and it is pretty simple.

Step 1:

First we will configure user name for the git.


:~$ git config --global user.name "<username>"

Where <username> is to be replaced by your actual user name through witch you have registered yourself on GitHub.

Step 2:


We will now configure email associated with the git account.


:~$ git config --global user.email "<emailid>"

Where <emailid> is to be replaced by your actual user name through witch you have registered yourself on GitHub.


Creating new repository


Now you would want your code to reside in repository and you would definitely like to protect it from deleting unknowingly.

So do make separate folder for the git repo using mkdir command and change to newly created directory.


:~$ mkdir git-repo
:~$ cd git-repo

Now we will create fresh git repository in local machine by below command.


:~$ git init first-repo
Initialized empty Git repository in /home/ubuntu/git-repo/first-repo/.git/

This command will create a fresh repository named "first-repo" in the local machine.

If command is successful it will print the below message.

Initialized empty Git repository in <path where git repo intialized>

Here the path provided in the message is the actual path where you have created the empty repo.

You would notice that there would be new folder with repository name. Now you can change the folder to repository folder using cd command.


:~$ cd first-repo


Creating files and adding files to repository index

It is customary to keep README file in the repository to explain the purpose of the repository.

We can create it simply using gedit or any other text editor and write the description according to need.


:~$ gedit README

The file README will contain in my case "This is testing repo for learning Git".

Now save it and close the file.

We have created README file, now we will create actual code file for sample.


:~$ gedit hello.c

You can write some small program like below in the file and save it.

1
2
3
4
5
6
#include <stdio.h>

int main(){
 printf("Hello World...!");
 return 0;
}
Now you will have two files in the repo. README and hello.c.

Here it should be noted that repo index is a kind of file which tracks changes to files and is aware about how many files to be tracked for changes.

We will add it to repo index using "git add" command. 

:~$ git add README
:~$ git add hello.c

We can add multiple files with one command by specifying file names separated by space.

At this stage if you have added a file which you didn't want to add, you can easily remove it by command git rm <filename>. You can again add required file.

Now we have registered our files with git repo index, so git will start tracking changes in the file now on-wards.


Committing files to repo


Now we assume that this is our final stable version of code which we will finally freeze as first version by committing files to repo.

As we have already added files in the repo we can now commit using "git commit" command.


:~$ git commit -m "Initial release version with hello world string"

Here -m switch enables you to add message to your commit. The message is short description of the version we are committing. You can have your own description in the message.

This step cannot be completed before the previous step of adding files to repo using git add.


Creating repository on GitHub


We have just created local repository in our local machine. So, data is still in the local machine. To upload the data in the git servers and in order to be able to access it through internet, we need to create the repository with same name in the GitHub website and link it to our local repo.

The step is pretty easy.

Go to https://www.github.com and log-in with you credentials.

Now on the home page you will find Plus (+) sign at top left to create new repository.

Just create repository with the same name as local repo. In our case it is "first-repo" in the "Repository Name" field and click "Create repository" button to create a repository on the GitHub website.

Once this step is completed, we can now push the contents of the local repository on to GitHub repository on website. For that we need to connect our local repository to GitHub repository. We can connect it using "git remote add origin".


:~$ git remote add origin https://github.com/<username>/first-repo.git

In above command you have to replace <username> with your GitHub account's user name and first-repo.git with the local repo that you have created.


Pushing files in GitHub repository

This is the last step for securing your data with first version.

Until now we had data in the local machine, but now we will be pushing it to GitHub server using git push command.

:~$ git push origin master

After firing this command you will be prompted to enter username and password of your account.

After this step you can log in to GitHub and check the repo you had created. You will be able to see two files README and hello.c on the GitHub website.

We have created and pushed our first repo and secured the first version of our code.


Cloning Git Repo

Now whenever you want your files back you just need to clone your repository. We will see this by deleting the entire directory and will clone the already saved repo.

Use Linux command rm -rf for deleting directory first-repo. Try ls command in Linux which will show that there is no directory named first-repo.

Now use git clone command to get your data back. You can use git clone command at any other location where you want your repo to be cloned.


:~$ rm -rf first-repo
:~$ ls
:~$ git clone https://github.com/<username>/first-repo.git
Cloning into 'first-repo'...
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 4 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.
Checking connectivity... done.

This above steps shows cloning the repo and create first-repo folder. You can check the files inside directory using ls command, they will be intact again.

If you like the content, like our facebook page. 

2 comments:

  1. Vishal, Great work dud... Your are using unix as a OS(Awesome). I would like to see more tutorials from your side keep posted

    Ambuj

    ReplyDelete
  2. @Ambuj, keep following, there is more coming in this series.

    ReplyDelete