Git Commands Cheat Sheet – List of Commonly Used Git Commands for easy referencing.

Git Commands Cheat Sheet

Git is no doubt the indispensable tool in the modern developers’ toolkit.  It is an awesome tool that facilitates many people to simultaneously work on different parts of the same project. And later, merge them all together in a magical way. The steps get repeated over and over as the project evolves over time. And this can carry on indefinitely creating progressive versions of the software.

So majorly it helps in version control and lets many people work together in tandem towards a common goal.

It is an intimidating tool for sure. Perhaps because it uses so much of the command line interface. At least in my case, it kept me procrastinating to the point that I shied away from it for several years. It was not so difficult after all when I finally got the courage to put my head into it. I wonder now why I didn’t start sooner.

Git Overview

Global Information Tracker or GIT is a free open-source system that you can download to your computer to manage your software projects using version control. If you make a mistake or your project files get deleted somehow, you can easily revert them back to a previous version through git.

However, if your computer is damaged, then your information is lost.

Here comes the Git Repository Management Services like GitHub, Bitbucket, GitLab, etc to the rescue. These services let you host your repository away from your local computer. Now you can push your local repository to the remote one hosted by any of these hosting services.

This also lets another user clone or branch your remote files and pull a copy to their local system and work alongside you.

Some Common GIT Terminology

Checkout – It refers to fetching your last committed or saved files from the local repository.

Stage – This is where you review your files prior to committing them to the repository.

Unstage  – This means removing a file or several files from the staging area so that they are not committed or saved to the local repository.

Commit – Saving a snapshot of your files into the local repository.

Pull – This means fetching a copy of the saved files from the Remote Git Repository.

Push – This is when you push a copy of the locally committed version to the Remote Git Repository.

Branch – As the name suggests, it is referring to files branching out from the main branch. A new branch would usually have a meaningful name indicating why the new branch has been created.

Merge – This is when a branch is merged back into the master/main branch.

Clone – Creating a copy of the existing repository on your local machine. You need a remote repository URL to clone it.

Origin – This usually refers to the Remote Repository URL set for the current project. The first time you connect to your remote repository, it will ask for your credentials to authenticate you. Once done, it will be saved to your local machine, and you won’t be prompted again and again for it.

These are some of the common GIT terminologies I can think of right now. If I missed any important ones, please let me know in the comments below, and I would be happy to add them to the list above.

Installing GIT On Your Local Machine

You can download GIT from here: https://git-scm.com/

It will show you different downloads depending on whether you are using Windows or Mac. So go ahead and download it and follow the installation instructions. It is a straightforward and easy process, so I am not going to delve too much into it. I believe it will also give you the choice to install Git Bash along with the GIT application.

You don’t necessarily need Git Bash to run Git commands, because you can now use any command prompt to run it. However, if you are using Visual Studio Code like me then you would be running all your git commands using the command-line interface that comes integrated with Visual Studio Code.

It is not a unique feature with Visual Studio though, most Integrated Development Environments (IDE) would let you do this. So, feel free to use the one you prefer to develop with.

List of Git Commands

We will try to list all git commands possible here. If we miss out on a few, please feel free to let me know in the comments so I can add them to the list. Let’s begin.

Checking Git version

This command can be run from any command prompt to check if Git is installed on your machine. It should display a Git version number.

git --version

Add Your Details to Git on Your System

You need to personalize your Git so that it knows who exactly is making the commits. You need to add your username and email address.

git config --global  user.name "Amrit Ray"
git config --global user.email "[email protected]"

Check Configuration Settings

If you need to check your current Git configuration settings.

git config –-list

Getting Help with Git Commands

If you need to access the comprehensive manual page for any Git command, while using Git, you can run the help command in any of the below-mentioned formats. Verb below is the actual Git command you need help with.

git help <verb>
git help branch

git <verb> --help 
git branch --help

man git-<verb>
man git-branch

If you simply need a quick overview of the available options for any Git command, you can use the -h option.

git <verb> -h
git add -h

Create a Local Git Repository

Before you can save your projects into the Git repository, you first need to create one. To do that, first, navigate to the folder containing your project using the command prompt and run the command below.

git init

Check the Status of Files & Folders

Check whether files have been modified, deleted, staged, etc. The command must be run from within a git repository folder so that it knows which files’ status to show.

git status

Add Files to Staging before Committing

All files & folders must be staged before you commit them into the repository. Here you can review the files that will be saved. Whatever is added to staging will eventually get committed and saved.

git add index.html
git add -A

The first command adds index.html to staging. And the second command would add ALL files to staging.

