Basic Git Commands

Photo of author

By Sameeksha Medewar

This digital era has brought huge traction to open-source distributions and the popularity of the same is increasing among students exponentially. They are an efficient way of learning and creating improved and updated ways for managing projects and they are free.

One of the most popular distributed version control systems, namely Git, is widely used by a lot of people around the globe for the management of projects of all sizes.

To make certain important actions while using Git, one has to make the use of Git commands. For new players in the game, it might seem to be quite complex to develop an understanding of the huge number of git commands. This blog covers some of the most repetitive and basic Git commands every programmer should know.

However, their community has built GitLab on top of Git for implementing a considerable amount of Git operations directly in your browser. But still one will always need to use the basic Git commands via command line for achieving advanced tasks. This blog is a 101 guide for an individual to get started with Git commands successfully.

What is Git?

Git is a free and open-source distributed version control system that helps in handling version control of projects with lesser time and improved efficiency. It is based on a distributed development environment of software that enables multiple developers to have access to the source code for modifications and manipulations that are visible to other members as well.

It allows projects to have “versions” based on various changes made to the code over time. In addition, Git also allows users to roll back to previous versions to undo all of the changes implemented to the source code. With the help of Git, developers or a team of developers are able to perform the following tasks without any hassle:

  • Monitoring and keeping track of all the files and modules of a project.
  • Having a detailed record of all the modifications done to the project files.
  • Restoring any of the previous versions of the project whenever required.
  • Easily comparing the codes of 2 different versions of a project and running a deep analysis.
  • Merging the codes received from different computers and different team members.

Features of Git

1. Distributed system

Being one of the most popular distributed version control systems, Git allows individuals from all around the world to perform modifications on the given project.

It holds a central repository that is being accessed by various developers remotely. Therefore, it successfully allows multiple developers to work on the same project, that too at the same time without interrupting each other.

2. Non-linear Development

Due to its nonlinear development, Git enables developers to implement modifications and operations remotely. It also allows them to pick up a specific part of a project and work on it and further update the modifications wholly.

3. Branching

Branches are the copies of projects that run parallel to the original project. They are helpful in offering users to make changes in the project without affecting the original version.

4. Lightweight

All of the data is stored from the central repository to the local repository, and because a lot of people are indulged in one project, this data gets bigger and bigger. To reduce the overwhelming load, Git utilizes the process of lossless compression that reduces the size of data and saves them in the local repository too with minimal space requirement.

5. Speed

Since all of the data and information gets stored within the local repository via the process of cloning, users are able to access the data quickly rather than wasting time fetching it from a remote repository.

In addition, it offers better speed and scalability options as compared to various other version control systems available in the market. This results in the efficient management of large projects easily.

Understanding the GIT Workflow

Git is a crucial part of companies nowadays and is mainly used for allowing multiple programmers to work developing software collectively. Every Git project is built from the following 3 major sections:

1. Working Directory

It is the place in which users edit, add and delete files and it also contains the .git folder. Implementing changes in the check-out branch is possible because of the working directory. And if there are no changes made in the project, the working directory will be completely clean.

2. Staging Area

Understand the staging area as a rough draft space where all the files for the new versions are added for working on the next versions of the project.

3. Git Directory

A Git directory is a place for storing all of the required information, such as commits and remote repository addresses that are utilized by projects in version control. In addition, it also contains logs that store all of the edit history for restoring versions whenever required.

So, the workflow of Git starts from the Git directory that includes adding, deleting, and editing the files. Later, these modifications are pushed forward in the staging area that temporarily saves the files for the new versions. After finalizing all the changes, they will be saved directly into the git directory.

Git works efficiently with all of the popular operating systems, such as Linux, Windows, macOS, and Solaris. However, it is quite complex to learn and get started in the first place.

Requirements for Getting Started with the Common Git Commands

For using Git locally, it is not mandatory for an individual to have a GitLab account. However, it is suggested to get yourself registered before running Git commands because some files require a stable connection among files on the computer and their versions on remote servers.

