In this guide, we have compiled all the important and most used Git commands together with their usage. Follow till last to get yourself comfortable with Git and version control. Trust me if you get acquainted with Git once you won’t be able to resist using it. It is such a beautiful thing.

Often beginners confuse Git with GitHub and it is understandable due to the huge popularity of GitHub. GitHub is a cloud hosting service that lets you manage your Git repositories and collaborate with other people using Git. While Git is an open-source distributed version control system that helps track changes and versions of your work.

How Git Works?

Git is an open-source distributed version control system, which means you can too contribute to the code of Git and make it better. It works by distributing work to many levels and people and saves only changes each time with author and date. Anyone can pull a copy of your project and keep it, they can add changes to it as well and can request you to merge them into the original version of the project i.e. your copy.

We should learn Git commands along with other GUIs as it gives better command over the system because with commands we understand each and every action and what it is doing. Also using CLI is faster than using GUIs.

Installation of Git

Git Official site
Official Site of Git VCS

In Windows 10

Git has both CLI and GUIs available but we will learn Git CLI in this guide, Git GUIs are self-explanatory, though you can follow the guide for GitHub Desktop here. You can download the Git CLI from its official site for Windows, macOS or Linux. On Windows the installation process is very simple, like any other software, you just need to press the “Next” button a few times and the Git CLI will be ready to work. You need not do anything on the installation screen as default settings are perfect for most people.

On macOS

If you have installed XCode or its Command Line Tools on your Mac then Git may be already installed on your device. You can check the version of Git using the command

git --version

If you do not have Git installed, you need not worry at all. You can download the Git for Mac installer and follow the prompts to install Git on your Mac. Or, if you have installed Homebrew to manage packages on OS X then you can follow the command below to install Git on your Mac.

$ brew install git

You can verify the installation by checking the version of Git using the command given above.

On Linux

The process of installing Git on Linux is very similar to that of macOS. You just need to type sudo apt-get install git on the terminal and Git will be installed. You can verify the installation by checking the version of Git installed.

sudo apt-get update
sudo apt-get install git
git --version

We have prepared this tutorial on a Windows device, but the commands and their usage remains the same in all the environments and it works with the same efficiency. You can seamlessly follow the guide irrespective of your operating system and its version.

What is Git Bash?

Git bash in Windows
Git Bash in Windows

Git Bash is basically an application that provides a Unix based shell to emulate the Git CLI on Windows environment. Bash is the acronym for Bourne Again Shell and a shell is a terminal application that lets users connect to the operating system through the command-line interface. Git Bash is a package that installs Bash, some Bash utilities and Git on Windows environment. It helps us connect to and control some Git elements and our project repositories using commands.

Git Bash as mentioned above is a Unix based shell and it lets users use Git on it that does not mean it is meant for Git only, you can use Git Bash as a full-fledged terminal that is quite similar to the terminal in Linux.

Basic Git Commands and their Uses

We will now explore how to use various Git commands and will understand the operations performed by Git. To understand this better we will create a separate folder somewhere and inside the same folder, we will right-click and run Git Bash inside which we will be running and testing our Git commands. Also, create 2-3 dummy HTML files for understanding the concepts more practically.

  • git config --global your_name configures the user globally for the system and every commit in that system is by default registered to that user.
  • git config --global your_email_id similarly configures your email-id globally for commits.
  • git config --global returns the name of the global user, similarly, you can view the email id of the user using git config --global
  • git config your_name and git config can be used to set repository-specific username/email configuration. For this, you need to move to the working directory in Git Bash or directly open the Git Bash inside the working directory.
  • code. opens the VSCode, I use VSCode, you may use any text editor according to your convenience.
  • git init initializes a new Git repository from scratch and creates a hidden folder within the directory that tracks the work of the repository for version control. You can also clone an existing repository using the command git clone repo_url.
  • ls -lart is a Linux command which shows all the hidden files in the current directory. You can see more Linux commands here along with the complete meaning of each letter used.
  • git status shows the status of the files in the directory whether they are untracked, staged, unmodified, modified or committed. You can better understand the states of a file with the illustration given below.
