24 Feb 2020
Git is an Open source version control System (It is a category of software tools that help software team manage changes to source code over time e.g. CVS, SVN, Mercurial, Fossil, and, of course, Git etc.) .
Difference between Git and other VCS:
The major difference between Git and any other VCS is the way Git thinks about its data. Conceptually, most other systems store information as a list of file-based changes. Other VCS systems think of the information they store as a set of files and the changes made to them over time .
Git thinks of or stores its data in another way. It thinks of its data like a series of snapshots of a miniature file system. With every commit, Git basically takes a Snapshot of what all your files look like at that moment and register a reference to that snapshot. To be efficient, It checks if the files have not changed, Git doesn’t store the whole file again, Only a link to the previous identical file it has already stored. It thinks about its data like a chain of snapshots.
The Three States:
Git has three states that your files can reside in: modified, staged, and committed:
Modified - Means that you have made changes to the file but have not committed it to your database yet.
Staged - Means that you have marked a changed file in its current version to go into your next commit snapshot.
Committed - Means that the data is safely stored in your local database.
This guide us to the three main sections of a Git project: the working tree, the staging area, and the Git directory.
Let’s get started with Git now:
You can download Git in multiple operating systems.
Verify if Git is installed by using the following command in the command prompt:
Create your local Git repository:
In your computer, create a folder for your project. Let’s name the project folder simple-git-demo. Go into your project folder and add a local Git repository to the project using the below commands:
The git init command adds a local Git repository to your project.
Let’s Add some Small Code now:
Create a file called demo.txt in the project folder and add the below text into it:
Here we will be demoing with only text instead of actual code, since the main focus of this article is on Git and not on any specific programming language.
Use the below 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
If you want to add all the files inside your project folder to the staging area, use the following command:
git add .
Use the below command to commit the file:
git commit -m "Initial Commit"
“Initial Commit” is the commit message here. Insert a relevant commit message to indicate what code changes were done in that particular commit.
Git Status and Git Log:
Now modify the demo.txt file and add the below snippet:
Initial Content Adding more Content
Use git status to find out information about files which are modified and what files are there in the staging area.
Use the below command to see the status:
The status shows that demo.txt is modified and it is not yet in the staging area.
Now 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"
Use git log command to print out all the commits which have been done up until now.
The command used for this is:
The log shows the author of each commit, the commit message and the date of the commit.
Till now we have not created any branch in Git. By default, Git commits registers into the master branch.
What is Branch?
A branch is a pointer to the recent commit in the Git repository. So currently our master branch is a pointer to the second commit “demo.txt file is modified”.Multiple branches are needed to support multiple parallel developments.
Create a new Branch in Local:
Create a new branch by name test using the following command:
git branch test
This command creates the test branch.
We are still in the context of the master branch. To switch to the test branch 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:
Do some commits in the new Branch:
Modify demo.txt by adding the following snippet:
Initial Content Adding more Content Adding Content from test Branch
Now stage and commit using the below commands:
git add demo.txt git commit -m " Test Branch Commit "
This commit was done into the Test Branch, and now Test Branch is ahead of Master Branch by a commit — as the test branch includes the 2 commits from the master branch.
You can also verify the commit history in Test Branch using:
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 useful.
In order to merge the code from the test branch into the master branch, follow below steps:
First go back to the master branch:
git checkout master
Then run the merge command:
git merge test
After running these commands, the merge should be successful.
Run git log and you will notice that the master also has 3 commits.
The Remote Git Repository:
Until now, we have been working in the local repository. Each developer will work in their local repository but finally, they will push the code into a remote repository. Once the code is in the remote repository, other developers can see and modify that code. Below picture demonstrates working of remote repository.
Copyright © 2022 Intellisense Technology All Rights Reserved
Transforming business Since 2006
Fully Digital Service