Git is very important and powerful tool when it comes to the software development life cycle. Now in this article, we are going to see why git is used vastly in the software industry and what kinds of operations we can perform with git. So git is basically a distributed version control system for tracking changes in source code during software development. Git is designed for coordinating work among developers, but it can be used to track changes in any set of files. Its goals include speed, data integrity, and support for distributed, non-linear workflows.
Git uses a version control system in which at some instances in a project a snapshot of the project is taken that is the progress made by that project is stored on a central repository. In simple words suppose the user is developing a web page first it will add its name and stored it as version then it added basic info and stored it as the second version then it added its profile picture and stored it as the third version now suppose the user added a wrong profile picture then there is always an option to revert back to the previous version after that it can add the correct profile picture.
Advantages of Version Control System
- Storing Versions: As explained in the above example If we encounter some error in our project code we always have the option to revert back to the previous version with the help of git it is easy to store version. If we try to store them manually It will be a hard task.
- Collaboration: In the software industry, multiple employees are working on the same project so with the help of version control system the main copy is stored on central remote server fetched by employees and after changes which are to be made are made employee uploads its own version to the central repository so others can fetch code updated by employees.
- Backup: In case if a central repository is crashed then anyone can push its local copy to the central server. As while making changes it has to be made on the local repository.
Git can be used with GUI as well as command-line. In this article, we are going to use the command line.
GIT can be downloaded from here. After the successful installation of GIT, there is a need to configure git using the following commands:
- Open terminal:
To check version of git
- To set your username
git config --global user.name "FIRST_NAME LAST_NAME"
- To set your email
git config --global user.email "MY_NAME@example.com"
Working with GIT commandline
Initializing a local repository:
This command will initialise our local repository.
Now our repository is initialized we can add some code to our project
Checking status of the repository:
As you can see a .git file is created as mentioned above. Now to check the status of these files, the following command is used:
Here, working area is where files that are not added to repository yet are stored. These files are called as ‘untracked files’. Staging area is files that are going to be a part of the next commit. Commit means basically storing a version of project at the current instance. As you add new files in your workspace you can add this files to staging area using git add command.
Adding files to the repository:
Now in the previous step we have seen that some files are untracked, with the help of git add filename we add these files to the staging area. git add command is used to add all files in the folder to the staging area
After adding files to the staging area if git status is called again, then it will show files to be committed.
Now files are ready to be committed they are ready to be added to the local repository
git commit -m "your message"
git commit command allows you to add your files to your local repository -m specifies that you have to pass commit message while committing a code
Every time a commit is made a SHA-256 key is created and assigned to that commit If we wanted to retrieve code at that commit it is possible using this id.
Accessing log of commits:
After you have created several commits, or if you have cloned a repository with an existing commit history, you’ll probably want to look back to see what has happened. That’s where git log comes in picture. This command allows us to see all commits made by user. With the help of commit id you can refer back to the previous version.
git clone "Remote_repo_url"
With the help of git clone command, you can clone various repositories that are present on websites like GitHub, GitLab, bitbucket, etc.
Any repository can be cloned as follows:
git clone "https://github.com/sanketpathak64/Kickstarter-Campaign.git"
Parallel development commands
git branch branch_name
This command allows to create a branch for the project. A branch is like exact copy of the project.
git checkout branch_name
This command allows to switch from one branch to another.
git merge branch_name
This command allows to merge a code of 2 branches in one branch.