Understanding Git
Git Staging and Commit areas
  • git add your_file.extension adds the file to the staging area and hence takes a snapshot of the changes performed in the file and includes it in the project’s history.
  • git add -A adds all the untracked files to the staging area. Then if you change the files, git status returns the modified tag beside the file. You can again add the modified files to the staging area using the git add -A command.
  • git commit commits the staged files finally. After git commit you will be asked to enter a commit message, you should know how to edit files in Vim.
  • git commit -m "Your_commit_message" is the command which directly commits the files with a given commit message.
  • git checkout your_file.extension matches your files to the last commit and restores the earlier version. It is useful if something went wrong in a particular file and you want to restore the earlier version of it.
  • git checkout -f matches the whole directory to the last commit and restores the earlier version of all the items in the directory.
  • git log shows who committed what and at what time along with the commit messages. You can exit out of the log by pressing “q” from the keyboard.
  • git log -p -5 shows the same details of the last 5 commits. Similarly, if you want to view the details for the last two commits, you can use git log -p -2.
  • git diff compares working untracked files with the staged files and highlights the changes. If we add all the modified files to the staging area then it returns nothing as there is nothing to compare.
  • git diff --staged compares staging area with the last commit and highlights all the changes.
  • git commit -a -m "your_commit_message" skips staging and directly commits all the files. It is not recommended most of the times.
  • git rm your_file_name.extension deletes the file from the staging area as well as from the working directory.
  • git rm --cached your_file_name.extension removes the file only from the staging area.
  • git status -s is similar to git status but it shows the status using symbols. An example of the output of git status -s in different scenarios is given below.
#When all the modified files are in the working directory and still not staged.
    M file_1.html
    M file_2.html
    M file_3.html
#When only 'file_1.html' is staged using 'git add file_1.html'.
  M   file_1.html
    M file_2.html
    M file_3.html
#Again if 'file_1.html' is modified then 'git status -s` returns.
  M M  file_1.html
    M file_2.html
    M file_3.html

What is .gitignore?

Git is an advanced version control system and an advanced system, it cannot follow a space-consuming architecture. So being smart, it tracks only source codes and ignores the automatically generated files like logs, output files of the source code and other automatically generated files. At the same time, to prevent any mishap it gives full control of ignoring such files to the master. Hence, we can create a .gitignore file and list all the unnecessary files which are not important and can slow down your push/pull speed.

  • touch .gitignore is a bash command used to create .gitignore file in the working directory. In the Windows environment, it is difficult to create .gitignore in file explorer so touch command is used in bash to create the file.

You can now open .gitignore in any advanced text-editor like VSCode and list down all the files which need not be tracked by Git.

Edit .gitignore in VSCode
Edit .gitignore in VSCode
  • If you list your_file_name.extension in .gitignore then it will ignore the mentioned file from the root directory as well as from all the subdirectories of the working directory.
  • *.extension will ensure that any file in all directories and subdirectories with extension .extension gets ignored. It could be any extension (I used .extension for reference it could be any extension like .log, .png, etc.).
  • /your_file_name.extension only ignores the file in the root directory where .gitignore is present.
  • folder_name/ ignores and complete subdirectory or folder.

Also Read: 10 VSCode Tips and Tricks You Should Know


Branches are basically separate copies of the main/master branch that are meant for development or modification purposes. For example, if a project is in production and you want to modify it or develop some new feature in it then, it is obvious that you won’t like to try changing the project on production directly, you will try and test your changes before pushing it into the production. The same feature is provided by Git with the help of branches.

  • git branch branch_name creates a branch with the name branch_name which is an exact replica of the master branch but any operation performed in this branch won’t be reflected in the master branch. You will have to manually switch to the new branch if you want to work in the new branch.
  • git branch gives a list of all the existing branches of the current project. It gives a star beside your current branch to specify which branch you are currently in.
  • git checkout branch_name changes your current branch to branch_name without affecting your work in the current branch.
  • git branch -m oldname newname changes the name of the branch oldname to newname.
  • git branch -m newname changes the name of the current branch to newname.
  • git merge branch_name merges the work of the branch branch_name to the master branch. Run this command being on master branch. The command git log -p -5 will show the last 5 commits including the commits of the branch branch_name.
  • git checkout -b branch_name creates a branch named branch_name and switches to that branch automatically. You need not switch to the new branch manually if you use this command.

