Skip to content
Latest: JSX in React explained clearly without any framework

Git Cheat Sheet – Git Commands for Easy Reference

What Is Git?

Git is a Distributed Version Control System (DVCS) for saving different versions of a file (or set of files)—wherein any version is retrievable at will.

Git makes it easy to record and compare different file versions. Consequently, details about what changed, who changed what, or who initiated an issue are reviewable anytime.

Below are some of the widely used Git commands.

Installing and Updating Git

Below are the commands for installing and updating Git on your system.

DescriptionCommand
Install Githttps://git-scm.com/download
Check Git’s installation location
Terminal window
which git
Check the installed Git version
Terminal window
git --version
Update Git (Windows)
Terminal window
git update-git-for-windows
Update Git (Mac)https://git-scm.com/download/mac

Initializing and Cloning Git

Below are the commands for initializing new local Git repositories or cloning existing Git repositories.

DescriptionCommand
Initialize Git
Terminal window
git init
Clone a Git repository
Terminal window
git clone the-git-repo-url where-to-save-the-cloned-git-folder

Configuring Git

Below are the commands for configuring, reviewing, and updating users’ information for all local Git repositories.

DescriptionCommand
Configure your Git username
Terminal window
git config --global user.name "Your Name"
Configure your Git email
Terminal window
git config --global user.email "[email protected]"
Check your project’s configurations
Terminal window
git config --list
Check your email configuration
Terminal window
git config user.email
Check your username configuration
Terminal window
git config user.name
Update your username configuration
Terminal window
git config user.name "New Name"
Update your email configuration
Terminal window
git config user.email "[email protected]"
Exit the Git configuration spacePress the Q keyboard key

Tracking Changes

Below are the commands for monitoring changes to files or folders.

DescriptionCommand
Check the status of your files
Terminal window
git status
Stage a specific file (or folder)
Terminal window
git add file-or-folder-name
Stage all modified and untracked files
Terminal window
git add -A
Stage the current directory’s modified and untracked files
Terminal window
git add .
Stage all modified files that you’ve committed previously
Terminal window
git add -u
Compare the working directory and staging area’s file versions
Terminal window
git diff
Compare the staging area’s file version with the most recently committed version
Terminal window
git diff --staged

Undoing the Staging of files

Below are the commands for undoing the staging of files.

DescriptionCommand
Undo the staging of a specific file
Terminal window
git reset HEAD file-to-unstage
Undo the staging of all files
Terminal window
git reset

Committing Files to Git

Below are the commands for saving files to the Git repository.

DescriptionCommand
Commit your filesgit commit -m "Write a message about the file(s) you are committing"
Stage and commit at oncegit add file-or-folder-name && git commit -m "Your commit message"
Commit without writing a commit message
Terminal window
git commit --amend --no-edit
Modify a previous commit messageHow to modify a previous commit message
Commit without staging first
Terminal window
git commit -a
Undo a specific commit
Terminal window
git revert commit-to-undo

Viewing Your Commits

Below are the commands for viewing your commits.

DescriptionCommand
See the changes you are about to commit
Terminal window
git commit -v
View your project’s commit history
Terminal window
git log
View a specific number of the project’s commit history (for instance, the last three)
Terminal window
git log -3
Exit the Git console spacePress the Q keyboard key
See all the files currently in the staging area and the Git repository
Terminal window
git ls-files
Check if a specific file or folder is in the Git directory
Terminal window
git ls-files | grep file-or-folder-name

Ignoring Files

Below are the commands for telling Git to ignore specific files.

DescriptionCommand
Create a .gitignore file
Terminal window
touch .gitignore
See an example of a .gitignore fileExample of a .gitignore file
Open Gitignore’s manual page
Terminal window
git help gitignore

Managing Git Branches

Below are the commands for creating, renaming, deleting, and switching between Git branches.

DescriptionCommand
Check your project’s local branches
Terminal window
git branch
Check your project’s remote branches
Terminal window
git branch -a
Create a new branch
Terminal window
git branch new-branch-name
Delete a specific branch
Terminal window
git branch -d branch-to-delete
Rename your project’s HEAD branch
Terminal window
git branch -m branch-new-name
Rename a non-HEAD branch
Terminal window
git branch -m branch-old-name branch-new-name
Switch from one branch to another
Terminal window
git switch branch-name
Switch back to the previous branch
Terminal window
git switch -
Create and switch immediately to a new branch
Terminal window
git switch -c new-branch-name
Switch to a specific commit history
Terminal window
git checkout commit-hash
Merge a specific branch into the HEAD branch
Terminal window
git merge name-of-branch-containing-your-changes
Compare branches
Terminal window
git log first-branch-name..second-branch-name

Sharing Git Repos

Below are the commands for sharing content between local and remote Git repositories.

DescriptionCommand
Link your project’s local and remote repositories

git remote add origin https://github.com/your-username/app-repo-name.git

Confirm the connection between your local and remote repositories
Terminal window
git remote -v
Push your local Git repo to the remote repository
Terminal window
git push -u remote-name branch-name
Confirm the upload of your local Git repo to the remote repositoryGo to your GitHub repository page and refresh the browser.
Undo the pushing of a commit upstream
Terminal window
git revert HEAD
git push remote-name branch-name
Publish your app on GitHub PagesHow to publish your website with GitHub Pages

Deleting Files and Folders

Below are the commands for deleting files and folders from the working directory and Git repository.

DescriptionCommand
Delete a file from the working directory and the Git repository
Terminal window
git rm file-to-delete
Forcefully delete a file from the working directory and the Git repository
Terminal window
git rm -f file-to-delete
Delete a folder from the working directory and the Git repository
Terminal window
git rm -r folder-to-delete
Delete a file only from the staging area—not from the working directory
Terminal window
git rm --cached file-to-delete
Rename a specific file
Terminal window
git mv current-file-name new-file-name
Replace a working directory’s file with its last committed version
Terminal window
git checkout local-file-to-delete-and-replace