In this post, I will give you a list of the most commonly used Git commands. Also, I will explain each of these commands so you will know when & why to use them. End of this post, I will give you a downloadable cheatsheet (PDF).
But why you should read this post when there are other sources?
Well, there are numerous Git commands, and you may don’t need many of them in your daily life. This post will show you the most commonly used Git commands that will serve your purpose even if you’re an expert.
Let’s get started.
Most commonly used Git commands & their explanation
Before you use any of these commands, make sure you installed Git on your machine. Otherwise, see the post that I linked in the last line to install it.
Once Git is available on your machine, open your command line. And this command line could be your Windows CMD (command prompt), Git Bash, Mac terminal, VS Code terminal, or any other Git-integrated IDE terminal.
Check the installed Git version
To check the version or if there Git is installed or not, type the following command & hit enter:
First time configure your global username & email
Git also needs to see who is making the changes. So you have to set the global username & email. Type the following two commands one by one & hit enter:
git config --global user.name "John"
git config --global user.email "firstname.lastname@example.org"
Make sure you replaced “John” & his email with your actual name & email. You have to keep the inverted commas (” “).
After you push a repository to the live hosting such as GitHub, Bitbucket, etc, you’ll see your Gravatar image that is associated with your email.
See the directory currently you are at
pwd command means “print working directory”
cd path_of_the_folder (cd means change directory).
Initiate Git on your local machine
By mean, this is the first git command that you have to use when starting a new project. It will create a new “.git” hidden folder in your project and start to keep track of changes.
check git status
git status will display the current status and show you the files that you made changes to.
Add all files to the staging
git add -A
The above command will stage all files that you made changes to. Learn more about staging files.
Add a few specific files to git
If you need to stage one or a few specific files, use the following command:
git add index.html sass/style.css This command will add index.html & style.css files to the stage. Here the style.css lives inside the sass folder.
If you only need to stage the index.html, use the following command:
git add index.html rest of the files will be ignored
Commit/save your changes
git commit -m "write a custom message"
This commit means saving your changes to the Git history. Note that you have to stage before making a commit (that I showed you earlier).
Stage & commit in one command
In the last two sections, you saw how to stage & make a commit (
git add -A,
git commit -m "message"). But you can combine these two commands just like the following:
git commit -am "changed font color to blue"
It’s generally used when you make small edits and make commits frequently.
Recover or restore your project up to the last commit
No matter if you made a mistake in your code or accidentally deleted project files & folders, the following command will restore your project up to the last commit.
git checkout -- .
Connect your local project with a remote repository
Before you do this, you have to create a git repository on GitHub or Bitbucket, or somewhere else. And then you can follow this command:
git remote add origin url_goes_here
Check the current git remote URL/destination
git remote -v
Replace the old remote URL with a new one
git remote set-url origin url_goes_here
For more detailed information, see how to change Git remote origin URL.
Push your progress to a live Git hosting server like GitHub, Bitbucket, etc
git push origin master
Note that you have to add a remote origin URL before you push the local project to the repository. And the repository could be on GitHub, Bitbucket, etc. The process/command is the same for everyone.
Create a new git branch
git branch name_of_new_branch
git branch footer-changes will create a new branch called “footer-changes”
Merge a feature branch with the master
Let’s say, you want to merge the “footer-changes” branch with the master. The command will be the following:
git merge footer-changes
See all sorts of branch-related commands.
There are two ways you can merge a branch to the master. If you want to learn more about merging, see this detailed post.
Remove a file from the staging (or unstage changes)
If you accidentally added all or unwanted files to staging, Git has still got you covered. To unstage changes or remove a file from the staging, type the following command & hit enter:
git reset path_of_the_file
For example, I want to remove a file from staging named “style.scss” which lives in the “sass” folder. So the command will be as follows:
git reset sass/style.scss
See more details and a step-by-step process to undo git add -A.
Pulling the latest progress from a git repository
git pull origin master
Why pulling: Let’s imagine that you’re working on a team and you went on vacation for one week. In the meantime, your coworkers made changes to the project. After you came back from vacation, you need the latest copy of the project. This is where & why the git pull comes in handy.
Learn more about git pull and how to grab the latest copy from the remote origin to your local machine.
Clone or copy a git repository
git clone repo_url_goes_here
For a more detailed walkthrough, see how to clone a Git repository.
Remove a file from the git commit
To remove a file from a Git commit, use the following command:
git rm name_of_the_file
Be sure to replace the “name_of_the_file” with the actual file name such as
style.css. To learn more about removing a file from git (local, remote origin, keep in local but remove on the remote origin), see this post.
There is also a very similar circumstance. Such as unstage a file from the commit. You can achieve it by git
reset HEAD^ file_name. Learn more about how to unstage a file from the git commit.
Change the last commit message
To change the last commit message, use the following command:
git commit --amend -m "Your new message"
If you want to change another past commit message such as the 5th or 9th commit message, you have to use the rebase. For example,
git rebase -i HEAD~5. For more detailed step-by-step guidelines & video instructions, see how to change the commit message.
And for the pushing to the remote origin, see how to push after rebase.
Undo the last git commit
To undo the last git commit locally, use
git reset HEAD~1
But if you need to undo the last git commit both locally & the remote origin, you have to use the
git reset --soft HEAD^
For a detailed & step-by-step process to undo a git commit, see this post.
Switch between git branches
To switch from one branch to another, use the following command:
git checkout name_of_the_branch
For example, if you want to switch to a branch named “john”, the command will be
git checkout john
Learn more about switching between branches in this post.
Git commands cheatsheetDownload PDF »
These are the most common git commands that I came across over the years. Yes, sometimes you may also need additional git commands such as
rebase, etc. But it’s very rare.
For your further reading, I have included a couple more git-related posts below that might be worth checking.