As a developer, you know that Git is an essential tool for version control and collaboration. It allows you to manage your codebase efficiently and keep track of changes made to your project over time. However, with so many Git commands available, it can be overwhelming to know where to start. That's why we've put together this list of the top 10 Git commands that every developer should know. These commands will help you streamline your workflow, save time, and improve your productivity. So, whether you're new to Git or a seasoned pro, keep reading to discover the Git commands that will make your life as a developer much easier. Let's dive in!
What is Git?
Imagine you are working on a group project with your classmates, and you are all responsible for different parts of the code. You want to keep track of any changes made to the codebase over time, so you don't lose any important work or accidentally delete someone else's code.
This is where Git comes in. It's like a time machine that keeps track of every change made to your project, so you can go back to a previous version if something goes wrong. It also allows your group to work on the same codebase without accidentally overwriting each other's work.
Git stores all of these changes in a structured way, kind of like a tree, so you can see exactly what was changed, who made the changes, and when. It's an essential tool for any developer working on a project with multiple collaborators.
Why is Git important for developers
Git is important for developers because it provides a centralized location for version control, making it easier to keep track of changes made to the codebase. This is especially helpful when multiple developers are working on the same project, as it helps to prevent conflicts and avoid overwriting each other's work.
With Git, developers can quickly revert to previous versions of their code, making it easy to fix any bugs or errors that might have been introduced in the code. Git also helps developers to keep their codebase organized and easy to understand.
Furthermore, Git is a great tool for collaboration among developers. It enables them to work together on the same project while keeping track of who made what changes, when, and why. This makes it easier for developers to review and discuss code changes with each other, ultimately leading to a better end product.
Key terms in Git
Before we dive into the essential Git commands, it's important to familiarize yourself with some key terms used in Git:
- Repository: The repository is where all your project's files and data are stored.
- Commit: A commit is a snapshot of your project at a specific point in time.
- Branch: Branch is like a separate path of development for specific features or bug fixes.
- Merge: Merging is the process of combining changes made on different branches into a single codebase.
- Pull: Pull fetches changes from a remote repository and merges them with your local codebase.
- Push: Push is used to upload your local changes to a remote repository.
- Clone: Cloning creates a local copy of a remote repository, while forking creates a copy of a repository in your own account.
- Fork: Fork is used to create a copy of a repository in your own account.
- Pull request: A pull request is a request to merge changes from one branch into another branch.
- Merge conflict: A merge conflict occurs when Git is unable to automatically merge changes made to the same file by different developers.
Setting up Git on your system
To set up Git on your system, you can follow these general steps:
- Download and install Git for your operating system.

- Open the terminal or command prompt on your computer.
- Verify that Git is installed and configured correctly by running the command
git --version
.

Verify that Git installation with git --version
command
- Set your name and email in Git by running the following commands:
git config --global user.name "Your username"
git config --global user.email "[email protected]"
Once you have Git set up on your system, you can start using it for version control in your projects.
Cloning
It's frequently required to create a copy of an existing repository on your local workstation machine when using Git. Cloning is the method that enables you to modify the codebase while keeping the original version in the remote repository.
Using the Git clone command
To clone a Git repository, you'll need to use the git clone
command followed by the URL of the remote repository. For example:
git clone <https://repository-link>
Examples of cloning a remote repository
Suppose we desire to retrieve a project from Github; in that case, we simply need to select the green button labeled "clone or download," copy the URL, and then insert it after the "git clone" command mentioned earlier.

By doing this, a new directory containing a copy of the remote repository will be created on your local computer. After that, you can start editing the codebase by navigating to the directory.
Staging and Committing Changes
Staging and committing changes are important steps in version control systems such as Git, which allow you to track changes to your code over time and collaborate with others.
Here's an overview of how staging and committing work in Git:
Adding files to the staging area
Firstly we need to create a file, or modify the Local Repository. To create a file, us the touch
command as follow:
touch file-name.txt
For Example, I have created a file called example.txt

Git add files
Add the modified files to the staging area using the git add
command. This tells Git which changes you want to include in your next commit.
Either we can add single or multiple files at once in the staging area.
git add <File-Name>
Or, we add all the files from the repository, using the following command :
git add .
Consider the below Output:

Commit your changes
Once you're satisfied with your changes, use the git commit
command to create a new commit with a commit message that describes the changes you made. This permanently records your changes in the repository history.
But while commit your changes, it is required to include a message that should be a small description of the changes committed.
So, for that you can use the below code:
git commit -m "Your message"
Also, it's a good practice to commit often and with meaningful commit messages. This makes it easier to track changes and understand the evolution of the code over time.
The output should look like this:

