DevBolt

Git Cheat Sheet

Interactive reference for 73 Git commands organized by category. Search, copy, and click “Build” to construct commands visually in the Git Command Builder.

Showing 73 of 73 commands

⚙️ Setup & Config

git init
Build

Initialize a new Git repository in the current directory

$ git init
Initialized empty Git repository in /project/.git/
git clone <url>
Build

Clone a remote repository to your local machine

$ git clone https://github.com/user/repo.git
Cloning into 'repo'...
git config user.name "<name>"

Set your name for commits in this repository

$ git config user.name "Jane Doe"
git config user.email "<email>"

Set your email for commits in this repository

$ git config user.email "jane@example.com"
git config --list

List all Git configuration settings

$ git config --list
user.name=Jane Doe
user.email=jane@example.com
core.editor=vim
git config --global core.editor "<editor>"

Set the default text editor for Git globally

$ git config --global core.editor "code --wait"
git config --global init.defaultBranch <name>

Set the default branch name for new repositories

$ git config --global init.defaultBranch main
git remote add <name> <url>
Build

Add a new remote repository connection

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

📦 Staging & Commits

git status

Show the working tree status — staged, unstaged, and untracked files

$ git status
On branch main
Changes not staged for commit:
  modified:   src/index.ts
git add <file>
Build

Stage a specific file for the next commit

$ git add src/index.ts
git add -A

Stage all changes (new, modified, and deleted files)

$ git add -A
# Stages everything in the entire working tree
git add -p

Interactively stage hunks of changes — review each change before staging

$ git add -p
Stage this hunk [y,n,q,a,d,s,e,?]? y
git commit -m "<message>"
Build

Create a commit with a message describing the changes

$ git commit -m "feat: add user authentication"
[main a1b2c3d] feat: add user authentication
git commit --amend
Build

Modify the most recent commit (message or content)

$ git commit --amend
# Opens editor to modify the last commit message
git diff

Show unstaged changes between working directory and index

$ git diff
-const x = 1;
+const x = 2;
git diff --staged

Show changes staged for the next commit

$ git diff --staged
# Shows only changes that have been git add'd
git reset HEAD <file>

Unstage a file while keeping changes in the working directory

$ git reset HEAD src/index.ts
Unstaged changes after reset:
M  src/index.ts
git rm --cached <file>

Remove a file from staging/tracking without deleting it from disk

$ git rm --cached .env
rm '.env'
# File stays on disk but is untracked

🌿 Branching

git branch

List all local branches; the current branch is highlighted with *

$ git branch
* main
  feature/login
  bugfix/header
git branch <name>
Build

Create a new branch (does not switch to it)

$ git branch feature/auth
# Branch created but still on current branch
git branch -d <name>
Build

Delete a branch that has been fully merged

$ git branch -d feature/auth
Deleted branch feature/auth (was a1b2c3d).
git branch -D <name>Destructive
Build

Force-delete a branch regardless of merge status

$ git branch -D experiment
Deleted branch experiment (was e4f5g6h).
git checkout <branch>
Build

Switch to an existing branch (legacy command)

$ git checkout main
Switched to branch 'main'
git checkout -b <name>
Build

Create a new branch and switch to it immediately

$ git checkout -b feature/auth
Switched to a new branch 'feature/auth'
git switch <branch>

Switch to an existing branch (modern replacement for checkout)

$ git switch main
Switched to branch 'main'
git switch -c <name>

Create a new branch and switch to it (modern syntax)

$ git switch -c feature/auth
Switched to a new branch 'feature/auth'

🔀 Merging & Rebasing

git merge <branch>
Build

Merge the specified branch into the current branch

$ git merge feature/auth
Merge made by the 'ort' strategy.
 2 files changed, 45 insertions(+)
git merge --no-ff <branch>
Build

Merge with a merge commit even if fast-forward is possible

$ git merge --no-ff feature/auth
Merge made by the 'ort' strategy.
git merge --abort

Abort a merge in progress and restore the pre-merge state

$ git merge --abort
# Working directory restored to state before merge
git rebase <branch>
Build

Replay current branch commits on top of the specified branch

$ git rebase main
Successfully rebased and updated refs/heads/feature.
git rebase --onto <new> <old> <branch>

Rebase a range of commits onto a new base

$ git rebase --onto main server client
# Replays client commits (since server) onto main
git rebase --abort

Abort a rebase in progress and restore the original state

$ git rebase --abort
# Returns to the state before rebase started
git cherry-pick <commit>
Build

Apply a specific commit from another branch to the current branch

$ git cherry-pick a1b2c3d
[main d4e5f6g] feat: add login
 1 file changed, 12 insertions(+)
git cherry-pick --no-commit <commit>

Apply a commit's changes without creating a new commit

$ git cherry-pick --no-commit a1b2c3d
# Changes applied to working directory, not committed

☁️ Remote

git remote -v

List all remote connections with their URLs

$ git remote -v
origin  https://github.com/user/repo.git (fetch)
origin  https://github.com/user/repo.git (push)
git fetch
Build

Download objects and refs from the default remote without merging

$ git fetch
remote: Enumerating objects: 5, done.
From https://github.com/user/repo
git fetch --all

Fetch from all configured remote repositories

$ git fetch --all
Fetching origin
Fetching upstream
git pull
Build

Fetch from remote and merge into the current branch

$ git pull
Updating a1b2c3d..d4e5f6g
Fast-forward
 2 files changed, 10 insertions(+)
git pull --rebase
Build

Fetch and rebase (instead of merge) the current branch on top of remote

$ git pull --rebase
Successfully rebased and updated refs/heads/main.
git push
Build

Upload local commits to the default remote branch

