Skip to main content

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.

How to Install and Update Git

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

DescriptionCommand
Install Git

https://git-scm.com/download

Check Git's installation location
which git
Check the installed Git version
git --version
Update Git (Windows)
git update-git-for-windows
Update Git (Mac)

https://git-scm.com/download/mac

How to Initialize and Clone Git

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

DescriptionCommand
Initialize Git
git init
Clone a Git repository
git clone the-git-repo-url where-to-save-the-cloned-git-folder
Buy CSS Flexbox book

How to Configure Git

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

DescriptionCommand
Configure your Git username
git config --global user.name "Your Name"
Configure your Git email
git config --global user.email "your-email@address.com"
Check your project's configurations
git config --list
Check your email configuration
git config user.email
Check your username configuration
git config user.name
Update your username configuration
git config user.name "New Name"
Update your email configuration
git config user.email "new-email@address.com"
Exit the Git configuration spacePress the Q keyboard key

How to Track Changes

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

DescriptionCommand
Check the status of your files
git status
Stage a specific file (or folder)
git add file-or-folder-name
note

Replace file-or-folder-name with the file (or directory)'s pathname.

Stage all modified and untracked files

git add -A
note

The -A flag is a shorthand for --all.

Stage the current directory's modified and untracked files
git add .
note

The dot symbol means "current directory."

Stage all modified files that you've committed previously
git add -u
note

The -u flag is a shorthand for --update.

Compare the working directory and staging area's file versions

git diff
Compare the staging area's file version with the most recently committed version
git diff --staged

How to Undo the Staging of files

Below are the commands for undoing the staging of files.

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

How to Commit Files to Git

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

DescriptionCommand
Commit your files
git commit -m "Write a message about the file(s) you are committing"
Stage and commit at once
git add file-or-folder-name && git commit -m "Your commit message"
Commit without writing a commit message
git commit --amend --no-edit
Modify a previous commit message

How to modify a previous commit message

Commit without staging first
git commit -a
Undo a specific commit
git revert commit-to-undo
note
  • Replace commit-to-undo with the commit you wish to undo—for instance, git revert b91ey04.
  • The git revert command does not delete any data. Instead, it creates a new commit that reverses the changes added to the specified commit.

How to View Your Commits

Below are the commands for viewing your commits.

DescriptionCommand
See the changes you are about to commit
git commit -v
View your project's commit history
git log
View a specific number of the project's commit history (for instance, the last three)
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

git ls-files
Check if a specific file or folder is in the Git directory
git ls-files | grep file-or-folder-name
note

Replace file-or-folder-name with the file (or directory)'s pathname.

How to Ignore Files

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

DescriptionCommand
Create a .gitignore file
touch .gitignore
See an example of a .gitignore file

Example of a .gitignore file

Open Gitignore's manual page
git help gitignore
note

The Gitignore manual will open in your default browser.

Rakuten Kobo US

How to Manage Git Branches

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

DescriptionCommand

Check your project's local branches

git branch
note

Git will mark the current branch with an asterisk (*).

Check your project's remote branches

git branch -a
Create a new branch
git branch new-branch-name
Delete a specific branch
git branch -d branch-to-delete

Rename your project's HEAD branch

git branch -m branch-new-name
Rename a non-HEAD branch
git branch -m branch-old-name branch-new-name
Switch from one branch to another
git switch branch-name
Switch back to the previous branch
git switch -
Create and switch immediately to a new branch
git switch -c new-branch-name
Switch to a specific commit history
git checkout commit-hash
note

A commit hash is a long string that follows the word "commit" in a git log command's output.

Merge a specific branch into the HEAD branch

git merge name-of-branch-containing-your-changes
important

Before invoking the merge command, make sure the active branch is the branch you want to merge into. In other words, switch to the branch you wish to update before running the merge command.

Compare branches
git log first-branch-name..second-branch-name
Buy CSS Flexbox book

How to Share 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
note

Replace your-username in the code above with your GitHub username. Likewise, replace app-repo-name with the name of the remote repository you want to connect to.

Confirm the connection between your local and remote repositories
git remote -v
Push your local Git repo to the remote repository
git push -u remote-name branch-name
note
  • Replace remote-name with your remote URL's short name—for instance, origin.
  • Replace branch-name with your local branch's name—for instance, main.
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
git revert HEAD
git push remote-name branch-name
note
  • Replace remote-name with your remote URL's short name—for instance, origin.
  • Replace branch-name with your local branch's name—for instance, main.
Publish your app on GitHub Pages

How to publish your website with GitHub Pages

How to Delete 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

git rm file-to-delete
Forcefully delete a file from the working directory and the Git repository
git rm -f file-to-delete
Delete a folder from the working directory and the Git repository
git rm -r folder-to-delete

Delete a file only from the staging area—not from the working directory

git rm --cached file-to-delete
Rename a specific file
git mv current-file-name new-file-name
Replace a working directory's file with its last committed version
git checkout – local-file-to-delete-and-replace
FREE PDF Git Cheat Sheet!

Get your free PDF Git cheat sheet.

Use it to easily remember the Git commands you need for your projects.

Download Now