Top 35 Git Commands with Examples (and a Bonus)
Tags: git, github
Published: 2021-05-11 06:33:37

If you are a new or experienced developer, you have to use source control. And good chances are you are using Git to manage your source code.

And to use Git to its full potential, you need to know Git commands. Here you will learn the most helpful Git commands that will take you from one level to another.

To make this Git commands guide more helpful, I have divided the guide into three different sections: **Basic, Intermediate, and Advanced Git commands**.

This is an epic guide. And to make it more useful, I have added a **Bonus section** where you can [**download 51+ Git commands**](https://acompiler.com/git-commands/) and a few more downloads to boost your productivity in Git.

## Download PDF section


__[51+ Best Git Commands](https://acompiler.com/git-commands/)__

__[Git HEAD: The Definitive Guide](https://acompiler.com/git-head/)__

__[Git best practices](https://acompiler.com/git-best-practices)__







## Basic Git Commands  

In this section, you will learn the essential Git commands. These basic Git commands are the foundation to learn more advanced commands.

Here are the nine useful Git commands  

### 1. git config

Git config command is super helpful. Especially when you are using Git for the first time, or you have a new Git installation. This command will set up your identity — Name and Email address. And this information will be used with every commit.

__Usage__

_$ git config — global user.name “Your name”
$ git config — global user.email “Your email”_  

### 2. git version

As its name implies, it’s just to check which version of Git you are using. At the moment, writing this guide, the latest version of Git for Windows is 2.31.1. It was released on 27th March 2021.

__Usage__

_$ git version_  

### 3. git init

This is probably the first command you use to start a new project in Git. This command will create a blank new repository, and then you can store your source code inside this repo.

__Usage__

_$ git init_

Or you can use the repository name with your git init command.

_$ git init [your repository name]_  


### 4. git clone

The git clone command will use an existing repository to copy. There is one main difference between the git init and git clone. You will use the Git clone when you need to make a copy on an existing repository. 

The git clone command internally uses the git init command first and then checks out all its contents.

__Usage__

_git clone [your project url]_  


### 5. git add

The Git add command will add all the new code files or modified files into your repository. This command offers different options to add files and folders. 

Here is the usage of the Git add command.

__Usage__

_$ git add your-file-name_

(it will add single file to your the staging area)

_$ git add *_

( this option will add all the modified and new files to the staging area)  


### 6. git commit

This Git command is essential. Your project quality may drop if you will not use this command appropriately. 

There is another article about how to use Git commands property, and you can read that here.

In simple words, git commit will add your changes to your local repository.


__Usage__

_$ git commit -m “your useful commit message”_  


### 7. git status

This Git command is convenient to see how many files are there which need your attention. 

You can run this command at any time. You can use it in between Git add, and Git commits to see the status.

__Usage__

_$ git status_  

### 8. git branch

Most of the time, you have multiple branches in your Git repository. In simple terms, the branch is an independent line of code development.

With the Git branch command, you can manage your branches effectively. There are many different options and switches of the Git branch.

To make it simple, here I will highlight how you can create and delete a Git branch. And, in case you need more depth, you can refer to — [Git Branching & Merging section of this article](https://acompiler.com/git-commands/)

__Usage__

(i) To list all branches
_$ git branch_

(ii) To create a new branch
_$ git branch <branch-name>_

(iii) To delete a branch
_$ git branch -d <branch-name>_  


### 9. git checkout

This Git command is used to switch between branches. This is one of the powerful git commands and can use used as a swiss knife,

In simple words, here is the syntax to switch to another branch.

__Usage__

_$ git checkout <branch-name>_

Also, you can create and checkout to a branch in a single like, here is the usage for that

_$ git checkout -b <your-new-branch-name>_    


## Intermediate Level Git Commands  

After the basic Git commands, it’s time to share with you intermediate Git commands; these Git commands are super helpful if you need to collaborate with your team, share your code with others. 

Also, there are commands like Git log that will help to see the history of previous commits.    

### 10. git remote
Git remote command acts like a border, and If you need to connect with the outside world, you have to use the Git remote command. This command will connect your local repository to the remote.

__Usage__

_$ git remote add <shortname> <url>_

__Example__

_$ git remote add origin https://dev.azure.com/aCompiler/_git/DemoProject_

### 11. git push

Once you are connected with the remote repository (with the help of the git remote command), it’s time to push your changes to that repository.

__Usage__

_$ git push -u <short-name> <your-branch-name>_

__Example__

_$ git push -u origin feature-branch_

You should have origin and upstream set up before you use Git push. And here is the command to set up upstream.

_$ git push — set-upstream <short-name> <branch-name>_

_$ git push — set-upstream origin feature-branch_


### 13. git fetch

When you need to download other team members’ changes, you have to use git fetch. This command will download all information for commits, refs, etc., 

so you can review it before applying those changes in your local repository.

__Usage__

_$ git fetch_

### 14. git pull

The Git pull command downloads the content (and not the metadata) and immediately updates your local repository with the latest content.

__Usage__

_$ git pull <remote-url>_

### 15. git stash

This Git command temporarily stores your modified files. You can work in stashed with the following Git command.

__Usage__

_$ git stash_

And you can view all of your stashes with the following command

_$ git stash list_

And if you need a apply a stash to a branch, simply use apply

_$ git stash apply_

### 16. git log

With the help of the Git log, you can see all the previous commits with the most recent commit appear first.

__Usage__

_$ git log_

By default, it will show you all the commits of the currently checked out branch, but you can force it to see all the commits of all the branches with all option.

__$ git log — all__

### 17. git shortlog

The shortlog command shows you a summary from the Git log command. This command is helpful if you are just interested in the short summary.

This command is helpful to see who worked on what as it group author with their commit.

__Usage__

_$ git shortlog_

### 18. git show

Compared to the Git log, this command git show will show you details about a specific commit.

__Usage__

_$ git show <your-commit-hash>_

### 19. git rm

Sometimes you need to delete files from your codebase, and in that case, you can use the Git rm command.

It can delete tracked files from the index and the working directory.

__Usage__

_$ git rm <your-file-name>_

### 20. git merge

Git merge helps you to integrate changes from two branches into a single branch.

__Usage__

_$ git merge <branch-name>_

This command will merge the <branch_name> into your current selected branch.

## Advanced Level Git Commands

And now the time to up one more level. In this section, you will learn the advanced Git commands. These commands will take time and practice. Once you know the basics of these commands, it will be easy to use them every day.

### 21. git rebase

Git rebase similar to the git merge command. It integrates two branches into a single branch with one exception. A git rebase command rewrites the commit history.

You should use the Git rebase command when you have multiple private branches to consolidate into a single branch. And it will make the commit history linear.

__Usage__
_$ git rebase <base>_

### 22. git bisect

The Git bisect command helps you to find bad commits.
__Usage__

i) To start the git bisect
_$ git bisect start_

ii) let git bisect know about a good commit
_$ git bisect good a123_

iii) And let git bisect know about a bad commit
_$ git bisect bad z123_

With Git bisect you can narrow down the broken code within a few minutes.

### 23. git cherry-pick

Git cherry-pick is a helpful command. It’s a robust command and allows you to pick any commit from any branch and apply it to any other branch.

__Usage__

_$ git cherry-pick <commit-hash>_

Git cherry-pick doesn’t modify the history of a repository; instead, it adds to the history.

### 24. git archive

Git archive command will combine multiple files into a single file. It’s like a zip utility, so it means you can extract the archive files to get individual files.

__Usage__

_$ git archive — format zip HEAD > archive-HEAD.zip_

It will create a zip archive of the current revision.

### 25. git pull — rebase

Most of the time, you need to do rebase (and no merge) when you use Git pull.

In that case, you can use the option
_$ git pull — rebase_

It will help you to keep the history clean. Also you can avoid multiple merges .

### 26. git blame

If you need to examine the content of any file line by line, you need to use git blame. It helps you to determine who made the changes to a file.

__Usage__

_$ git blame <your-file-name>_

### 27. git tag

In Git, tags are helpful, and you can use them to manage the release. You can think of a Git tag like a branch that will not change. It is significantly more important if you are making a public release.

__Usage__

_$ git tag -a v1.0.0_

### 28. git verify-commit

The git verify-commit command will check the gpg signature. GPG or “GNU Privacy Guard” is the tool used in sign files and contains their signatures.

__Usage__
_$ git verify-commit <commit>_

### 29. git verify-tag

In the same way you can confirm a tag.

__Usage__

$ git verify-tag <tag>

### 30. git diff

Most Of the time, you need to compare two git files or branches before you commit or push. Here is a handy command to do that.

__Usage__

i) to compare working directory with local repo
_$ git diff HEAD <filename>_

ii) to compare two branches
_$ git diff <source branch> <target branch>_