$ git push
To https://github.com/user/repo.git
   a1b2c3d..d4e5f6g  main -> main
git push -u origin <branch>
Build

Push a branch and set the upstream tracking reference

$ git push -u origin feature/auth
Branch 'feature/auth' set up to track 'origin/feature/auth'.
git push --force-with-leaseDestructive
Build

Force-push only if the remote branch matches your local expectation

$ git push --force-with-lease
# Safer than --force: fails if someone else pushed

📋 Stash

git stash
Build

Temporarily save uncommitted changes and clean the working directory

$ git stash
Saved working directory and index state WIP on main: a1b2c3d
git stash save "<message>"
Build

Stash changes with a descriptive message for easy identification

$ git stash save "WIP: auth refactor"
Saved working directory and index state On main: WIP: auth refactor
git stash list

List all stashed change sets

$ git stash list
stash@{0}: WIP on main: a1b2c3d
stash@{1}: On main: WIP: auth refactor
git stash pop
Build

Apply the most recent stash and remove it from the stash list

$ git stash pop
On branch main
Changes not staged for commit:
  modified:   src/index.ts
Dropped refs/stash@{0}
git stash apply
Build

Apply the most recent stash but keep it in the stash list

$ git stash apply
# Changes restored, stash entry preserved
git stash drop

Remove the most recent stash entry without applying it

$ git stash drop
Dropped refs/stash@{0} (a1b2c3d4e5f6g7h8)

🔍 Inspection & Comparison

git log

Show the commit history for the current branch

$ git log
commit a1b2c3d (HEAD -> main)
Author: Jane Doe
Date:   Mon Mar 19

    feat: add auth
git log --oneline --graph

Compact commit history with ASCII branch graph

$ git log --oneline --graph
* d4e5f6g (HEAD -> main) merge feature
|\  
| * a1b2c3d feat: add login
|/  
* 7h8i9j0 initial commit
git log -p

Show commit history with the full diff for each commit

$ git log -p
commit a1b2c3d
--- a/index.ts
+++ b/index.ts
+const auth = true;
git show <commit>

Display details and diff of a specific commit

$ git show a1b2c3d
commit a1b2c3d
Author: Jane Doe

    feat: add auth

diff --git ...
git diff <branch1>..<branch2>

Show differences between two branches

$ git diff main..feature/auth
-old code
+new code
git blame <file>

Show who last modified each line of a file and when

$ git blame src/index.ts
a1b2c3d (Jane 2026-03-19 10:30) const app = express();
git reflog

Show a log of all reference updates (branches, HEAD moves, resets)

$ git reflog
a1b2c3d HEAD@{0}: commit: feat: add auth
7h8i9j0 HEAD@{1}: checkout: moving to main
git shortlog -sn

Summarize commits by author, sorted by number of contributions

$ git shortlog -sn
    42  Jane Doe
    28  John Smith
     7  Bot

↩️ Undo & Recovery

git reset --soft HEAD~1

Undo the last commit but keep all changes staged

$ git reset --soft HEAD~1
# Commit undone, changes remain in staging area
git reset --mixed HEAD~1

Undo the last commit and unstage changes (default reset mode)

$ git reset --mixed HEAD~1
Unstaged changes after reset:
M  src/index.ts
git reset --hard HEAD~1Destructive

Undo the last commit and discard all changes permanently

$ git reset --hard HEAD~1
HEAD is now at 7h8i9j0 previous commit
git revert <commit>
Build

Create a new commit that undoes the specified commit's changes

$ git revert a1b2c3d
[main e5f6g7h] Revert "feat: add auth"
git checkout -- <file>Destructive

Discard changes in a file, restoring it to the last committed state

$ git checkout -- src/index.ts
# File restored to last committed version
git restore <file>Destructive

Discard working directory changes (modern replacement for checkout --)

$ git restore src/index.ts
# File restored to last committed version
git clean -fdDestructive

Remove all untracked files and directories from the working tree

$ git clean -fd
Removing build/
Removing temp.log
git reflog + git reset --hard <hash>Destructive

Recover a lost commit by finding it in reflog and resetting to it

$ git reflog
a1b2c3d HEAD@{3}: commit: lost work
$ git reset --hard a1b2c3d
HEAD is now at a1b2c3d lost work

🏷️ Tags

git tag

List all tags in the repository

$ git tag
v1.0.0
v1.1.0
v2.0.0
git tag -a <name> -m "<message>"
Build

Create an annotated tag with a message at the current commit

$ git tag -a v1.0.0 -m "Release 1.0.0"
# Annotated tag created with metadata
git push --tags

Push all local tags to the remote repository

$ git push --tags
 * [new tag]  v1.0.0 -> v1.0.0
git tag -d <name>

Delete a tag locally

$ git tag -d v1.0.0
Deleted tag 'v1.0.0' (was a1b2c3d)
git push --delete origin <tag>Destructive

Delete a tag from the remote repository

$ git push --delete origin v1.0.0
To https://github.com/user/repo.git
 - [deleted]  v1.0.0

📦 Submodules

git submodule add <url> <path>

Add a Git repository as a submodule at the specified path

$ git submodule add https://github.com/lib/utils.git vendor/utils
Cloning into 'vendor/utils'...
git submodule init

Initialize submodule configuration from .gitmodules after cloning

$ git submodule init
Submodule 'vendor/utils' registered for path 'vendor/utils'
git submodule update

Fetch and checkout the committed submodule versions

$ git submodule update
Cloning into 'vendor/utils'...
Submodule path 'vendor/utils': checked out 'a1b2c3d'
git submodule update --remote

Update submodules to the latest commit on their tracked remote branch

$ git submodule update --remote
Submodule path 'vendor/utils': checked out 'd4e5f6g'

Related Tools