Git Command Cheat Sheet (40+ Commands with Examples)

Complete Git command cheat sheet with 40+ essential Git commands, syntax, examples, and workflow tables. Learn commonly used Git commands such as git clone, git commit, git branch, git push, git pull, and git reset with practical examples for beginners and advanced developers.

Published

Updated

Read time 13 min read

Reviewed byDeepak Prasad

Git Command Cheat Sheet (40+ Commands with Examples)

Introduction to Git Command Cheat Sheet

Git is one of the most widely used distributed version control systems used by developers to track changes, collaborate on projects, and maintain code history. Whether you are working on a personal project or contributing to a large team repository, understanding the most important Git commands can significantly improve your development workflow.

This Git command cheat sheet provides a quick reference to the most commonly used Git commands along with their syntax and examples. The goal is to help beginners quickly learn Git basics while also giving experienced developers a handy reference for daily development tasks.

In this guide, you will learn how to initialize repositories, stage and commit changes, manage branches, synchronize with remote repositories, and undo changes using commonly used Git commands. For a structured approach, see Git workflow guide.


Quick Git Command Cheat Sheet

This quick Git command cheat sheet summarizes the most frequently used Git commands for everyday development tasks. Use this table as a fast reference when working with repositories, branches, commits, and remote repositories.

Task Command
Initialize a new repository git init
Clone a repository git clone <repository-url>
Check repository status git status
Stage changes git add <file>
Stage all files git add .
Commit changes git commit -m "commit message"
View commit history git log
Create a new branch git branch <branch-name>
Switch branch git checkout <branch-name>
Create and switch branch git checkout -b <branch-name>
Download changes from remote git fetch
Pull latest changes git pull
Push commits to remote git push
Delete a branch git branch -d <branch-name>

These commands cover the most common Git operations performed during daily development workflows.

Most common Git commands quick reference table

The following table provides a quick overview of essential Git commands along with their purpose.

Command Purpose
git init Initialize a new Git repository
git clone Create a local copy of a remote repository
git status Show the current status of files
git add Stage files for commit
git commit Save staged changes to repository history
git branch List or create branches
git checkout Switch branches
git merge Merge changes from another branch
git fetch Download changes from remote repository
git pull Fetch and merge remote changes
git push Upload commits to remote repository
git reset Undo commits or staging changes
git revert Create a commit that reverses previous changes

This table acts as a quick reminder of the commands most frequently used when working with Git.


Git workflow overview (working directory, staging area, repository)

Git workflow overview

The Git workflow revolves around three main areas: Working Directory, Staging Area, and the Local Repository. The diagram above shows how changes move through these stages before they are eventually shared with a remote repository such as GitHub, GitLab, or Bitbucket.

Understanding this workflow is essential because almost every Git command interacts with one of these stages.

Working Directory

The working directory is the location where you actively modify files in your project. When you create, edit, or delete files inside your project folder, those changes occur in the working directory.

At this stage Git can detect file changes but they are not yet part of the repository history. You can check the current state of files using:

bash
git status

This command shows:

  • Modified files
  • New untracked files
  • Files staged for commit

Changes in the working directory must first be added to the staging area before they can be committed.

Staging Area (Index)

The staging area, also called the index, is an intermediate step between the working directory and the repository. It allows you to review and select which changes should be included in the next commit.

You move changes from the working directory to the staging area using the git add command.

bash
git add filename

To stage all files in the current directory:

bash
git add .

The staging area helps developers create clean and meaningful commits by allowing them to group related changes together.

Local Repository

The local repository stores the complete commit history of your project on your machine. Once changes are staged, they can be permanently saved using the git commit command.

bash
git commit -m "Add login feature"

Each commit represents a snapshot of the project at a specific point in time. Git assigns a unique commit hash to every commit, allowing developers to track and revert changes when necessary.

Remote Repository

After committing changes locally, developers often synchronize their repository with a remote server such as GitHub, GitLab, or Bitbucket. This allows teams to collaborate and share code.

Changes are uploaded to the remote repository using:

bash
git push

To download the latest changes from the remote repository:

bash
git pull

Typical Git Workflow

A typical Git workflow follows this sequence:

text
Modify files → git add → git commit → git push
  1. Modify files in the working directory
  2. Stage changes using git add
  3. Save changes to the repository using git commit
  4. Upload commits to the remote repository using git push

This workflow ensures that code changes are tracked, versioned, and safely shared with other developers.


Git Repository Setup Commands

These commands help you initialize a new repository, configure Git settings, and clone existing repositories from remote servers.

Command Example Description
git init git init Initialize a new Git repository
git clone git clone https://github.com/user/repo.git Download a remote repository
git config git config --global user.name "John" Configure Git username or settings

