Basic Git CLI Commands For Beginners

Basic Git CLI Commands For Beginners

Git is a very popular tool among developers around the world. It is a distributed version control system for tracking file changes in any type of file. It is basically designed for programmers to work together on the same source code at the same time. In this blog, I will discuss the basic CLI commands of git for a beginner to start with. Let's jump right in.

Prerequisites

For this tutorial, I am assuming that git is installed on your machine. If not go to the official git website to download and install it. Also, You have created a git account and configure it properly. Here is the official documentation on how to setup git for the first time. Now that git is installed and configured correctly on your machine. Let's start with the commands.

Git Init

This command is used to initialized an empty git repository in the current directory.

$ git init
# Initialized empty Git repository in /home/wasif/test/git/.git/

Git Status

It gives the status of files changed and those which still need to add or commit. You will get this type of message:

$ git status
# On branch master

# No commits yet

# Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
#    new file:   index.html

# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
#  (use "git restore <file>..." to discard changes in working directory)
#    modified:   index.html */

Git Add

To add one or more files to the staging area.

$ git add <filename>
#to add a particular file

$ git add .
#to add all the untracked files to the staging area

Git Commit

This command is used to commit changes to the head locally not to the remote repository. -m flag is used to give a descriptive message to the commit. So that in the future when someone visits this commit they can understand changes by the commit message.

$ git commit -m "commit message"

#...[master (root-commit) 43450ff] commit message
#1 file changed, 9 insertions(+)
#create mode 100644 index.html

Git log

It lists all the previous commits with details such as commit id , commit message, and date.

$ git log
#commit 43450ff2e9e0e6bab9f77f5a7ac191a17542e7e1 (HEAD -> master)
#Author: Wasif Baliyan <w***b*****@gmail.com>
#Date:   Wed Dec 16 02:14:29 2020 +0530

#    first commit

Git Branch

This command is used to work with the branches. We can create branches to work on a feature and merge it later with the main branch.

$ git branch
#list all the branches

$ git branch <branch-name>
#create new branch

Git Checkout

This command is used to work with the branches. We can create branches to work on a feature and merge it later with the main branch.

$ git checkout <branch-name>
#switch to specific branch

$ git checkout -b <branch-name>
#create new branch and switch to it

$ git checkout -d <branch-name>
#delete branch

Git Clone

This command is used to create a copy of a remote repository on the local machine.

$ git clone git@github.com:<github-username>/<repository-name>
#create a copy of remote repo on the machine

Git Remote

This command is used to add remote origin to a local repository. First create a repository on Github and copy the repo address.

$ git remote add origin git@github.com:<github-username>/<repository-name>
#connect remote repo to local repo

$ git remote -v
#List all currently configured remote repositories

Git Push

Push the changes to the remote repository.

$ git push origin master 
#push all changes in the master branch to remote repo

$ git push origin <branch-name>
#push all changes in the a specific branch to remote repo

Git Pull

This command is used to fetch and merge changes on the remote repository to the local working directory.

$ git pull
#pull changes from remote to the local repo and merge them

Git Merge

This command is used to merge a different branch into your active branch.

$ git merge <branch-name>
#merge the branch-name to the current branch

Conclusion

Git is a very vast topic and it is very difficult to understand in the beginning but as you start to use it on daily basis it becomes very fun to use. These are the basic and most used commands to get started with git.

References

To learn more about Git there are many online resources available to solve git problems, and here are some of them: