Table of Contents
What Is Git and Why Use It?

Git is the most widely used distributed version control in the industry. While working on a project collaboration is the key git can easily create and maintain multiple branches allowing different people to work on the same project.
Git is easily available in an open-source environment(free) or a dedicated Git instance for your organization(paid). whatever the case it follows the same strategies and workflows while working.
Git provides flexibility, efficiency, and reliability while working on a project and is one less thing to worry about.
In this post, we will discuss common git commands for beginners for the development and merging of the code which eventually helps in building and deploying the software applications
Basic Git Commands Every Developer Should Know
1. Setting Up a New Git Repository
Git Init
Git Init is an acronym for git initialize. It can be used within an empty directory or a project with other version control. Usually, Git Init is the first command that will be used to set the git ecosystem and for the availability of different git commands.
cd /path/to/code
git init
# Initializing repository at a given path
git init <directory>
# Initializing repository with a given template
git init <directory> --template=<template_directory>
Once git init is executed a .git subdirectory is added to the current directory.
2. Cloning an Existing Repository
Git Clone
Git Clone copies or clones of a given repository. Git clone is the most commonly used git commands, it creates the working copy from a remote repository or a local filesystem.
It also creates a remote connection often known as ‘origin‘ pointing to the original repository.
# Cloning from a remote repository
git clone ssh://test@example.com/path/to/my-project.git
# start with the Devlopment
# Cloning a specific branch
git clone --branch<branchName> <repoName>
# Cloning a specific tag
git clone --branch <tag> <repo>
3. Configuring Git Settings
Git Config
The Git Config command is used to read and write different configurations.
# Reading the user email
git config user.email
There are three different levels at which configurations can be read and written.
- Local – By default git config will write and read to local configuration if no config level is specified. All local config values are stored inside the repository directory /.git/config.
- Global – Global Configuration is a user-level configuration. it will be applied to the user’s home directory C://Users//.gitconfig
- System – System configuration will be applied at the machine level for all users. Config data will be stored at C:\Programdata\Git\config
# Writing a Value at global level
git config --global user.email "test@example.com"
Saving and Managing Changes
4. Committing Code Changes
Git Commit
Git commit captures the changes from your current repository to your ‘origin‘ repository(remote or local). It will assign alphanumeric codes for your changes before you push changes to ‘origin’.
The git commit command is used with git add and git push.
# Commiting all staged Changes
git commit
#Commit changes to all the tracked files
git commit -a
#Commit with a message
git commit -m "commit message"
5. Comparing Code Differences
Git Diff
Git Diff will output the changes between git sources as files, commits, and branches.
# Git diff between the file and HEAD of the origin
git diff HEAD ./path/to/file
# Git diff between two commits
git diff <commit1> <commit2>
# Git diff between two branches(devlop & feature)
git diff devlop feature
6. Temporarily Stashing Changes
Git Stash
The git stash command will temporarily shelve the changes from your working directory. These changes can be applied and popped to the current branch or any other branch.
# Shelving or stashing current changes
git stash
# Popping you changes from stash and applying back to current repository
git stash pop
# Re-applying stash to the repository without popping it from stash
git stash apply
# Listing multiple stashs
git stash list
# Popping a particluar stash from the list
git stash pop stash@{2}
# Cleaning a particlular stash
git stash clear stash@{2}
# Cleaning all stashes
git stash clear
Inspecting and Reviewing Code
7. Tagging Releases
Git Tag
Git Tags are the reference points in the Git history. tags are used to capture a release of a particular version.
Tags can help create different release versions of a repository and track what changes are deployed on a particular environment or machine.
# Creating a git tag
git tag <tagname>
# Listing all tags
git tag
# Checking out repo at a tag(v1.4)
git checkout v1.4
# Deleting tag
git tag -d <tagname>
8. Identifying Code Changes
Git Blame
The Git Blame command checks the author’s metadata to specific committed lines in a file. It is mostly used while debugging the code.
# Full Blame output for a file
git blame <filename>
# Reading Blame with line 1,N
git blame -L 1,N <filename>
Undoing and Reverting Changes
9. Reverting Commits
Git Revert
The Git Revert command will take the commit, inverse the changes from that commit, and create a new revert commit. This new revert commit will be the head of the branch. Git revert will not change the project history and it can be applied to any commit in the history.
# Reverting the latest commit
git revert HEAD
10. Resetting the Repository
Git Reset
The Git reset Command will reset the head and current branch pointers to the specified commit. It will remove the commits done after a given commit and will update the git history which will not be done in for revert.
# Resetting to a Particular Commit
git reset <commit>
The git reset command will not used often in enterprise setup as it can change the git history by moving head/branch pointers. It also needs elevated privileges to perform these kinds of operations which will not be the case.
It also has three arguments for the scope of changes as –soft, –mixed,
–hard.
11. Removing Tracked Files
Git rm
The Git rm command removes tracked files from the git Index.
# Removing file from Git Tracking
git rm <fileName>
# Removing multiple files from GIT
git rm <fileName1> <fileName2> <fileName3>
#Removing fileswith wildcard
git rm ~./directory/*
Rewriting Git History
12. Rebasing Branches
Git Rebase
The Git rebase commands move or combine a sequence of commits into a new commit. It is the most used git commands while merging changes to the repository

In the above example, you have started working on a feature branch by creating it from the main branch. But as the feature branch progressed main branch also progressed with certain commits. Once your changes are done you want to merge them back to the main branch but without rebasing your branch with the new commits in the main it will give you merge errors. Hence it would help if you rebased so that it would look like you have all the changes from the main branch and then you can merge your changes via a merge commit.
Conclusion
Mastering Git Commands is of Utmost importance in you developer journey. It helps you to collaborate, manage code versions and effective use of git as version control system.
Leave a Reply