Most Commonly Used Git Commands

Rajesh Chittampally
6 min readApr 25, 2020

--

Git is a distributed version control system for tracking changes in source code during software development. Git is used by developers daily across the globe. Though there are many commands in Git, not all those commands will be used in daily work.

I have listed down some of the most commonly used Git commands.

Before you can use Git commands you need to have a repository to work with. There are mainly two ways of creating a Git repository.

  • By creating a new Git repository using git init
  • By cloning an existing Git repository using git clone

1. Git init

Git init will make a normal directory(folder) into a Git repository. If you want to create a Git repository then first create an empty folder and then go to that folder.

Now, once you are in the folder run this below command.

The above command will now create a subdirectory called .git. This folder is a Git repository skeleton and will contain the necessary files.

2. Git clone

If you already have a Git repository and wish to have that repository in the local environment so that you can work on it, we use the below Git command. Git clone will make an identical copy of the latest version of the project in a repository and saves it to your computer.

The Git clone command will look like the one shown below. The URL should be replaced by the link of the repository which you want to clone.

git clone command syntax

For example, let us clone Google’s lighthouse project repository from Github into our computer. Below is the screenshot of the lighthouse project’s repository. What you need to do is, click on the Clone or download (green button) and copy the URL which is shown below the button and paste in the URL part of the Git command shown above.

Github repo of Google’s lighthouse project

After copy-pasting of the URL, the clone command should look like the one shown below.

cloning Google’s lighthouse repository

As shown in the image above, we used the git clone command to clone a repository from Github into our local development machine.

We used Google’s lighthouse project repository for demonstrating the Git clone command here.

3. Git branch

Git branch command will list all the branches in the current repository.

To let us know which branch we are currently working on, an asterisk (* mark) will be shown beside the name of the current branch.

Since there is only one branch in the repository, only the master branch is being shown here. And it is the current branch.

4. Git checkout

Git checkout is one of the most used commands. It is mainly used to switch from one branch to another branch. But, we can also use it to switch to a particular file or to a particular commit by specifying the commit SHA.

Before you run this command, you need to make sure

  • that there are no changes in your current branch, they are either committed or stashed
  • the branch which you want to switch to exists in your local
git checkout command

Replace the NAME_OF_THE_BRANCH with the branch name.

We can also use this command to create a new branch and switch to that branch.

This command will create a new branch called new_branch and will switch to that branch

-b in the command will tell to create a branch and switch to it.

5. Git status

Git status command is used to get the required information about the current branch.

git status command

Git status will give us information about

  • current branch
  • If there are any files in the branch to be committed
  • If there are any files that are untracked, staged or unstaged
  • If there are any files that are created, modified or deleted or to push or pull

6. Git add

Git add command is used to add the files that are created, modified, or deleted to the staging index. Any file that has to be committed should be present in the staging index before they are added to the staging index, all these are files will be known as unstaged or unstaged files.

If you want to add a single file to the staging index, then you need to replace FILE in the command with the path to that file.

You can also add all the unstaged files to the staging index at once.

For that, you need to use the above command.

7. Git commit

Git commit is another important and most used Git command. After working on some feature, we need to save those changes to the repository. We use this command for that purpose.

This will create a commit and all the files in the staging index will be included in the newly created commit. A short message is required to be added to the commit. This will be the commit messaging explaining the feature that was committed or the fix for a particular issue.

One thing to note here is that this command will save the changes on our local machine only.

git commit command syntax

8. Git push

Previously, we have created a commit and save changes in our local. Now, we need to save these changes on the remote repository (a repository on Github or somewhere else).

Git push will take the changes in the commit we have made and will upload them to the remote repository.

This will push the changes in our local to the branch named new_branch in the remote repository.

Suppose, if the branch is newly created then we need to upload the changes with the below command.

9. Git pull

Git pull command as the name tells, will pull the latest changes from the remote repository and will apply those changes in our local. Git pull command is a combination of two commands (git fetch & git merge).

This command will get the latest updates for the current branch.

10. Git remote update

This command is used to update all the local branches with changes that are present in the remote repository.

This command will pull changes for all the branches present in the repository but will not merge any changes.

--

--

Rajesh Chittampally

#reacting to the challenges faced by users and developing solutions with the help of #php, #node, #mysql so that users can #vue the applications as intended