Pushing Changes to a Remote Repository
After we are done with making changes to our code, staged them and have committed it to your local Git repository, the next step is to push those changes to a remote repository. Here's how you can do that:
git push -u origin <branch_name>
In our example, we haven’t yet created a Branch, so we will directly push the changes into the main
Branch.

Branching and Merging
Understanding the Git branching model
When working on a large and complex projects or when collaborating with multiple people in the same codespace, Git Branching comes into play which allows multiple parallel lines of development to occur within the same repository
Creating a new branch
To create a new branch in Git, you can use the git branch
command followed by the name of the new branch.
For example, to create a new branch named dev
you can use the following code:
git branch dev
Switching between branches
To switch to a different branch in Git, you can use the git checkout
command followed by the name of the branch you want to switch to.
For example, to switch to the dev
branch, you can run the command:
git checkout dev
This switches you to the dev
branch and updates your working directory to reflect the code in that branch.
Merging branches
To merge changes from one branch into another branch in Git, you can use the git merge
command.
For example, to merge changes from the dev
branch into the master
branch, you can first switch to the master
branch using
git checkout master
and, then run the command to merge the changes from the dev
branch:
git merge dev
This will merge the changes from the dev
branch into the master
branch.
Reverting Changes
Understanding the Git revert command
Reverting changes in Git is the process of undoing or removing a previous commit This can be helpful if you committed something incorrectly or for any other reason you need to undo modifications that were done in a prior commit.
Reverting changes in Git
To revert the changes we first will require the Hash code next to your commit. To check out the commit history we can use the following code:
git log --oneline
This is the commit history of my current Branch, and the yellow text is the Hash code.

Now we just need to specify the Hash code for the commit we want to undo:
git revert <hash-code>
In my case, I want to undo the commit having the Hash code 23ed91e

After this, a window will open like this, you just need to press shift+q
to exit. And then you can see the changes reverted.

note
PS. A new commit will be created without deleting the older one, but the changes will be reverted.
Git Stash
Understanding the Git stash command
Stashing in Git involves temporarily saving changes that are not yet ready to be committed, which is a useful technique when you need to switch to a different branch or feature without committing your current changes. Here's how you can use the git stash
command to stash changes in Git:
Stashing changes in Git
To stash changes in Git, you can use the git stash
command. This will save the changes you've made to your working directory, but not yet committed, in a temporary storage area. The command git stash
will stash all the changes that have been made to the working directory including untracked files. Here is an example:
git stash
If you want to stash only some of your changes, you can use the -p
option followed by the git stash
command. This will interactively ask you which changes you want to stash, and which ones you want to keep in your working directory.
git stash -p
Git Bisect
Understanding the Git bisect command
Bisect is a Git command that aids in identifying the exact commit that caused a bug or issue in your code. It can be particularly valuable when dealing with a vast codebase that has undergone numerous changes over time and you require to pinpoint the precise commit responsible for a problem. Here's how you can use the git bisect
command to debug code in Git:
Using Git bisect to debug code
To use Git bisect, you first need to identify a "good" and a "bad" commit. The "good" commit is one that is known to be working correctly, while the "bad" commit is one that is known to have introduced the bug or issue you are trying to fix. Once you have identified these two commits, you can use the following command to help you find the commit that caused the problem.
git bisect
The git bisect
command works by performing a binary search through the commit history of your repository, starting at the middle point between the good and bad commits, and asking you to test each commit to see if it is good or bad. Based on your feedback, Git will then move to the next commit to test, and continue the process until it finds the commit that introduced the bug.
Conclusion
Git is a powerful version control system that enables developers to successfully work with others and track changes to their code. It offers a number of tools and features, such as branching, merging, reverting changes, stashing, and bisect, that make it easy to manage complex codebases and workflows.
By understanding the basics of Git and learning how to use its key features effectively, developers can improve their productivity, reduce mistakes, and streamline their development process. Git is a crucial tool that can assist you in achieving your objectives more quickly and effectively, whether you are working on a team project or on a Personal project.
Although learning Git properly may seem overwhelming at first, it's worth the effort. Just trust the process, and you will get used to it. You can advance your coding abilities and become a proficient and confident developer by using Git regularly and becoming familiar with its commands and workflows.