Amit Ghadi
Blogs For Developers

Blogs For Developers

Git Cheat Sheet

Kickstart your knowledge of Git

Git Cheat Sheet
Amit Ghadi's photo
Amit Ghadi

Published on Sep 10, 2021

6 min read

Git is the most used version control system. Git tracks the changes you make to files, so you can track what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing to track changes by multiple people and allow to be merged into one source.

Let's start with

  • Installation
# On Windows 10
# Installing on Linux
$ sudo dnf install git-all
# on a Debian-based distribution, such as Ubuntu, try apt:
$ sudo apt install git-all
# On Mac
#Install Git with Homebrew
$ brew install git
  • Check version

Open a terminal and verify the installation was successful by typing git --version:

$ git --version
  • Configure the Git

Configure your Git username and email using the following commands. These details will be associated with any commits that you create.

#Configure user name. 
$ git config --global "Your Name" 

#Configure user email. 
$ git config --global ""
  • .gitignore

For Git there are three types of files in your working copy.

  1. tracked:- Files which has been previously staged or committed

  2. untracked:-File which has not been staged or committed

  3. ignored:-File which Git has been explicitly told to ignore

.gitignore file is a config file that specifies which files should be ignored from commit.

create .gitignore file in the working directory and add code like bellow.

# ignore all logs
  • Global Git ignore rules

create global Git ignore patterns for all repositories on your local system by setting the Git core.excludesFile property.

#bellow commands 1)create .gitignore file in root of working directory 
#2)set core **.gitignore** file path to **core.excludesFile** property  
$ touch path-of-file/.gitignore
$ git config --global core.excludesFile path-of-file/.gitignore
  • Ignoring a previously committed file
$ echo mysql.log >> .gitignore  
$ git rm --cached mysql.log
$ git commit -m "Start ignoring mysql.log"
  • Committing an ignored file
$ cat .gitignore
$ git add -f mysql.log  
$ git commit -m "Force adding mysql.log"
  • Create & Clone

Create New Repository with git init. you will use this command only once during the initial setup of a new repo. running this command will create a new .git subdirectory in your current working directory. This will also create a new main branch.

$ git init
  • Clone your repository

To create a local copy on your computer and sync between the two locations.

$ git clone
  • git status

The git status command displays the state of the working directory and the staging area.

$ git status
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
  • Add & Remove

git add command adds a change in the working directory to the staging area.

#create an initial commit of the current directory
$ git add . 
#Stage all changes in <file> for the next commit.
$ git add <file>
#Stage all changes in <directory> for the next commit.
$ git add <directory>
#To stage the chunk of file for the next Commite
$ git add -p
  • Delete File

git rm is actually a command that combines the standard shell rm and git add to remove a file from the working directory and track removal to the staging index.

$ git rm <file name>
  • Stashing your work

git stash temporarily save(or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later.

#saves them away for later use
$ git stash
#stash your untracked files
$ git stash -u
#list all Stash
$ git stash list
#Re-applying your stashed changes
$ git stash pop

#Cleaning up your stash
#delete Specific stash
$ git stash drop stash@{1}
#delete all stash
$ git stash clear
  • Commit & Synchronize

The git commit command takes a snapshot of the project's currently staged changes.

#takes a snapshot of the project's currently staged changes
$ git commit -m "commit message"
#creates a commit of all the staged changes and takes an inline commit message.
$ git commit -am "commit message"
  • Push the changes

Use to push the changes and file to a remote server.

$ git push <remote> <branch>
#use to push all the changes 
$ git push origin master
  • Pull the changes

git Pull fetch the changes from a remote server and merge them in local files

#fetch the changes from a remote server and merge them in local files 
$ git pull 
#Fetch all of the branches from the repository. 
$ git fetch <remote>
#Fetch the specific branches from the repository. 
$ git fetch <remote> <branch>
#Fetches all registered remotes and their branches
$ git fetch --all
  • Branching

bellow are the commands for operations related to branch

#List all remote branches
$ git branch -a
#List all branch 
$ git branch
#Create new branch 
$ git branch <branch>
#Create new branch
$ git checkout -b <branch>
#Switch to any branch eg. master
$ git checkout master
#Delete any branch
$ git branch -d <branch>
#Force delete the specified branch
$ git branch -D <branch>
#Rename the current branch to <branch>
$ git branch -m <branch>

#merge the changes to the master branch 
$ git checkout master
$ git merge <changed branch >

#view changes between two branches
$ git diff <source branch> <target branch>
  • Tagging

Tagging is generally used to create an index in history that is used for a marked version release (i.e. v1.0.3). A tag is like a branch that does not change. Unlike branches, tags have no further history of commits once created.

#after pushing code to master branch 
#bellow command use to list all tags
$ git tag
#Create new tag till the last changes pushed  
$ git tag -a "tag_name" -m "tag_message"

#**push the tag to remote server **
$ git push origin "tag_name"

#**Checking Out Tags**
$ git checkout v1.4
#Deleting Tags
$ git tag -d v1

#**Tagging Old Commits**
#list all the commits
$ git log --pretty=oneline
#commit id is for commit for which we are creating tag
$ git tag -a v1.2 <commit_id>
  • Restored

use to get a working copy with the latest HEAD

$ git checkout --<file_name>
  • Blaim

    git blame is the use to display author metadata attached to specific committed lines in a file.

$ git blame <file name>
  • Undoing changes
#check the log of commit 
#You can retrieve a list of commit IDs and their associated commit messages using the –oneline flag.
$ git log --oneline
#checkout particular Commit id (get the previous version or commit to local ) 
$ git checkout <Commit id >
Share this