Git Command Cheat Sheet (40+ Commands with Examples)

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.

TaskCommand
Initialize a new repositorygit init
Clone a repositorygit clone <repository-url>
Check repository statusgit status
Stage changesgit add <file>
Stage all filesgit add .
Commit changesgit commit -m "commit message"
View commit historygit log
Create a new branchgit branch <branch-name>
Switch branchgit checkout <branch-name>
Create and switch branchgit checkout -b <branch-name>
Download changes from remotegit fetch
Pull latest changesgit pull
Push commits to remotegit push
Delete a branchgit 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.

CommandPurpose
git initInitialize a new Git repository
git cloneCreate a local copy of a remote repository
git statusShow the current status of files
git addStage files for commit
git commitSave staged changes to repository history
git branchList or create branches
git checkoutSwitch branches
git mergeMerge changes from another branch
git fetchDownload changes from remote repository
git pullFetch and merge remote changes
git pushUpload commits to remote repository
git resetUndo commits or staging changes
git revertCreate 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.

CommandExampleDescription
git initgit initInitialize a new Git repository
git clonegit clone https://github.com/user/repo.gitDownload a remote repository
git configgit 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.

CommandDescription
git add file.txtStage a specific file
git restore --staged file.txtRemove file from staging area
git rm --cached file.txtUnstage 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.

CommandDescription
git commit -m "message"Save staged changes
git commit -am "message"Stage and commit modified files
git commit --amendModify 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.

CommandDescription
git statusShow repository state
git logShow commit history
git log --onelineCompact commit history
git reflogShow 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.

CommandDescription
git branchList branches
git branch -d featureDelete branch
git checkout featureSwitch branch
git switch featureModern branch switching
git merge featureMerge 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.

CommandDescription
git remote -vView configured remote repositories
git remote add origin URLAdd a new remote repository
git remote set-url origin URLChange the remote repository URL
git fetch originDownload changes from remote repository
git pull origin mainFetch and merge remote changes
git push origin mainUpload 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.

CommandDescription
git reset HEAD file.txtUnstage a file
git reset --soft HEAD~1Undo commit but keep staged changes
git reset --hard HEAD~1Remove commit and discard changes
git revert commit-hashCreate a new commit that reverses changes
git restore file.txtRestore 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.

CommandDescription
git stashTemporarily save changes
git stash listShow saved stashes
git stash applyApply a stash without removing it
git stash popApply 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.

CommandDescription
git tagList all tags
git tag -a v1.0 -m "Release"Create annotated tag
git push --tagsPush 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.

CommandDescription
git reflogShow reference history
git bisect startFind buggy commit
git blame file.txtShow line-by-line author info
git show commit-hashDisplay 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.

CommandDescription
pwdDisplay current working directory
lsList files and folders
cd projectChange directory
mkdir projectCreate new directory
rm file.txtDelete file
clearClear terminal screen
git statusCheck 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.

FlagCommand ExampleDescription
-mgit commit -m "message"Add commit message
-agit commit -aAutomatically stage modified files
-bgit checkout -b branchCreate and switch branch
-dgit branch -d branchDelete branch
-fgit push -fForce push commits
--amendgit commit --amendModify last commit
--onelinegit log --onelineShow 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.

StepCommandDescription
Clone repositorygit clone URLDownload repository
Check statusgit statusView modified files
Stage changesgit add fileAdd file to staging
Commit changesgit commit -m "msg"Save snapshot
Push changesgit push origin branchUpload 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.

TaskCommand ExampleDescription
Create new branchgit checkout -b featureStart new feature development
Pull latest changesgit pull origin mainSync with remote repository
Push branchgit push origin featureUpload feature branch
Merge branchgit merge featureCombine 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

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, and data presentation using Matplotlib and Plotly.