Introduction

Git is free and one of the most widely used version control systems that were developed by Linus Torvalds in the year 2005. Git can extensively handle changes in small to very large projects. Git smartly monitors the changes in our project (whether it is small or large) and helps us to keep track of these changes made in the source code of our project. Git saves the various versions of our project into a special folder named Git repository. The Git repository consists of files having different versions of the project. Some of the features of Git which make it one of the most widely used version control tools are:

  • Git is free software.
  • Git is used to track the changes made in the source code.
  • Multiple users can work together using Git.
  • We can create multiple branches in Git which makes development easier.
  • Git creates a backup of our project so that we can get the source code in case of data loss.

Let us suppose that a team is working on a project (for example a game development). Since a team is working then everyone should have an exact copy of the actual source code. Now, whenever a team member makes any kind of change in the project then he/she must update the source code for every other team individual. So, this is a hectic task to be carried out each time.  Now the solution to this project development problem (and many other such problems like team management, code management, code sharing, code updation, etc.) has one solution i.e. version control system, particularly Git.

Essential Git Commands Every Developer Should Know

As we have discussed that Git is a version control system that smartly monitors the changes in our project and helps us to keep track of these changes made in the source code of our project. So, now let us learn the essential commands of Git.

1. git config

One of the most basic and primary steps of using any application is to configure it. So, to configure Git, we use the “git config” command. We need to specify our user name and our email address along with the “git config” command so that both can be used in our future git commits (we will be learning about “git commit” later in this section).

Command:

git config --global user.name "<user-name>"

git config --global user.email "<email-address>"

Note: In the above command, “—global” is known as a flag.

2. git init

For Git to track a certain directory or folder (having our project), we first need to initialize the git repository. By the term initialization, we mean that the git repository must be created as this repository will contain the files that will ultimately have eth record of versions of our project.

Command:

git init

When we run the above command, all of the necessary files and directories needed for Git to keep track of everything are created. All of these necessary files are kept inside a hidden directory namely “.git”.

Note: “init” command is an abbreviated form of initialization.

3. git status

As we know, status is a state of someone's mind. Similarly, in the git world, the state of our project or the status of our project can be seen “git status” command.

Command

git status

When a new file is added, the git status command shows us the new file in green color and recommends us to add this file so that git can track this file as well.

This command also shows the file(s) in red, which means there were some changes made to this file and git has not been updated with the changes. So, to update the changes, we use another command git add which we will discuss next.

4. git add

Before learning about the “git add” command, we should be familiar with the concept of the staging area.

The staging area is a kind of draft space where all the versions of a file or directory that we want to commit are saved. By committing a file present in the staging area, we are saving the snapshot of that state of the file.

Refer to the image for better visualization.

https://www.earthdatascience.org/images/earth-analytics/git-version-control/git-checkout.png

[Image courtesy: kaplan-consultants.org].

Command

git add <filename>

or

git add .

We can add multiple files by adding them one after the other separated by space. We can also all the entire uncommitted files using “add .” which means adding all the files to the staging area.

5. git commit

As we have discussed above, a commit is a snapshot of the current version of the file(s). The “git commit” command saves a log message as well as a unique commit id of the current modifications made to the git repository.

When we use the ”git commit” command, the changes are saved in our local system along with a short message related to the commit is also stored which is provided by the user along with the command.

Command

git commit –m "our commit message"

6. git push

If we want to move the content of the local repository (git) to a central repository like GitHub, we use the “git push” command.

Command

git push

Note: For using the “git push” command, we first need to add the remote repository.

7. git clone

Cloning means copying a certain thing. the “git clone” command is used to clone a certain remote repository (using the URL of the repository).

The command simply creates a copy of the remote repository to our local working directory.

Command

git clone <https://github.com/><URL_of_remote_repository>

8. git pull

If we want to fetch certain file(s) or any changes in the remote directory, we can use the “git pull” command. This command pulls the latest changes from the remote repository to the local working repository.

Command

git pull

Note:  It ensures that the latest or most recent are on our local system so that our project has up-to-date information from our teammates.

9. git branch

The “git branch” command enables us to perform parallel development. The command can create, rename, list, and delete branches.

Command for creating a new branch

git branch "new branch name"

Command for listing all the branches

git branch

or

git branch --list

10. git checkout

The “git checkout” command helps us to switch between various branches. It also lets us create and switch to a new branch. Now, for switching between the branches, the branches must be present in our local system.

Command

git checkout "branch name"

Note: The ”git checkout” command can also be used to check out the files of our current working repository.

Now, we can combine the “git branch” and “git checkout” commands to create and switch into a new branch using only one command:

git checkout -b "branch name"

Here, -b is a flag that resembles a branch.

11. git log

As we know log is a list containing certain details. Similarly, a git log is a list of all the commits of the repository. The “git log” command can be used to list and add the commits of the current working branch.

Command

git log

What is GitHub?

GitHub is a cloud-based central repository that can be used to host our code for team collaboration. It is a hosting service that is used to manage the git repository in the central server. GitHub is a free (for certain limits) and easy-to-use platform that enables teammates to work together on projects.

Some of the most commonly used GitHub commands are:

  • git commit
  • git pull
  • git push
  • git fetch
  • git merge, etc.

Microsoft Corporation owns GitHub.

Conclusion

Git is free and one of the most widely used version control systems that were developed by Linus Torvalds in the year 2005. Git saves the various versions of our project into a special folder named Git repository. The Git repository consists of files having different versions of the project. It smartly monitors the changes in our project and helps us to keep track of these changes made in the source code of our project. Git creates a backup of our project so that we can get the source code in case of data loss. GitHub is a cloud-based central repository that can be used to host our code (our git repository) for team collaboration.