8 Basic GIT Commands Every Newbie Developer Must Know

Gaurav Kukade
Arnekt-AI
Published in
5 min readDec 30, 2019
Cover image showing ‘git init’ command

GIT is one of the most important parts of the developer’s day to day work. So learning GIT is a must for a newbie developer. In this article, you are going to learn the 8 most important basic GIT commands.

Below, I have listed down all the 8 commands and then, we will have a look at them one by one.

  1. git init
  2. git clone
  3. git add
  4. git commit
  5. git status
  6. git branch
  7. git pull
  8. git push

1. git init

git init command initializes brand new GIT repository (locally) and begins tracking the existing directory.

When you hit the git init command, git adds a sub-folder withing an existing directory that manages all required files for version controlling.

git init command in git bash

You are hitting git init means you want to initialize the current directory as GIT repository.

The following GIF show initializing a new repository and a hidden sub-folder containing all data structure required for version control.

Initializing a git repository locally
Initializing a git repository locally

2. git clone

git clone creates a local copy of a repository that already exists remotely. The local copy is the exact copy of the remote repository, it contains the same files, history, and branches.

$ git clone <remote-repository-link>

You can clone any public repository from the platforms like GitHub, BitBucket, GitLab, and other GIT hosting platforms.

The following GIF shows the git clone command

Cloning the remote repository
Cloning the remote repository

3. git add

git add stages a change.

If you are done with changes in your code then its necessary to stage that changes and take a snapshot of it to include it in the repository’s history.

git add does the first step, it stages a change.

$ git add <path-of-the-file-you-made-changes-in>

If you made changes in the multiple files and you want to stage all files in the same command then add the file path of all files separated y a single space.

$ git add <first-filepath-you-want-to-stage> <second-filepath-you-want-to-stage> <nth-filepath-you-want-to-stage>

If you want to stage all the files, then write ‘.’ (dot) after git add

$ git add .

Any staged changes will become the part of the next snapshot and a part of the repository’s history.

You can stage and take a snapshot of the current changes in a single command also but is not recommended.

Staging your changes first and then taking snapshot gives you complete control over the repository’s history.

The following GIF shows the git add command

Staging the changes
Staging the changes

4. git commit

git commit save the snapshot to the repository’s history.

git add does the first step i.e. staging the changes and git commit does the final step i.e. it saves the snapshot to the repository’s history. In GIT these two-step completes the changes tracking process.

$ git commit -m "<meaningful-git-commit-message>

You can write a meaningful message to the commit. It is recommended to write a commit message in the ‘Simple Present Tense’.

If you are committing a new feature to your project then your commit message should be "Add <feature-name> feature".

The following GIF shows the git commit command

Committing the staged changes
Committing the staged changes

It is the simple way to write the commit message but there is a more in-depth way to write a commit message with title and description. We will see that in a separate blogpost.

5. git status

git status shows the status of the changes as untracked, modified or staged.

The followig gif shows the git status command

Showing status of the current change process
Showing status of the current change process

6. git branch

git branch list the existing branches from the local repository. The current branch will be highlighted in green and marked with an asterisk.

git branch command in the git bash

The following GIF shows the git branch command

Showing the list of branches
Showing the list of branches

7. git pull

git pull update the local repository with updates from its remote counterpart. i.e. remote repository.

If your teammate made commits to the remote branch and you want to reflect these changes in your local environment then you will hit the command git pull.

This command will check if there are any updates on the remote branch as compared to your local environment, if yes, then it will update your local environment with these changes. If no, then it will do nothing.

The following GIF shows the git pull command

Pulling the changes committed by others on remote repository
Pulling the changes committed by others on remote repository

8. git push

git push updates the remote repository with any commits made locally to a branch

$ git push origin <branch-name-you-have made commits on>

If the branch does not exist on a remote repository then the whole branch with its commits will be pushed to the remote repository.

git push command in the git bash
Pushing locally committed changes to the remote repository
Pushing locally committed changes to the remote repository

Since you’ve made it to the end of this post, here is some bonus content!

How to create a new branch locally

You can create a new branch locally using the following command

$ git checkout -b <your-new-branch-name>

The following GIF shows creating a new branch locally

Creating a new branch locally
Creating a new branch locally

If you have queries please let me know in the comment section below.

--

--

Gaurav Kukade
Arnekt-AI

Java Developer 💻, TechGeek⚙, writes 🖋 at coderolls.com Follow me on twitter @gdkukade ❤