Introduction to Git: Basic Commands Everyone Should Knowby@abram
646 reads
646 reads

Introduction to Git: Basic Commands Everyone Should Know

by AbramFebruary 3rd, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In the previous article, we talked about the basic concepts of Git every developer should know. In this article, I will be explaining to you what Git is, and the basic commands to get you up and running. Let’s get started!

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Introduction to Git: Basic Commands Everyone Should Know
Abram HackerNoon profile picture

In the previous article, we talked about the basic concepts of Git every developer should know. In this article, I will be explaining to you what Git is, and the basic commands to get you up and running. Let’s get started!

Understanding Git Workflow

Git is the most used open-source VCS (Version control system) that allows you to track changes made to files. Companies and programmers usually use Git to collaborate on developing software and applications.

A Git project consists of three major sections: the working directory, the staging area, and the git directory.

The working directory is where you add, delete, and edit the files. Then, the changes are stages (indexed) in the staging area. After you commit your changes, the snapshot of the changes will be saved into the git directory.

Everyone can use Git as it is available for LinuxWindows, and even Mac. The software may have a steep learning curve, but there are lots of tutorials ready to help you.

Basic Git Commands

Here are some basic Git commands you need to know:

git init will create a new local Git repository. The Git command git init will create a repository in the current directory.

Alternatively, you can create a repository within a new directory by specifying the project name: git init [project name]

git clone is used to copy a repository. If the repository lies on a remoter server, use: git clone username@host:/path/to/repository

Conversely, run the following basic command to copy a local repository: git clone /path/to/repository

git add is used to add files to the staging area. For example, the basic Git following command will index the temp.txt file: git add <temp.txt>

git commit will create a snapshot of the changes and save it to the git directory: git commit -m "Message to go with commit file here"

git config can be used to set user-specific configuration values like email, username, file format, and so on. To illustrate, the command for setting up an email will look like this: git config --global [email protected]

The -a global flag tells Git that you're going to use that email for all local repositories. If you want a different email for different repositories, use the command: git config --local [email protected]

git status displays the list of changed files together with the files that are yet to be staged or committed: git status

git push is used to send local commits to the master branch of the remote repository. Here's the basic code structure: git push origin <master>. Replace <master> with the branch where you want to push your changes when you're not intending to push to the master branch.

git checkout creates a branch and helps you to navigate between them. For example, the following basic command creates a new branch and automatically switches you to it: git checkout -b <branch-name>

To switch from one branch to another, simply use: git checkout <branch-name>

git remote lets you view all remote repositories. The following command will list all connections along with their URLs: git remote –v

To connect the local repository to a remote server, use the command below: git remote add origin <host-or-remoteURL>

Meanwhile, the following command will delete a connection to a specified remote repository: git remote rm <name-of-the-repository>

git branch will list, create, or delete branches. For instance, if you want to list all the branches present in the repository, the command should look like this: git branch

If you want to delete a branch, use: git branch –d <branch-name>

git pull merges all the changes present in the remote repository to the local working directory: git pull

git merge is used to merge a branch into the active one: git merge <branch-name>

git diff lists down conflicts. To view conflicts against the base file, use: git diff --base <file-name>

The following basic command is used to view the conflicts between branches before merging them: git diff <source-branch> <target-branch>

To list down all the present conflicts, use: git diff

git log is used to see the repository’s history by listing certain commit’s details. Running the command will get you an output that looks like this: commit 15f4b6c44b3c8344c Author: Israel Abraham <[email protected]> Date: Wed Jan 20 09:44:29 2021 -0600

git reset command will reset the index and the working directory to the last git commit’s state: git reset --hard HEAD

git rm can be used to remove files from the index and the working directory: git rm filename.txt

git stash command will temporarily save the changes that are not ready to be committed. That way, you can go back to that project later on: git stash

git show is a command used to view information about any git object: git show

git fetch allows users to fetch all objects from the remote repository that don’t currently reside in the local working directory: git fetch origin

git ls-tree allows you to view a tree object along with the name, the mode of each item, and the blob’s SHA-1 value. Let’s say you want to see the HEAD, use: git ls-tree HEAD

git cat-file is used to view the type and the size information of a repository object. Use the -p option along with the object’s SHA-1 value to view the information of a specific object, for example: git cat-file –p d670460b4b4aec


If you are just starting with Git, it can be hard to remember even the basic commands. I recommend you write these commands on a sticky note and paste it somewhere you can always take a glance, or write the commands on your mobile phone notepad.

Practice these commands and make the most of your developing skills! Happy Coding.