### 31. git citool

Git citool is a graphics alternative of the Git commit.

__Usage__

_$ git citool_


![git citool](https://acompiler.com/wp-content/uploads/2020/06/git-citool-productivity-tool-768x431.png)

### 32. git mv

To rename a git file. It will accept two arguments, source and target file name.

__Usage__

_$ git mv <old-file-name> <new-file-name>_

### 33. git clean

You can deal with untracked files by using the Git clean command. You can remove all the untracked files from your working directory by using this command. 

In case you want to deal with tracked files you need to use the Git reset command.

__Usage__

_$ git clean_


### 34. git help

There are many commands in Git, and if you need more help with any command, you can use git help at any time from the terminal.

__Usage__

_$ git help <git-command>_

### 35. git whatchanged

This command does the same thing as git log but in a raw form. And it’s in the git because of historical reasons.

__Usage__
$ git whatchanged

## Bonus Section

As not, you know the Git commands, and it is time for the bonus section. 

Here I will share with you Git best practices and downloadable material so you can read in your own time.

First, let’s talk about the free download that I would love to share with you,

## Download PDF section


__[51+ Best Git Commands](https://acompiler.com/git-commands/)__

__[Git HEAD: The Definitive Guide](https://acompiler.com/git-head/)__

__[Git best practices](https://acompiler.com/git-best-practices)__

### Git Best Practices

Here I will share in brief one of the favourite Git best practice — __AFTER technique__

A — Atomic commits
F — Frequent commits
T — Test before your push
E — Enforce standards
R — Refactoring is not a feature

I hope you enjoyed this article. These Git commands will take your skills to the next level.

For such informative articles, please [join aCompiler’s newsletter](https://acompiler.com/newsletter/).