git init

Creates a new Git repository in the current directory.

bash
git init

git clone

Creates a local copy of an existing remote repository.

bash
git clone https://github.com/user/repository.git

git config

Configures global Git settings used for commits.

bash
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Git Staging Commands

Staging commands allow you to select which changes should be included in the next commit.

Command Description
git add file.txt Stage a specific file
git restore --staged file.txt Remove file from staging area
git rm --cached file.txt Unstage file but keep it locally

git add

Adds the file to the staging area so it can be included in the next commit.

bash
git add file.txt

git restore --staged

Removes a file from the staging area without deleting the file.

bash
git restore --staged file.txt

git rm --cached

Removes a tracked file from Git but keeps the file in the working directory.

bash
git rm --cached file.txt

Git Commit Commands

Commit commands record changes permanently in the Git repository.

Command Description
git commit -m "message" Save staged changes
git commit -am "message" Stage and commit modified files
git commit --amend Modify the previous commit

git commit

Creates a new commit containing the staged changes.

bash
git commit -m "Initial commit"

git commit -am

Stages and commits modified files in a single command.

bash
git commit -am "Update README"

git commit --amend

Updates the last commit message or includes additional changes.

bash
git commit --amend -m "Correct commit message"

Git Status and History Commands

These commands help inspect the current state of the repository and view commit history.

Command Description
git status Show repository state
git log Show commit history
git log --oneline Compact commit history
git reflog Show reference history

git status

Displays modified, staged, and untracked files.

bash
git status

git log

Shows detailed commit history including author and timestamps.

bash
git log

git log --oneline

Displays a condensed commit history.

bash
git log --oneline

git reflog

Shows the history of HEAD changes and helps recover lost commits.

bash
git reflog

Git Branch Commands Cheat Sheet

Branch commands allow developers to create, manage, and merge branches during development.

Command Description
git branch List branches
git branch -d feature Delete branch
git checkout feature Switch branch
git switch feature Modern branch switching
git merge feature Merge branch changes

git branch

Lists all local branches.

bash
git branch

git branch -d

Deletes a branch after it has been merged.

bash
git branch -d feature-login

git checkout

Switches to the specified branch.

bash
git checkout feature-login

git switch

Modern alternative to checkout for switching branches.

bash
git switch feature-login

git merge

Combines changes from another branch into the current branch.

bash
git merge feature-login

Git Remote Repository Commands

Remote repository commands allow you to connect your local Git repository to remote servers such as GitHub, GitLab, or Bitbucket. These commands help synchronize code between local and remote repositories.

Command Description
git remote -v View configured remote repositories
git remote add origin URL Add a new remote repository
git remote set-url origin URL Change the remote repository URL
git fetch origin Download changes from remote repository
git pull origin main Fetch and merge remote changes
git push origin main Upload commits to remote repository

git remote

Displays the list of configured remote repositories and their URLs.

bash
git remote -v

git remote add

Adds a new remote repository named origin.

bash
git remote add origin https://github.com/user/repo.git

git remote set-url

Updates the remote repository URL.

bash
git remote set-url origin https://github.com/user/newrepo.git

git fetch

Downloads the latest commits and branches from the remote repository without merging them.

bash
git fetch origin

git pull

Fetches the latest changes and automatically merges them into the current branch.

bash
git pull origin main

git push

Uploads local commits to the remote repository.

bash
git push origin main

Git Undo Commands

Git provides several commands to undo commits, unstage files, or revert changes safely such as git reset and git revert.

Command Description
git reset HEAD file.txt Unstage a file
git reset --soft HEAD~1 Undo commit but keep staged changes
git reset --hard HEAD~1 Remove commit and discard changes
git revert commit-hash Create a new commit that reverses changes
git restore file.txt Restore file to last committed state

git reset

Removes a file from the staging area.

bash
git reset HEAD file.txt

git reset --soft

Moves HEAD back one commit but keeps changes staged.

bash
git reset --soft HEAD~1

git reset --hard

Completely removes the last commit and deletes all changes.

bash
git reset --hard HEAD~1

git revert

Creates a new commit that reverses the changes from a previous commit.

bash
git revert commit-hash

git restore

Restores a file to the last committed version.

bash
git restore file.txt

Git Stash Commands

Git stash commands temporarily save uncommitted changes so you can work on another task without committing unfinished work using git stash.

Command Description
git stash Temporarily save changes
git stash list Show saved stashes
git stash apply Apply a stash without removing it
git stash pop Apply and remove stash

git stash

Temporarily stores all uncommitted changes.

bash
git stash

git stash list

Displays all saved stash entries.

bash
git stash list

git stash apply

