Basic Git Commands

If you are new to git then you can follow our previous article on What is Git and how to use it in Linux? which explains git, its installation, configuration, and usage on a Linux system.

Today in this article I will discuss basic and mostly used git commands.

Basic and mostly used git commands

Below is the list of git commands with their usage –

git config

This command is used to get and set global options such as the author’s name and email address.

Example and usage of git config command –

git config --global “[name]”
git config --global “[email_address]”

git init

The git init command is used to initialize a repository or start a new project, it creates a hidden directory called .git which stores all of the objects and refs that git uses and creates as a part of your project’s history.

Usage of git init command –

git init [repository name]

Example –

git init /home/lalit/testProject

git clone

The git clone command is used to download a repository from an existing URL.

Usage of git clone command –

git clone [url]

For example, the following command will clone the repository hellogitworld from the given URL.

git clone

git add

This command is used to add the files to the index.

Usage of git add command –

git add [file]

For example –

git add hello.c

If you want to add all the files under the current working directory to git index then use the following command –

git add.

git commit

To store the content of a file added to the git index permanently we will have to commit the changes. This will be saved in the local git repository.

Now commit the changes using the following command –

git commit -m “[ Type a message for changes you have made]”

To commit all the files use option -a with git commit command –

git commit -a

git diff

This command shows the file differences that are not yet staged.

Usage of git diff command –

git diff

To see the difference between the two branches use –

git diff [first_branch] [second_branch]

git status

This command displays the state of the working directory and staged snapshot. It is used in conjunction with git add and git commit to see exactly what’s being included in the next snapshot.

git status

git branch

The following command shows the local branches in the current repository.

git branch

To create a new branch use –

git branch [branch name]

To delete the features of a branch use –

git branch -d [branch name]

git rm

This command deletes the file from your working directory and stages its deletion.

Usage of git rm command –

git rm [file]

git pull

The git pull command fetches and merges changes from the remote server to your current working directory.

Usage of git pull command –

git pull [URL of Repository]

git push

This command sends the committed changes of the master branch to the remote repository.

Usage of git push command –

git push [variable name] [branch]

Example –

git push origin master

Now you can find all git commands and their usage in detail in the official documentation of git.

Leave a Comment