Git and Version Control.
Version Control System
When we work in collaboration, there are always changes in the code made by multiple developers. Also, projects in companies have a large code that could be more than thousands of lines of code. For the maintainer of the project, It is very difficult to manage the code and keep track of changes in the files. Hence, in order to manage and track changes in a file, we use Version Control System (VCS).
A Version Control System is software that helps to track changes in a file or directory. It stores the history of all the changes or versions that have been created in a project. It can also roll back to any of the previous savepoints of a file. Git is the most famous and widely used version control system by developers around the globe. Some of the other Version Control Systems are Gitlab, Apache Subversion (SVN), and Monotone.
This blog is divided into two parts: In the first part, we will discuss about the following topics of Git:
- Introduction to Git.
- Installation of Git.
- Basic Git commands.
Introduction to Git.
Git is a widely used version control system used by developers. Git is an open-source distributed software. Anyone can use Git for free and can also contribute to its source code. It is usually used to collaborate with other developers and to manage the source code.
Let’s get familiar with the terms used in Git.
- Working Directory: Working directory is the folder in which the files related to your project are present.
- Staging Area: Staging Area can be thought of as a place, think it like a -buffer where only those files are stored that you want to track. Those files which are not included in the staging area will not be tracked or added to the local repository. The staging area helps to differentiate between which files to track and which files to not.
- Local Repository: A local repository can be thought of as a buffer where all the tracked files are stored with the latest changes.
- Remote Repository: Remote repository is same as a local repository but the difference is that it is hosted on a server remotely, for making remote repositories we use GitHub.
Alright! Let’s install git and learn git commands through practical implementation.
Steps to Install Git on your machine
- Step1: Go to git-scm.com
- Step2: In downloads select your operating system.
- Step3: Download a 32-bit or 64-bit setup as per your computer.
- Step4: Once the file is downloaded, install it in the system by following the next and finish prompts in the setup wizard for installation.
- Step5: Run the below command one by one in the terminal.
- Check the git version:
git --version
- Add your username in git:
git config --global user.name <your name in quotes>
ex: git config — global user.name “Groot” - Add your email in git:
git config --global user.email <your name in quotes>
ex: git config — global user.email “Groot@123gmail.com”
Basic Git Commands.
git init - In order to initialize a directory with git we use the following command in the command prompt. Make sure that you are in the right directory and then write the below command.
git init
git add - This command is used to add one or more files to the staging area.
git add <filename>
you can add more than one file at a time using the same command as follows.
git add filename1 filename2
If you want to add all the files present in the directory to the staging area, then use this command with --all
, as shown below
git add --all
git status - This command is used to see which files are included in the staging area and which files are not in the staging area. File names in the green color text are inside the staging area while those in red are not.
git commit - This command is used to commit all the files present in the staging area to the local repository with all the currently staged changes. Committing the files captures a snapshot of the files. It acts as a savepoint.
In the below image git commit
is the command, -m
is the attribute which accepts a message, and ‘First Commit’
is the commit message that you pass to that attribute. A commit Message is a small description of what we have committed.
git log - This command returns a record of all the commits made to a project along with the date, time, author name, and commit message.
To learn other important commands and concepts of git we should create a remote repository for our project and we have to learn about GitHub.
So, in the next part of this blog, I will walk you through the process of creating a remote repository on GitHub, moving a local repository to a remote repository, other basic concepts, and a lot more!
Closure:
Hope you find this article helpful. Thank you for spending your time on this blog. Follow for more such articles.
My name is Alfiya Siddique, I am a 2nd-year diploma student studying Computer Science. You can follow me on Linkedin and Twitter.