Also, ensure to pre-install Git on your computer. For checking if it’s already installed on your system or not, run the following code:

git --version

If the system fails to show any result, it simply means that you must install Git. After that, run the aforementioned command again to verify whether it was correctly installed or not.

Configuring Git

Now to get started, you need to configure Git on your system by entering your credentials i.e. user name and email address that will be utilized for authorizing yourself for your work. The credentials should be similar to the ones entered in the GitLab.

For adding the user name, run the following command in the command prompt:

git config --global "your_username"

Use the following code for entering the email address:

git config --global ""

Now, check your configuration by running the following command:

git config --global --list

Adding the –global option enables Git to use your information for all the work an individual will perform on the project. However, if someone wants to use the configuration only for a specified repository, then the –local option would be used instead of –global.

Basic Git Commands

1. Creating a new local repository

Use the following command in order to create a new local repository on your system:

git init

2. Check out a repository

For making the clone or working copy of any local repository, use the following code:

git clone /path/to/repository

However, if you are up to remote servers, use:

git clone username@host:/path/to/repository

3. Add files

For adding single or multiple files for indexing:

git add <filename>
git add *

4. Push changes

Run the following command for pushing all the changes made directly to the master branch of your remote repository:

git push origin master

5. Check status

You can list and monitor all of the edited files including the ones that are still in the process using the following command:

git status

6. Connect to a remote repository

For connecting local repositories with remote servers, one need to first push to it with the help of the following command:

git remote add origin <server>

Now, run the below-mentioned command in your command line for listing all the currently configured remote repositories

git remote -v

7. Branches

For creating new branches, we can use:

git checkout -b <branchname>

For shifting from one branch to another, we have:

git checkout <branchname>

Listing all of the branches in repositories along with telling the current location of your branch:

git branch

For deleting the features of a branch:

git branch -d <branchname>

Pushing a branch to remote servers for allowing anyone to work on it:

git push origin <branchname>

For pushing all of the branches to remote repositories:

git push --all origin

Deleting a specific branch on remote repositories:

git push origin :<branchname>

8. Update from the remote repository

Pulling and merging changes of the remote servers with the working directories:

git pull

9. Undo local changes

To undo unwanted modifications, you can run the following command for replacing the current version with the previous versions:

git checkout -- <filename>

Also, you can opt to remove all the changes permanently by simply fetching the latest history and pointing the local master branch at it:

git fetch origin
git reset --hard origin/master

10. Fetching objects

With the help of git fetch, users are allowed to fetch objects from remote repositories and later add them to the local working directory:

git fetch origin

11. Viewing tree objects

For viewing tree objects along with other associated information, such as name, mode of each item, and the blob’s SHA-1 value, run:

git ls-tree HEAD

12. Viewing GUI

Simply run the below-mentioned code for viewing the graphical interface for a local repository:


13. Creating zip files

By using git archives, users will be able to create zip or tar files that are composed of constituents of a particular repository tree:

git archive --format=tar master

14. Identifying corrupt objects

To run a quick check-up for corrupted or malicious objects in the git file system, use the following command:

git fsck


Git is a widely-used and extremely reliable tool that enhances the development of software along with emphasizing allowing multiple developers to work at the same time.

Indeed, there are loads of commands used for achieving certain goals on Git, but for getting started and working without many hurdles, one should be well-versed with the basic Git commands mentioned in the blog. These commands will help you in:

  • Registering the username and email address on the local repository.
  • Create new local repositories.
  • Adding and removing files for a staging area.
  • Implement changes in the repositories and check the status of edited files.
  • Undo the unwanted or wrong changes performed.
  • Fetch and add your repositories to remote servers.
  • Creating zip and tar files containing the components of a specific repository tree.
  • Identifying corrupted objects of any git file system.

Getting hands-on experience with these commands will enable developers to be efficient with Git along with controlling the source code easily. However, it is quite possible that remembering all of them may get complex at the start, but using them regularly will help in mugging them up eventually.

People are also reading: 

Leave a Comment