Today we are going to talk about the most popular version control system(Git) used among developers.
From the start to the end of the project lifecycle, we write thousands of lines in our code, and sometimes by mistake, we screw things up while writing code and it becomes hard to undo it. Git is a life savior for these disasters. So let’s understand what is Git and How we can use it?
What is Git?
Git is a distributed version control and source code management system. It was created by Linus Torvalds in 2005. It tracks the changes made in the files by creating a record of each version which can be reverted back in case of any mishappening. It also allows multiple users to collaborate in a project and merge changes into one source.
Firstly, you need to install Git on your system. If not installed then, click here to download the latest version of Git for your operating system.
Now you can either create a local or remote git repository. In the local git repository, all git revisions are stored on local storage space, while in the remote repository in addition to the local repository you have a backup on the servers of remote git providers. You can use any web-based Git service to create your remote repository like Github, bitbucket, GitLab.
Creating a Git repository
If you want to create a local repository then you need to follow below steps:
1. Create a folder for your project and open the command prompt or terminal in the project directory.
2. Now initialize the repository using the below command.
git init .
A new hidden directory with .git will be created under your project path.
In order to create a remote repository, firstly create your account on any of the web-based Git platforms. I will guide you through the process of Github.com. Login to your Github account.
Click on the ‘+’ icon and select New repository
Enter the repository name and description and check the ‘Add a Readme File option’, then click on the Create repository button.
A new repository with a Readme file will be created. You can clone the remote repository into your local storage by using the below command:
git clone https://github.com/the7topics/gitdemo.git
Once your Git code is downloaded in local storage you can start adding a file or making changes to the files. Add some files in the project directory and follow the next steps to commit the changes.
Adding and uploading changes to Git
Once you are done with creating new files or modification of the files, we need to add and commit the changes in order to help git officially track the changes.
In order to add changes to Git, you need to add the modified file to git by using the below command
git add .
The above command will include all file changes into git repo. If you want to ignore some file from getting added to the git repo then you need to create a file name .gitignore and mention all files to be excluded from tracking.
data.sql temp/ node_modules/
If you want to add specific files, then you can use the below command.
git add filename
Once the file is added to the git repo we need to commit the changes using the below command
git commit -m "Initial commit"
Here -m flag is used to provide remarks/notes for the changes made in the current revision.
For uploading changes to the remote repository, use the below command
Note: Anytime in between while making changes you can verify the files modified by using the git status command.
To get the list of all the commits, use the Git log command:
git log commit c64be4bfc88dff569a1d52e053f32286942d0a38 (HEAD -> master) Author: Rahul Ranjan <email@example.com> Date: Mon Nov 23 13:06:40 2020 +0530 second change commit b1599929981ad8ae47187e96d354c5f3d949d8aa Author: Rahul Ranjan <firstname.lastname@example.org> Date: Mon Nov 23 13:01:23 2020 +0530 first change
Now we will take a look at branching. While working on your project you can either use the main branch(master branch in Git terminology) or can create a separate development branch, along with the entire team. It helps to keep all development or in-progress changes separate from the main branch and once you are done with your changes, you can merge changes into the main branch for production.
You can check your current branch using the below command:
You can create a new branch using the below command:
git checkout -b development
After making and adding changes to the development branch you can merge the changes from the development branch into the master branch using the below commands:
git checkout master git merge development
In the above article, we learned about Git and Github. Try to create a Git repository by yourself to understand it practically. To learn more on related articles just explore our website and to get the latest news and updates follow me on Twitter & Facebook, subscribe to my newsletter. If you have any feedback please let us know by using the comment form.