What is Git? Git Basic Concepts, Installation, Commands With Examples & Demo Video | Step-By-Step
Fakhar ul Hassan
Cloud Infrastructure | Azure, Azure AD/Entra ID, AWS, DevOps, Automation, Infrastructure as Code (IaC), GitHub, GitLab CICD, Ansible, Terraform, Python, PowerShell, Bash
Agenda
- 1.1 Introduction to Git & Version Control System
- 1.2 Git Installation
- 1.3 Basic Git Commands With Examples
- 1.4 Demo Video
1.1 Introduction to Git & Version Control System
Git is a free and most widely used version control system.The software developers used to interact with Git almost on daily basis. Version control system are used to track the changes in code / files etc. In case, at any stage during software development life cycle, we want to revert back to any previous state of changes in the code then these version control system maintains its track and help us to do that.
Consider, If we have some project related files /code at a particular location / folder in our system. Then, at first we shall initialize a git repository for that project. Then during our project related working, any changes in the file / code will be captured by the Git. If we are satisfied with the changes (i.e. creation. deletion or updating of files etc.) then using Git commands we can make these changes permanent by committing it.
Another very useful advantage of Git is 'branching'. Consider, If many developers are working on a single project, where one group of developers is working on a specific set of product features while another group of developers is working on some different set of features of the same product. In such case, to maintain the integrity of the code and to avoid conflicts, separate copies of the code will be made available to both of the developers groups. These separate copies of the project code / files can be considered as branches. So, in Git, separate branches will be created either based on the product features or based on some other project scope of work. Once the new changes are finalized in these separate branches then both of the branches can be merged together to be released for the next SDLC (software development life cycle) stage.
However, If anything goes wrong in the aforementioned software release then the system can be reverted back to any previous committed stable state, which is maintained in the Git repository.
Now, in the next section, we shall explain how to install Git for Windows.
1.2 Git Installation
- Open the following URL to download the 'Git' installer for windows:
URL: https://git-scm/download/win
- Click the appropriate the download link for windows operating system (32 Bit or 64 Bit). Here, I have downloaded the 64 Bit installer based on my system specifications. [See image below]
Run the downloaded installer setup with administrative rights and click 'Next'. [See image below]:
Review the installation path and then click 'Next'. [See image below]:
On the next screen , you may select the components that you want to install or you may proceed with default settings.
Click 'Next' to proceed. [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Next' to proceed ( if you don't want some advanced customization) . [See image below]:
Click 'Install' to proceed ( if you don't want some advanced customization) . [See image below]:
The installation process will take some to complete. [See image below]
Click 'Finish'. [See image below]:
In the Git command line, type 'git --version', as shown in the below image:
Note: If you can see the git version correctly, then the 'Git' has been installed successfully in your system.
In the next section, we shall explore some basic commands of Git with step-by-step examples.
1.3 Basic Git Commands with Examples
First we shall create a 'Demo-Project' folder and place couple of project files in it, which will be tracked and managed by 'Git' later. For this, we shall run following commands:
$ mkdir Demo-Project
$ cd Demo-Project
$ touch index.html
$ touch myapplication.js
Following is the content of the 'index.html' file. However, you may use different file content, as you want:
===========Code Snippet Starts===================
<html>
<head>
<title>My Application</title>
</head>
<body>Hi Fakhar! This is a simple HTML page!</body>
</html>
===========Code Snippet Ends===================
[See below image for reference]
Following is the content of the 'myapplication.js' file. However, you may use different file content, as you want:
===========Code Snippet Starts===================
console.log('Hi Fakhar! Greetings');
===========Code Snippet Ends===================
[See below image for reference]
1.3.1 Initialize the Git Project
To initialize the git project, go to the folder where the project files placed and run the following command:
$ git init
[See image below]
To add the global user configuration (i.e. user name and user email), run the following commands (with appropriate information relevant to your account) ;
$ git config --global user.name 'Fakhar ul Hassan'
$ git config --global user.email '[email protected]'
1.3.2 Git Status & Add Files to the Local Git Repository
Now, you can check the status of local git repository by running following command;
$ git status
You can see the files in red color, which are not being tracked as we have not added any of the files to the local git repository. [See image below]
To add these untracked files to the local git repository, run the following command:
$ git add .
Now, all files are added to the staging work environment and ready to be committed in the master branch. [See image below]
1.3.3 Git Commit
Now, you can commit the changes and all the files in the staging work environment will become part of production work environment, which means that all the changes will be merged to the main master branch, by running the following command:
$ git commit -m "Adding the Demo-Project files to the master branch for the first time!"
[See image below]
You can check / verify the same using following command:
$ git status
[See image below]
1.3.4 Git Branches
To see all the branches present in current local repository, run the following command:
$ git branch
You will see all the local repository branches. [See below image]
To create a new branch from the master branch run the following command. [The same can bee seen in the above image]:
$ git checkout -b br_feature01
The 'br_feature01' is the new branch name, which has exactly the same files as master branch currently possess. [See image below]
1.3.5 Git Merge Branch
Now we shall make some changes in the myapplication.js file in the new 'br_feature01' branch by running the following command (Please ignore the syntax of console message instruction) :
$ echo console.log\('Hi Fakhar! Greetings'\)\; >> myapplication.js
Now, add the changed /updated file 'myapplication.js' to the staging work environment of 'br_feature01' branch using following command:
$ git add myapplication.js
Now, all files are added to the staging work environment and ready to be committed in 'br_feature01' branch production work environment. [See above image]
Now, commit these changes to the production of 'br_feature01' branch. [See image below]
The changes made in the 'myapplication.js' file are only present in the 'br_feature01' branch and these changes are not there in the master branch. To merge those changes from 'br_feature01' branch to the master branch, first switch to the master branch by running the following command:
$ git checkout master
Then, merge the 'br_feature01' branch to the master branch by running following command:
$ git merge br_feature01
Now, both branches (i.e. master and br_feature01 branches) have the same files. [See image below]
Git Commands Demonstration Video
(Optional - Only for better understanding)
For better understanding, watch below video for practical demonstration of above scenario:
Assistant Manager - Database Delivery (PMP, RDBMS and NoSQL Admin)
3 年Good one