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 and a few more downloads to boost your productivity in Git.
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
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.
$ git config — global user.name “Your name”
$ git config — global user.email “Your email”
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.
$ git version
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.
$ git init
Or you can use the repository name with your git init command.
$ git init [your repository name]
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.
git clone [your project url]
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.
$ 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)
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.
$ git commit -m “your useful commit message”
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.
$ git status
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
(i) To list all branches
$ git branch
(ii) To create a new branch
$ git branch
(iii) To delete a branch
$ git branch -d
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.
$ git checkout
Also, you can create and checkout to a branch in a single like, here is the usage for that
$ git checkout -b
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.
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.
$ git remote add
$ git remote add origin https://dev.azure.com/aCompiler/_git/DemoProject
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.
$ git push -u
$ 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
$ git push — set-upstream origin feature-branch
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.
$ git fetch
The Git pull command downloads the content (and not the metadata) and immediately updates your local repository with the latest content.
$ git pull
This Git command temporarily stores your modified files. You can work in stashed with the following Git command.
$ 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
With the help of the Git log, you can see all the previous commits with the most recent commit appear first.
$ 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
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.
$ git shortlog
Compared to the Git log, this command git show will show you details about a specific commit.
$ git show
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.
$ git rm
Git merge helps you to integrate changes from two branches into a single branch.
$ git merge
This command will merge the into your current selected branch.
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.
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.
$ git rebase
The Git bisect command helps you to find bad commits.
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.
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.
$ git cherry-pick
Git cherry-pick doesn’t modify the history of a repository; instead, it adds to the history.
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.
$ git archive — format zip HEAD > archive-HEAD.zip
It will create a zip archive of the current revision.
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 .
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.
$ git blame
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.
$ git tag -a v1.0.0
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.
$ git verify-commit
In the same way you can confirm a tag.
$ git verify-tag
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.
i) to compare working directory with local repo
$ git diff HEAD
ii) to compare two branches
$ git diff
Git citool is a graphics alternative of the Git commit.
$ git citool
To rename a git file. It will accept two arguments, source and target file name.
$ git mv
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.
$ git clean
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.
$ git help
This command does the same thing as git log but in a raw form. And it’s in the git because of historical reasons.
$ git whatchanged
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,
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.