Remove Files from Staging

You can unstage files and folders so that they do not get saved with the commit. The below command would remove the index.html file in the app folder from staging.

git reset app/index.html

Saving Changes to the Repository

Saving a snapshot of the current files & folders. It is good practice to add a message with every commit so you know where the project stands with the current commit. If you ever need to revert to an earlier version of the project, the message will help be very helpful.

git commit -m "My first commit."

Fetching Last Committed/Saved Files

If you ever need to restore your files from your local repository, you can run the command below to restore your last committed files.

git checkout -- .

Fetching Saved Files from Git Remote Server

git pull origin <branch name>
git pull origin master

Clone an Existing Repository

Cloning or copying an existing repository to your hard drive.

git clone <repository-url>
git clone https://github.com/git-repo-account-name/repo-name.git

Check Git Remote Repository Address

View the value set for Origin. It is the Git remote repository address where your files will be saved when you push them online.

git remote -v

Add New Git Remote Repository Address

You add/set this value for the first time when you push your files to the remote repository. Then it gets saved and used automatically each time you access the Origin.

git remote add origin <repository-url>
git remote add origin https://github.com/ray-creations/rctheme.git

Update Remote Git Repository Address

This command lets you update the currently set remote Git repository address where the files will be saved online. Sometimes when you clone a repository it would come with the Origin address already set. You would need to set it to your own Git Repository address. This command would do this for you.

git remote set-url origin <repository-url>
git remote set-url origin https://github.com/amritray/travel-site.git

Push Your Local Commits to a Remote Git Repository

Here we are assuming your main branch is called, “master”.

git push origin master

List All Branches

git branch

Create a New Branch

git branch <branch-name>

Switch Git Repository Branch

Checkout a particular branch.

git checkout <branch-name>

Create & Checkout Branch at the Same Time

You can create a new branch and switch to it at the same time using the command below.

git checkout -b <branch-name>
git checkout -b our-features

Push New Branch to the Remote Repository

git push origin <branch-name>

Setting Upstream Branch with Git Push Command

git push --set-upstream origin <branch-name>
git push -u origin <branch-name>

Changing Upstream Branch

git branch -u <origin/new-branch>
git branch -u origin/new-features

Deleting a Branch

git branch -d <branch-name>
git branch -d count-to-fifteen

Merging Branch to the Master Branch

Ensure you are switched to the master branch and then run the following command locally.

git merge <branch-name>
git merge the-red-footer

Updating a Feature branch from Main

The process involves you first updating your local master branch from from origin/master, and then merging it to your feature branch.

git checkout master
git fetch -p origin

We checkout the local master branch which we want to update here. We use -p to delete any remote tracking references that no longer exist in the remote.

git merge origin/master

The above command brings your local main branch in sync with your remote repository. Now we need to checkout the feature branch that we want to update from the the main branch.

git checkout <feature-branch>
git checkout new-sidebar

Now you are in your feature branch. Merge the main local branch to this branch by running the following command:

git merge master

Now this may open up vim where you will enter a commit message, save it, and then quite vim. Press “a” to enter insert mode. Second press “Esc” to enter command mode. Third, press “:wq” to write the file to disc and quite.

Your feature branch is now up to date. You can push it to your remote repository to bring it up to date too.

git push origin <feature-branch>

This process is explained in more detail here.

Create Tags

git tag <tag-name>
git tag v1.0

Create Annotated Tag

git tag -a v1.0 -m "version 1 of the software"

Display Tags

List the existing tags for your local repository.

git tag
git show v1.0
git tag -l "v1.*"

Push Local Tags to Remote Git Repository

git push origin v1.0
git push origin --tags
git push --tags

Delete Tags (if ever required)

Delete local tags:

git tag -d v1.0
git tag --delete v1.0
git tag -d v1.0 v1.1

Delete tags from remote Git Repository:

git push origin -d v1.0
git push origin --delete v1.0
git push origin :v1.0
git push origin -d v1.0 v1.1     

Help Us Keep the List Updated

These are some of the commonly used Git commands we use on a regular basis. The list helps us with the day-to-day use of Git and to reference the commands whenever needed. If I have missed any out that you feel should be included in the list above, please feel free to let me know in the comments below. I will be happy to add them too.

The ultimate aim of the article is to create a useful list of Git commands that one can use as a cheat sheet whenever needed.

Also, it would be great if you can share it on your social handles and link to it if possible. It would help the article reach a wider audience and be of more help to others.

Leave a Reply

Your email address will not be published. Required fields are marked *