Remote Repository on GitHub

Sign up on GitHub
Sign Up on GitHub for free

We can create a remote repository on GitHub or any other Git hosting services like Bitbucket, GitLab, etc. The process of creating an account on GitHub is fairly simple and user-friendly.

After creating your GitHub account, there are two ways of authenticating the account on Git, a sign-in pop up while you add a remote repository to the Git project or using an SSH key. Using an SSH key is more secure and satisfying and it is a must for private repositories. You can follow this guide to generate and link the SSH key to your account. You can also find the link to the guide on your GitHub account’s ‘Settings’ under the ‘SSH and GPG keys’ tab.

Authentication on GitHub
Generate and Add SSH key to GitHub

Now, you can create your first repository on GitHub by clicking on the ‘New’ button and fill in all the details for the new repository. You can add a Readme file too which will contain all information about your repository. You can use tools like to create an intuitive readme file very quickly.

New Repository on GitHub
Create Your first Remote Repository on GitHub

You can get the URL and SSH to any public repository by clicking on the ‘Code’ button as shown in the image below.

The URL or SSH of GitHub Repository
Get the URL of any Public Repository Here
  • git remote add origin your_github_repo_url/ssh links your remote repository to the current project and gives the name origin to the particular remote repository.
  • git remote shows all the names of the remote repositories linked with the current project.
  • git remote -v shows all the linked remote repositories along with their URLs.
  • git push origin master pushes your local master branch to the master branch of the remote repository added with the name origin.
  • git push -u origin master pushes the branch master to the remote repository and sets it to default such that any further pushes using git push or git push origin master is a push from the local master branch.
  • git push origin other_branch_name pushes the branch other_branch_name to the remote repository.
  • git push origin --all pushes all the branches from the local repository to the remote repository named origin.
  • git fetch origin downloads all history from remote-tracking branches.
  • To change the URL of the remote repository named as origin you can use git remote set-url originnew_url/ssh.
  • git clone repo_url clones an existing repository (whose URL is provided) to the current folder with the repository’s name.
  • git clone repo_url folder_name clones an existing repository with the name folder_name.

Change Default Repository in GitHub

There has been a recent update in GitHub, according to which the default repository in GitHub has been named as main, earlier it was master. So, if you push your local master branch to the remote repository on GitHub, it will create a new branch master. You can change the default branch of that repository by heading to ‘Repository Settings’ >> ‘Branches Tab’ >> ‘Switch to another branch’. See the image below for reference.

Default branch on GitHub
Switch default branch on GitHub

You can also change your default branch settings for all your repositories by going to ‘Account Settings’ >> ‘Repositories Tab’ >> ‘Update default branch name’. See the image below for reference.

Change default branch on GitHub
Update default branch for all repositories

Push to remote main branch from local master

You can also push directly to the main branch from your local master branch using the command:

git push origin master:main


This guide is a very good platform for beginner’s to get introduced to Git and start using it in their projects. The commands provided above are some of the most used Git commands and most of the time, beginners won’t be requiring other Git commands but if you need to perform some other action, you can always try putting your queries on Google and explore the solutions. We have tried to give you a start with Git, there are much more things you can do with Git and GitHub. The more you explore, the more you will learn.

We hope you liked this article, there are many more such guides on our blog. You can try reading the complete list of Linux commands for beginners and 100 Windows 10 shortcuts that can make your life easy.

Kindly share this article with your friends or colleagues who are looking towards starting off with Git and GitHub. You can comment down your queries in the comment section below, let us have a fruitful discussion.


Ajay Choudhury

Ajay Choudhury is the co-founder of OrbitGadget. He is a tech geek and a constant learner with a burning passion for technology. He loves sharing his know-how with others through his projects and blog. When not coding or writing content he loves playing football or enjoying music.

Leave a Reply

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

Our posts may contain affiliate links! If you buy something through one of those links, you won't pay a penny more, but as an Associate/referrer, I earn a small commission from qualifying purchases which helps us keep our good work on. Check our complete disclosure here. Thanks for the support!