Applies the most recent stash but keeps it saved.

bash
git stash apply

git stash pop

Applies the latest stash and removes it from the stash list.

bash
git stash pop

Git Tag Commands

Tags are used to mark specific points in a repository history, usually for version releases.

Command Description
git tag List all tags
git tag -a v1.0 -m "Release" Create annotated tag
git push --tags Push tags to remote repository

git tag

Lists all existing tags.

bash
git tag

git tag -a

Creates an annotated tag with a message.

bash
git tag -a v1.0 -m "First release"

git push --tags

Pushes all local tags to the remote repository.

bash
git push --tags

Git Debugging and Inspection Commands

These commands help inspect repository history, identify issues, and debug changes using git reflog and git blame.

Command Description
git reflog Show reference history
git bisect start Find buggy commit
git blame file.txt Show line-by-line author info
git show commit-hash Display commit details

git reflog

Shows the history of HEAD changes and helps recover lost commits.

bash
git reflog

git bisect

Helps locate the commit that introduced a bug using binary search.

bash
git bisect start

git blame

Displays who last modified each line in a file.

bash
git blame file.txt

git show

Shows detailed information about a specific commit.

bash
git show commit-hash

Git Bash Command Cheat Sheet

Git Bash provides a Unix-like command-line interface for Windows users to run Git commands and other shell utilities. It allows developers to interact with repositories, execute Git workflows, and automate development tasks directly from the terminal.

Command Description
pwd Display current working directory
ls List files and folders
cd project Change directory
mkdir project Create new directory
rm file.txt Delete file
clear Clear terminal screen
git status Check repository state

Most useful Git Bash commands

Git Bash supports both Git commands and standard shell commands, allowing developers to navigate directories and manage repositories efficiently.

bash
pwd
ls
cd project-folder
git status
git branch

These commands help you navigate the project directory and check repository status.

Common Git Bash workflow examples

A typical workflow in Git Bash may look like this:

bash
git clone https://github.com/user/project.git
cd project
git checkout -b feature-login
git add .
git commit -m "Add login feature"
git push origin feature-login

This workflow clones a repository, creates a new branch, commits changes, and pushes them to the remote repository.


Common Git Flags Cheat Sheet

Git commands support various flags that modify their behavior. Understanding these flags helps you perform operations more efficiently.

Flag Command Example Description
-m git commit -m "message" Add commit message
-a git commit -a Automatically stage modified files
-b git checkout -b branch Create and switch branch
-d git branch -d branch Delete branch
-f git push -f Force push commits
--amend git commit --amend Modify last commit
--oneline git log --oneline Show condensed commit history

Git Workflow Quick Reference Table

The Git WorkFlow typically follows a sequence of commands that move changes from the working directory to the remote repository.

Step Command Description
Clone repository git clone URL Download repository
Check status git status View modified files
Stage changes git add file Add file to staging
Commit changes git commit -m "msg" Save snapshot
Push changes git push origin branch Upload commits

Complete Git workflow commands in order

The most common Git workflow includes the following steps:

bash
git clone repository-url
git status
git add .
git commit -m "Update project"
git push origin main

Clone → Stage → Commit → Push workflow

The typical workflow used by developers is:

text
Clone repository
Modify files
git add
git commit
git push

This sequence ensures that changes are tracked locally before being shared with other collaborators.


Git Command Examples for Daily Development

Developers use Git commands repeatedly in daily workflows such as creating branches, updating repositories, and resolving conflicts.

Task Command Example Description
Create new branch git checkout -b feature Start new feature development
Pull latest changes git pull origin main Sync with remote repository
Push branch git push origin feature Upload feature branch
Merge branch git merge feature Combine branch changes

Create new feature branch workflow

Developers often create feature branches to work on new functionality.

bash
git checkout -b feature-login
git add .
git commit -m "Add login feature"
git push origin feature-login

This keeps development isolated from the main branch.

Pull latest changes from remote

Before starting new work, developers usually update their local repository.

bash
git pull origin main

This ensures your local branch contains the latest commits from the remote repository.

Resolve merge conflicts

Merge conflicts occur when two branches modify the same file. After resolving conflicts manually, you can commit the changes.

bash
git add resolved-file
git commit -m "Resolve merge conflict"

Summary

This Git command cheat sheet covered the most commonly used Git commands required for daily development workflows. You learned how to initialize repositories, manage branches, stage and commit changes, interact with remote repositories, undo commits, and inspect repository history.

By understanding these commands and workflows, developers can efficiently track changes, collaborate with teams, and maintain version control across projects. Keeping this cheat sheet handy can help you quickly recall commands and streamline your development process.


Official Documentation

Steve Alila

Specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, …