Essential Git Commands Every Developer Should Know

Essential Git Commands Every Developer Should Know

Note: This article is not a quick guide, nor is it meant to be grasped in a single sitting. Instead, think of it as a map leading you through the expansive landscape of version control and collaborative development. Bookmark this page, and consider reading just a paragraph or two each day as you embark on your learning journey. This is not about memorization; it's about gradual familiarity. By incorporating these commands into your daily practices, you'll find that over time, they become second nature – tools at your disposal to navigate the vast realm of software development.

Git is a powerful and widely used version control system that allows developers to track changes, collaborate on projects, and manage codebase efficiently. Whether you're a beginner or an experienced developer, mastering the essential Git commands is crucial for effective version control and collaboration. But there is so much confusion in the world of Git and GitHub or we can say the version control system (VCS). Let's clarify some concepts first and then we will dive deep into Git commands.

First, Let's clarify that Git and GitHub are two different things. Although they are related to each other but not the same.

What is Git? Git is a distributed version control system that enables efficient tracking of code changes and collaborative development.

It simply lets us keep track of the changes that we make in our code and share them with other developers in our team. For example yesterday night you were making a game. you were halfway through it and everything was working fine as per the requirements. But today morning you did something wrong which lead you to crash the whole codebase and you are trying to solve the problem for the last 2 hours without even taking your breakfast but nothing working. Right now you would be more than happy if you can get the code in the state that it was yesterday night, aren't you? There comes the concept of the version control system. What if you can save a screenshot of your codebase every time you want and later on if something goes wrong you can just get on in a time machine and go back to the last time when the code was working fine? I always think of it like in a game where there are multiple checkpoints in each stage and if you die you can restart from the last checkpoint.

Now what if one day your computer is dead without any reason or some bad guy stole your machine? Do you lose all of the code that you have written in the last month without even taking a shower? Here comes the concept of keeping your code along with the version control system somewhere in the cloud(as they say. actually it is just another computer or server in some other part of the world).

What is GitHub? GitHub is a web-based platform for hosting, sharing, and collaborating on software development projects using Git.

Think of it just like keeping all of your code along with the screenshots of your code you took from time to time or the version control system(Git in our case) on the internet. So that no matter what happens to your personal computer you get the code just by logging in to the VCS(Version Control System) service provider (GitHub in our case). And the bonus part is as the code is on the internet, you can collaborate with other developers for the development. if there are 5 features that you would like to implement in your software you can ask 4 other developers to work for you. they will get the code from GitHub(Obviously you will authorize them) and work parallelly with you at the same time.

One thing we need to keep in mind is that Git is not the only software available for version control or SCM(source code management). There's Mercurial which is known for its simplicity and many more are there.

Also, GitHub is not the only service provider that provides the service to host codes with Git. there's GitLab. BitBucket and many more. Big companies like Google uses their own platform to host their codes.

But Git is the most popular among all version control systems and GitHub is the most popular to host Git Repositories(projects that use Git for VCS). That is the reason we are learning the most important command of Git to rule the world of programming.

Now follow me and run the commands on your own to get the best output. Create a folder in your desired directory (in the Documents directory for me). To do so from the file explorer go to the Documents folder and create a new folder with the name "myNewRepository"

If you are in the Linux terminal write the following command to do the same thing.

cd ~/Documents
mkdir myNewRepository
cd myNewRepository

Now create two files in the folder just created by you with the name "" and "".

Inside the first file type

print("hello world!")

and save.

open the second file and type

print("bye world!")

and save.

now first install let's install git in our computer.

Windows users open Powershell and type the following command to install git on their computers. if it shows any prompt then choose yes.

winget install -e --id Git.Git

If you face any problems while installing on Windows go to and download the executable file and install.

And Linux users open the terminal and type the following command:

sudo apt-get install git

if it asks for a password type your system password that you use to log in when starting your computer.

