Git : Collaborative Development

Git is an Open Source Distributed Version Control System used for tracking changes in code during software development.

Let me break it down and explain the wording:

  • Control System: This basically means that Git is a content tracker. So Git can be used to store content — it is mostly used to store code due to the other features it provides.
  • Version Control System: It keeps track of all the changes being make to code base and creates a new version when an code is checked in keeping the old version as it is
  • Distributed Version Control System: Distributed because it can keep track of a code base that is being updated by different developers. Full history of the code is available to all the developer through the remote repository.

Why a Version Control System like Git is needed

In real life multiple developers work on the same code base in parallel. So a Git is needed to keep track of the changes and ensure there are no code conflicts between the developers.

A version control system allows developers to revert back to an older version of the code in case of any issues with the code.

Let’s get started on using Git now

Now let us see through examples how git works.

Download git

Use this like to download and setup Git in you system:
https://git-scm.com/book/en/v2/Getting-Started-Installing-Git

VerTo verify if Git is installed properly use the following command in command prompt(windows) or terminal (linux).

git --version

Create your local Git repository

To use git we need to have repository where we are going to initialize git.

In your computer, create a folder/directory for your project. Give it a name, for our example we will use project folder as git-demo.

Now to initialize a git repository in the new folder/directory created open the command line and type the commands:

cd simple-git-demo 
git init

The git init command initializes a local Git repository to the project.

Let’s Add some Small Code now

Create a file called demo.txt in the project folder and add some text into it for demo purpose:

Initial Content

Staging and Committing the code

Committing is the process in which we check in our code into local repository once the changes are done. Before committing the code, it has to be in the staging area. Ii keep track of all the files that are to be committed.

Files not added to the staging area will not not be considered for commit. Depending on the files the developer want to commit, he/she moves it to staging area.

Staging

Use the following command for staging the file:

git add demo.txt

In case you want to add multiple files you can use:

git add file1 file2 file3

To add all the files inside the local repository to the staging area, use the following command:

git add .

Committing

Use the following command to commit the file:

git commit -m "Initial Commit"

“Initial Commit” is the commit message here. Every commit has to have a commit message, add a relevant commit message to indicate what code changes were done in that particular commit.

Git Status and Git Log

Now modify the demo.txtfile and add the following snippet:

Initial Content 
Adding more Content

Status

git status is used to check which files were modified and what files are there in the staging area.

Use the following command to see the status:

git status

The status shows that demo.txt is modified and is not yet in the staging area.

Now let us add demo.txt to the staging area and commit it using the following commands:

git add demo.txt 
git commit -m "demo.txt file is modified"

Log

git log returns all the commits that has been made to the repository till now.

The command used for this is:
git log

The log returns details like author of each commit, the date of commit and the commit message.

Branches

By default, Git commits go into the master branch. In git we can create different branch from the master branch and work on different changes simultaneous and later merge them. Let us see more about branching in detail.

What is a branch?

A branch is a pointer to the latest commit in the Git repository. So currently our master branch is a pointer to the second commit “demo.txt file is modified”.

Why are multiple branches needed?

For parallel developments in the same code base we need branches. Refer the image below to see how branches work.

At first commit 1 and commit 2 were done in the master branch. The we see a new branch was created from commit 2 called as “Test”. In the new “Test” branch commit 3 and commit 4 are made.

At the same time, some one else may be working on master branch and a different commit 3 and commit 4 is made to it. Here we can see that after Commit 2, two parallel developments are being done in 2 separate branches.

Later the Test Branch can be merged with the Master branch using git merge. This will make sure that all the changes or commits of “Test” branch that are not in the “master” branch are added to it.

Create a New Branch in Local

Create a new branch called test using the following command:

git branch test

This command creates the test branch.

We are still in the context of the master branch. In order to switch to the testbranch. use the following command:

git checkout test

Now we are in the test branch.

You can list out all the branches in local using the following command:

git branch

Do Some Commits in the New Branch

Modify demo.txt by adding the following snippet:

Initial Content 
Adding more Content
Adding some Content from test Branch

Now stage and commit using the following commands:

git add demo.txt 
git commit -m "Test Branch Commit"

This commit was done in the Test Branch, and now Test Branch is ahead of Master Branch by 1 commit — as the test branch also includes the 2 commits from the master branch.

You can verify the commit history in Test Branch using:

git log

Merging

Currently, Test Branch is ahead of the Master by 1 commit. Let’s say that now we want all the code in the Test Branch to be brought back to the Master Branch. This is where git merge is very useful.

In order to merge the code from the test branch into the master branch, follow these steps:

First go back to the master branch:

git checkout master

Then run the merge command:

git merge test

After running these 2 commands, the merge should be successful. In this example, there are no conflicts.

In real projects, there maybe conflicts while we try to merge two branches. Resolving the conflict is something which comes with experience, so as you work more with Git you will be able to get the hang of resolving conflicts.

Run git lognow and you will notice that the master also has 3 commits.

The Remote Git Repository

Till now, we were working only in the local repository. During development each developer works on their own local repository of the same code base. But eventually, they have to push their code changes into a remote repository. Once the code is in the remote repository, other developers can see and modify that code.

Showing Remote and Local Repositories

GitHub

Here we will be using GitHub for the remote repository.

Go to https://github.com/ and create an account.

After creating your GitHub account, create a new remote repository by click ing on Start a Project. Give the repository a name and click “Create Repository”

We will use the name git-remote-demo.

The repository URL is the highlighted portion https://github.com/your-usernname/git-remote-demo.git

In order to point your local repository to the remote repository, use the following command:

git remote add origin [repository url]

Git Push

To push all the code from the local repository into the remote repository, use the following command:

git push -u origin master

This pushes the code from the master branch in the local repository to the master branch in the remote repository.

Additional Commands

Git Pull

git pull is used to pull the latest changes from the remote repository into the local repository. The remote repository code base is updated continuously by various developers who are working on the same project, hence git pull is necessary from time to time:

git pull origin master

Git Clone

git clone is used to clone an existing remote repository into your computer. The command for this is:

git clone [repository url]

Congrats

Now you know the basics of how to use Git, so go ahead and explore more!

I will soon be publishing one more article on slightly more advanced concepts of Git. Stay tuned!

You can also checkout our this posts on Machine Learning and Deep Learning

Spread the knowledge

xpertup

Let's Expert Up

Leave a Reply

Your email address will not be published. Required fields are marked *