Top Git Commands Every Developer Should Know

Git is a version control system that allows developers to track and manage changes made to their code. It is an essential tool for any software development team, it helps to keep the codebase organized, maintainable, and easily shareable.

It allows multiple developers to work on the same codebase simultaneously without overwriting each other’s changes and it also makes it easy to merge contributions from different team members.

Git keeps a complete history of all changes made to the codebase, making it easy to go back to a previous version or restore a deleted file.

Last but not least it allows developers to create separate branches for different features or bug fixes, and then merge them back into the main codebase when they are ready. This allows for easy experimentation and testing without affecting the main codebase.

But, which are the top git commands that every developer should know?

git init

git init is a command used to initialize a new Git repository? When you run this command in a directory, Git will create a new hidden subdirectory called “.git” where it will store all the metadata about your repository, including a record of all the commits you make. Example:

git init
git clone

It is a command used to create a copy of a remote Git repository on your local machine. This is typically done when you want to start working on an existing project that is hosted on a remote server, such as GitHub.

When you run git clone, Git will create a new directory with the same name as the repository and copy all the files from the remote repository into it. Example:

git clone https://github.com/etrupja/complete-ecommerce-aspnet-mvc-application.git
git add

It is a command used to stage changes in the working directory for a commit. When you make changes to the files in your working directory, you can use git add to tell Git which changes you want to include in the next commit.

The git add command takes one or more file names as arguments, and it adds the changes made to those files to the staging area. Example:

git add htmlFileName.html

You can also use the

git add .

command to stage all the changes made to the files in the working directory, regardless of whether they are new files or changes to existing files.

git commit

It is a command used to create a new commit in the local repository. A commit is a snapshot of the current state of the codebase, including all the changes that have been staged using the git add command.

git commit -m "added new feature to app with this commit"

The -m flag allows you to add the commit message directly on the command line.

git push

It is a command used to send commits from the local repository to a remote repository. When you run this command, Git will send all the commits that have been made locally but not yet pushed to the remote repository.

git push

This command pushes all the commits in the current branch to the origin remote repository. But, you can also specify the remote repository and the branch name to push your commits.

git push remote specific-branch-name

git status

It is used to check the current status of a local repository, including which branch you are currently on, which files have been modified, and which files are staged for commit.

An example response of git status would look like below:

On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        newfile.txt

no changes added to commit (use "git add" and/or "git commit -a")
git log

It is a command used in the Git version control system to display a log of commits in a repository. The log lists each commit with its SHA-1 (Secure Hash Algorithm 1) hash, the author of the commit, the date it was committed, and the commit message. An example response of git log looks like below:

commit df3c4d5c4e7f8a9b2c5d6e7f1a2b3c4d5e6f7g8
Author: Ervis Trupja <[email protected]>
Date:   Wed Jan 11 10:30:00 2023-0700

    Update README with new instructions

commit a1b2c3d4e5f6g7h8i9j0a1b2c3d4e5f6g7h8
Author: Jane Smith <[email protected]>
Date:   Fri Jan 13 12:00:00 2023 -0700

    Add new file newfile.txt
git diff

It is a command that allows you to view the differences between the files in your working directory and the files in the most recent commit. It shows the changes you have made but are not yet committed to the repository.

Example response of git diff looks like below:

diff --git first/example.txt second/example.txt
index a981273..d67af56 100644
--- first/example.txt
+++ second/example.txt
@@ -1,5 +1,5 @@
 This is an example file.
-It has some text in it.
+It has some important information in it.
 
 And some more text on the next line.
git branch

In Git, a branch is a separate line of development. By default, when you create a new repository, it has a single branch called “master”.

As you start developing your app you can create branches other than master to help manage your code or so different developers can work on different features.

The git branch command allows you to create, list, and manage branches in your repository. Some of the commands you can use with git branch include:

  • git branch – lists all branches in the repository, and shows an asterisk next to the current branch you are on
  • git branch branch_name – creates a new branch called “branch_name”
  • git branch -d branch_name – deletes the “branch_name” branch
  • git branch -m old-branch-name new-branch-name – renames the “old-branch-name” branch to “new-branch-name”
  • git branch -a – shows all branches in the local and remote repositories.
git checkout

It is a command that allows you to switch between different branches in your repository, or to switch to a specific commit.

Some of the commands you can use with git checkout include:

  • git checkout master – switch to the “master” branch
  • git checkout new-feature – switch to the “new-feature” branch
  • git checkout HEAD~3 – switch to the commit three commits before the current commit
  • git checkout dotnethow1234 – switch to a specific commit identified by the hash “dotnethow1234”

You can also use git checkout to create a new branch, by using the -b option. This will create the branch and switch to it at the same time. Example:

checkout -b new-branch-name

This command will create a new branch called “new-branch-name” and switch to it.

git merge

It is a command that allows you to combine changes from multiple branches in your repository. It is typically used to take the changes you have made in a separate branch and apply them to the “master” branch.

Here are some examples of how you might use the git merge command:

  • git merge new-branch-name– merge the “new-branch-name” branch into the current branch
  • git merge --abort – abort a merge that is in progress

What you have seen above is a list of what I think as the most important commands that every developer should know, but it is definitely not the full list. If you want to check out the full list of all the git commands, you can navigate to this link.


Enjoyed this post? Subscribe to my YouTube channel for more great content. Your support is much appreciated. Thank you!


Check out my Udemy profile for more great content and exclusive learning resources! Thank you for your support.
Ervis Trupja - Udemy



Enjoyed this blog post? Share it with your friends and help spread the word! Don't keep all this knowledge to yourself.