Hello Sunil
Essential Git commands - Basic to advance with examples

Essential Git commands : Basic to advance with examples

Git & GitHub has steadily risen from being just a preferred skill to a must-have skill for multiple job roles today. In our previous article we have covered Git workflow with basic Git commands.

Also Read: Getting started with Git and GitHub: the complete beginner’s guide

In this article we will be focusing on some advanced Git commands which you will be using frequently while you are working with Git.

First, you need to install Git on your computer. Follow these instructions if you have not done that already. Once you have Git installed, we can move on to advance Git commands.

How to install Git on Linux

Most of the Linux distributions comes up with Git as pre-installed. Even if it is already there, it’s good to update it to the latest version.

For RedHat / CentOS / Fedora, use the following command in the terminal to install it.

sudo yum install git

Install git on redhat linux - git commands

Note: For this article we are using Red Hat Enterprise Linux, you can find the step by step installation instructions form here.


If you are on a Debian based distribution like Ubuntu, try the following command to install it.

sudo apt-get install git

Install git on Ubuntu linux - git commands

For openSUSE, use the following command:

sudo zypper install git-core git

Install git on openSUSE linux - git commands


For more different Linux distributions, there are instructions for installing on this link.

Now, you have successfully installed Git on your Linux system. Here are the top essential Git commands that will help in your day-to-day tasks.

The simple examples will give you an understanding of the commands, so you can easily remember the syntax when you need to use them.

Note: If you are new to command line then you should first brush-up your skills to comfortable with command line in Linux terminal by reading this article.

#1 git config

User identity

In Git you can specify user identity by providing its “Username” and “Email address.” This information will be used during each commit operation. Execute below command in terminal to assign identity:

git config –global user.name “your_GitHub_username”
git config –global user.email “your_GitHub_email id”

git config - git commands

But for some reason if you want to keep your email private then follow the instructions in this article. You only need to check two boxes in your GitHub account to achieve it.

Editor

Now that your identity is set up, you can configure the default text editor that will be used when Git needs you to type in a message. If not configured, Git uses your system’s default editor.

Also Read : How to install Visual Studio Code on Linux

For Ex: For this article we are using Visual Studio Code as our default editor. If you want to set it as default editor for you too then type below command in Git bash:

git config –global core.editor “code –wait”

Note: –wait option will wait for the Visual Studio Code window to be closed before returning control to the command line.

If you want to use Sublime Text or other editor as default on your system then you can read this article for setup.

Note: – You can open Visual Studio Code from terminal by typing “Code . “

Compression

This setting configures compression level to be used. Valid range for compression is -1 to 9.

-1 value indicates zlib compression and is default compression level. 0 value means no compression, and 1 to 9 are various speed/size tradeoffs, 9 being slowest.

git config –global core.compression 2

Diff tool

This setting configures diff viewer tool. For example, below command configures vscode as a diff tool:

git config –global diff.tool vscode

git config diff tool - git commands

Note: We have used –global option everywhere, which will make this configuration global.

It means that same configuration will be applied to all repositories present on current system. To make configuration repository specific just remove –global option.

Also Read : How to use VS Code as default editor for Git merge tool

List configuration

To list Git configuration execute below command in terminal:

git config –list

git config list - git commands

Sunil Pradhan

Hi there 👋 I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.

Add comment

Stay Updated

Want to be notified when our article is published? Enter your email address below to be the first to know.

Sunil Pradhan

Hi there 👋 I am a front-end developer passionate about cutting-edge, semantic, pixel-perfect design. Writing helps me to understand things better.