Mentor Konnect
6 min readMay 28, 2021

--

Git is a free and open source distributed version control system designed to track changes in different files and directories in a Project. Like any other version control system one can not only track the historical changes that were done in a file or files, but also perform many actions like..

  1. Revert back to a particular version
  2. Compare files across different versions (commits)
  3. Track the committer and date of commits

Many such features offer one complete control over the files in a Project. Recovery is easy when things really get screwed up, like when one accidentally deletes a file or wants to rollback a change that isn’t intended to continue forward.

Version control in a nutshell is a system that records changes to a file or a set of files over time so that you can recall specific versions later.

How is Git different compared to other VCS ?

Git has an edge over the other Version Control Systems (VCS) like SVN, etc because of its unique features like..

Local branching
Git allows multiple local branching of the repositories that are independent of each other. This comes with multiple advantages like..

  • Easy switching between branches.
  • Easier merging and deletion of branches.
  • Using specific branches for specific purposes like a specific branch (let’s call it ‘master’) which is used for production deployment and another branch (let’s call it ‘develop’) for initial code commits during the development phase.
  • Using specific branches for specific purposes like a specific branch (let’s call it ‘master’) which is used for production deployment and another branch (let’s call it ‘develop’) for initial code commits during the development phase.

Convenient staging areas

It’s an intermediate area where the files are staged before the final commit. One can select to commit only some of the modified changes that one would want. Commit is generally performed in 2 steps..

The ‘git add’ command is used to add the files to be committed, and it moves the selected files to the staging area.

The ‘git commit’ command moves the files to the actual repositories where the files are maintained.

git commit -a skips the staging area and directly pushes the files to the repository

Support for multiple workflows.

Git is ‘distributed’ in nature. One can clone the entire repository into the local system, create multiple backups which can be used to replace the main repository in case of a major corruption. This makes it fail safe. The various supported workflows are

Subversion-Style Workflow

In this model Git will not allow you to push if someone has pushed since the last time you fetched.

Integration Manager Workflow

In this model only a single person commits to the particular repository. A number of developers then clone from that repository, push to their own independent repositories, and ask the integrator to pull in their changes.

Dictator and Lieutenants Workflow

In this model, some people called ‘lieutenants’ are in charge of a specific subsystem of the project and they merge in all the changes related to that subsystem. Another integrator called as the ‘dictator’ can pull changes from only his/her lieutenants and then push to the particular repository that everyone then clones from again.

What’s a Repository ?

A virtual storage for your source code where the files and directories along with their metadata are stored.

Git Setup

Follow the below steps to setup git on your local system..

  • Based on your operating system, download the relevant git installable from here
  • Follow the documentation here to setup and configure git locally

Cross check the following settings post configuration [ using the ‘gitconfig’ command ]

user.name
user.email

It’s a good practice to not commit files that are residual outcomes during project development activities, these files could be metafiles from the IDE that you are using, log files or for e.g. compiled class files from a Java based project and so on.

Setting up a .gitignore file in your repository’s root directory or at a system level tells Git which files and directories to ignore when you make a commit.

You can find more details about setting up a .gitignore file here

Contents of a sample .gitignore file for a Java ( Maven ) based project would look like below..

target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.checkstyle

Repository Creation

Once git has been set up locally, you can start creating a repository, adding & committing files, and perform many such operations.

Local Repository Setup

Let’s take a local directory which is not git controlled yet, and turn it into a git repository

$ cd /Users/<username>/<project_dir>$ git init

The above command will create a new directory .git which will contain files for the repository

Now, let’s add some files and commit them into our repository

$ git add src/main/java/*$ git commit -m 'First cut of the project'

Remote Repository Setup / Cloning

The best way to get started with a remote repository hosted on a cloud based git provider like Github, Bitbucket , Gitlab, etc is to use the clone feature (provided that you have the permissions to access the remote repository).

You can clone an existing remote repository into your local system by using the below commands

# The below command will clone the remote repository into your local system 
# and create a local directory with the same name as the <repo-name>
$ git clone <https://github.com/><repo-name>

You can pass optional params in the clone command based on your requirement.

# To clone the remote repo into a different local directory$ git clone <https://github.com/><repo-name> <local-dir-name>

Git Providers

There are many git hosting providers which may provide free services at an individual level. Below are a few names for your reference

Github

Gitlab

BitBucket

SourceForge

As an example, we will go through the steps to create an individual account [ currently free of cost ] on Github.

Step 1: Signup on Github here.

Screenshot of the signup page

Step 2: After successful signup, you can sign in and create a repository by visiting this link

Screenshot of the repository creation page

You can opt to create a public or private repository based on your choice

Step 3: You can opt for auto creation of files like Readme and .gitignore as visible in the screenshot above and it will appear in the repository. Now you can copy the URL of the repository and clone it onto your local system.

Cloning from a remote repository

Let’s try cloning the remote repository to our local system..

$ git clone https://github.com/mentorkonnect/SparkStreaming.gitUsername for '[<https://github.com/mentorkonnect/SparkStreaming.git>](<https://github.com/mentorkonnect/SparkStreaming.git>)': xxxxxxxxx
Password for '[https://](<https://mentorkonnects@gmail.com/>)xxxxx@github.com/mentorkonnect/SparkStreaming.git':
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), done.

** As you can see, that while cloning the repository, it asked for the credentials because the git credentials were not configured. This can be permanently configured using the below command

$ git config — global credential.helper ‘store — file ~/.my-credentials’

However, GitHub will be deprecating password based authentication very soon. You can configure 2-factor authentication to continue uninterrupted access to its services. Read more regarding this here.

Now, let’s check the contents of the repository that was cloned on our local system

$ cd SparkStreaming/
$ ls -l
total 8
-rw-r--r-- 1 xxxxxxx xxxxx 16 Jan 19 20:07 [README.md](<http://readme.md/>)

The Readme file appears in the list command above. The .gitignore being a hidden file isn’t visible.

Now you can start using the git commands to add, commit, merge your code and perform many such operations that are required to manage the files in git version control system.

References

https://git-scm.com
https://github.com

--

--

Mentor Konnect

Mentor Konnect is a learning platform to upskill one's technical expertise specially in the areas of Big Data, Data science, ML & AI and other niche domains.