Now close the terminal and reopen it. and navigate to myNewRepository using cd command. if you don't know how to do that read the following article first.

Essential Linux Commands: Your Guide to Command-Line Basics

1. git clone

When collaborating on a project, you often need to start working with an existing repository. The git clone command allows you to copy a remote repository onto your local machine. Here remote means the project/codebase is somewhere on the internet(Ex: in GitHub). to get the code in our computer (or to clone the code in our local computer) you can use this clone command.

Keep in mind that this will clone the project/repo(short for repository) in the folder where currently you are in. so make sure to change to the directory/folder before cloning.

git clone <repository_url>

For example, if you want to clone the repository from this ( URL. type the following command in your terminal/cmd/PowerShell

git clone

2. git init

To start using Git in a project, you need to initialize a Git repository. The git init command is used to create a new Git repository in a directory. It sets up the necessary internal Git files and structures, making the directory ready for version control. You do it only once in the lifetime of a project. One of the important things while writing Git commands is that, in the terminal, you need to be inside the folder. In our case, we need to be in the myNewRepository and then type the following command. to check if you are in the right folder type pwd and hit enter, if the last word of the output is the folder you are working with(myNewRepository) then you are fine.

git init

3. git add

Before committing changes, you need to stage them. Now what does this "commit" and "stage" mean? commit means taking a screenshot of your code at the current time. A commit keeps track of the changes made to the codebase since the last commit. It includes information about what specific lines were added in a file, modified, or deleted, along with metadata(information about the data) such as the author's name, email, date, and a commit message explaining the purpose of the changes. And "Staging" refers to the process of preparing changes in the code of your working directory to be included in the next commit.

The git add command adds changes from your working directory to the staging area, preparing them for the next commit.

git add <file(s)>

Here, after the add command if you want to only add one or two files you need to write them implicitly. for example, there are two files named and I have modified and I want to add them. The command would be

git add

but what if there are 10 files I have modified and want to commit? Do I need to write all of their names? In that case, we can use the (.)period instead of writing the names. the command would look like

git add .

4. git commit

Once your changes are staged (after the git add command), you can create a commit. Commits are snapshots of your code at a specific point in time. git commit command will commit(take a screenshot of) all the codes that we have added to the staging area with the git add command. the following command will commit all the 10 files that we have added previously. so if you want to commit one or two specific files then don't use (.) in the git add command instead write the name of the specific file after git add.

git commit -m "Your commit message"

5. git status

To see the status of your working directory and staged changes, you use the git status command. It will show which files are edited and not committed, which files are committed but not pushed(uploaded) in the remote repository(in the GitHub), and so on.

git status

Okay so now that you have committed the changes to your code you need to upload them in GitHub. How can you do so?

For that, first, you need to create a repository/folder in GitHub.

If you do not have a GitHub account create one and then click on the + icon beside the search bar.

Select New repository

after that, you need to write the name of your repository. Try to keep the name the same as the one on your computer.

Keep it public if you want to share it with everyone visiting your profile or private if only you want to use it. for now, let's keep it public as there is nothing secret in our and

Then click Create Repository.

Now you will be shown the following commands. here we can go for the second part where it says to push an existing repository from the command line.

type the given commands one by one.

6. git remote

Remote repositories are copies of your project hosted on other servers. The git remote command helps you manage connections to remote repositories.

git remote add <remote_name> <repository_url>
git remote -v                     # List remote repositories

For our case, git remote add origin, and then the git link of the remote repo.

git remote add origin

Here git remote add origin means we want to connect a remote repository (A repository on the internet) with the repository in our local machine. the link after the origin is the link to the remote repository. And origin will be the name of the remote link the next time.


 git branch -M main

Here we are going to set main as the main branch of our project. Now what is this "branch" thing? we will get to this in a moment. now just keep doing what it is saying.

Now the next command is

git push -u origin main

Here we are pushing(uploading our codes to the remote repository in GitHub). For making things simple, I will suggest you install GitHub CLI from before running this command.

after installing GitHub CLI. open a terminal/PowerShell and type

gh auth login

here you will be given some options. navigate using the up arrow and down arrow and select using enter button. select according to the following.



  • type y and enter

  • Login with a web browser

Now you will be given a code. you can write it on a piece of paper and then click enter. a browser window will open. if you are not logged in to GitHub then log in first. Now write the code you just copied and continue. You will be asked to authorize GitHub. click on the green button that says Authorize GitHub.

Boom. you are all set now. Go back to the PowerShell window, navigate to myNewRepository, and type:

git push -u origin main

You have successfully uploaded your code to you can check that by going to and then your profile.

From now on if you are working in the same directory then each time you want to commit your code and upload on GitHub just type these three commands.

git add .
git commit -m "commit messsage"
git push

Now that is enough for you to begin with Git/GitHub. You can upload a copy of your codes on GitHub. But as a developer, you need to collaborate with other developers, and also in case you messed up with your code you need to go back to a previous commit. For that, you need to master many more commands. Let's do that.

7. git branch

Branches allow you to work on different features or fixes concurrently. The git branch command helps you manage branches in your repository.

In a git repository, we can create multiple branches (subfolders of the same code). for example when more than one developer works in the same repository, to implement different features they create one branch each to keep the code safe. let's say we are writing code in 10 different files in one repository for the last 2 months. today we decided that from now on, 10 different programmers will work on those 10 different files and collaborate with each other. so every programmer will create their own branch inside the repository. Let's make this clearer.

Here in the image, we can see the project starts with one branch which is the main branch. Generally in big projects we don't commit directly to the main branch. For fixing any bug or implementing a new feature we create a new branch and modify the code there. Guess that there's a bug(problem) in our code and a developer named Juned is assigned to solve that. What juned will do is he will create a branch with the name A.

git branch                        # List all branches. in our case it will only output main
git branch A                      # Create a new branch named A
git checkout A                    # Now current branch is A. and all the work he do will be on this branch only without changing anyting on the main branch

This branch will contain all code until the time when the branch is created. Juned will work on the branch with solving the bug and nothing else, And no one else will work on the bug and the file that juned is working on.

8. git checkout

The git checkout command is used to switch between different branches. It's also used to revert changes in your working directory to the state of a specific commit. In Juned's case after he created branch A he switched from branch main to branch A with the command 'git checkout A'.

git checkout <branch_name>        # Switch to a different branch
git checkout <commit_hash>        # Revert to a specific commit

git checkout is also used to go back to a specific commit. In our game example, we talked about earlier, we can get back to the code at the state of the previous night with the hashcode of the commit. now what this hashcode is? every commit in git has its unique hashcode and we can get back to the state of our codebase to this hashcode using git checkout.

9. git log

The git log command displays a chronological list of commits in your repository. It's useful for reviewing the commit history.

git log

We can use Git log to see all the previous commits

or else we can go to the remote repository and click on the commit count(4 commits here) and we will be taken to the commits section.

we can see here there are 4 commits each one with its hashcode on the right side. Let's assume that we want our code to be the same as it was in the second commit with the hashcode 2dda732.

we will write the command as

git checkout 2dd37a2

Now we can look at our code as it was at that time. but if we want it to be like this forever then what we can do is:

10. git reset

The git reset command is used to unstage changes from the staging area or move the current branch to a specific commit.

git reset <file(s)>               # Unstage changes
git reset --hard <commit_hash>    # Move branch to a specific commit

We need to reset our head to the commit hashcode. so we will type the command

git reset --hard 2dd37a2

But we need to be careful as this will delete all the code that was added after that specific commit.

11. git push

Now let's get back to Juned fixing the bug. After he fixes the bug he will push this A branch into the remote repository. the command will be as follows:

git push origin A

Here juned is pushing the code in the A branch of origin(name of the remote repository).

NB: We assume that this is a team project and all of them are working on the same remote repository. For open-source contributions, there will be another post. You can follow this blog to get that whenever it publishes.

Now forget about juned and the team. Normally when you're ready to share your local commits with others, the git push command is used to upload your changes to a remote repository.

git push <remote> <branch>

You don't need to type the remote(origin in our case) and branch(main/A in our case) every time you push. you can just use git push. But if you have multiple remote URLs or multiple branches then you need to explicitly tell the code to be pushed in which remote URL and also if your repository has multiple branches then you need to tell in which branch of your remote repository you want to push.

for example, if I want to connect myNewRepository to another remote repository named test that I have created in my GitHub account. I will do the following

git remote add link2

So now my local repository myNewRepository is connected with two remote repositories origin(for and link2(for now if I want to upload the codes to the test repository I need to do the following.

git push link2 main

Here I am saying to my terminal that I want to upload my codes to the main branch in the link2 repository which I set earlier to the Test repository.

12. git merge

Merging combines changes from one branch into another. It's often used to integrate feature branches into the main branch.

git checkout <target_branch>
git merge <source_branch>

As Juned edited everything in the A branch, if he wants to merge the A branch with the main branch, he will write the command as:

git checkout A
git merge main

This will merge the A branch into the main branch. Now as we don't need A branch anymore we can safely delete it using the following command:

git branch -d A                        # this will delete the A branch

13. git fetch

The git fetch command retrieves updates from a remote repository without automatically merging them. This is useful for reviewing changes before merging.

git fetch <remote>

If any change is there in our remote repository we can fetch the changes without merging them with the following command.

git fetch

14. git pull

To fetch and merge changes from a remote repository into your local branch, you use the git pull command. This is important to keep your local repository up-to-date with the latest changes. git pull combines both git fetch and git merge command.

git pull <remote> <branch>

Here remote and branch are not important if you are working with only one remote URL and one branch. but if you are working with multiple remote URLs and branches then you need to explicitly tell which branch and remote link to pull from. For our case we can do as follows:

git pull origin main

15. git stash

When you're in the middle of something and need to switch branches, but don't want to commit your changes, you can use the git stash command to temporarily save your changes.

git stash                         # Save changes
git stash apply                   # Apply saved changes

These essential Git commands lay a strong foundation for efficient version control and collaborative development. As you continue your journey in software development, you'll likely discover additional Git commands and advanced techniques that further enhance your productivity and collaboration skills.

Remember, Git is a powerful tool that can greatly streamline your development workflow. Take the time to practice and understand these commands, and you'll be well on your way to becoming a Git guru. Happy coding!

Mastering Git and GitHub is a journey that spans not just weeks, but often months to years. The intricate web of commands, workflows, and collaboration techniques can seem overwhelming at times, but it's important to remember that the path to expertise is built through consistent effort, practice, and persistence.

Throughout your learning journey, it's crucial to maintain a positive attitude and a patient mindset. Mistakes are an integral part of the learning process, and each misstep is an opportunity for growth. Don't let frustration deter you; rather, embrace it as a sign of progress. The beauty of version control and collaboration tools like Git and GitHub lies in their ability to accommodate experimentation and learning.

If you find yourself stuck or encountering obstacles that seem insurmountable, remember that it's perfectly acceptable to take a step back. Starting over with a fresh perspective can often yield better results than endlessly struggling with a particular issue. Embrace the flexibility that Git provides – just as you can reset a branch to an earlier state, you can reset your learning path and embark on it anew.

So, whether you're a solo coder striving for mastery or a team member contributing to a larger project, keep your determination alive. The road to proficiency might be longer than you initially imagined, but with every commit, push, and merge, you're inching closer to unlocking the full potential of Git and GitHub. Embrace the process, stay curious, and remember that, in the world of software development, the journey itself is a rewarding destination.