Skip to main content

Git Cheatsheet

Searchable reference of 106 essential git commands organized by workflow. Find what you need fast.

106 commands found

Setup & Config

Set global username

git config --global user.name "Your Name"

Set global email

git config --global user.email "you@example.com"

List all config settings

git config --list

Set default branch name

git config --global init.defaultBranch main

Set editor for commit messages

git config --global core.editor "code --wait"

Enable colored output

git config --global color.ui auto

Set credential caching

git config --global credential.helper cache

Create a global alias

git config --global alias.co checkout

Creating & Cloning

Initialize a new repository

git init

Initialize a bare repository

git init --bare

Clone a repository

git clone <url>

Clone into a specific folder

git clone <url> <directory>

Shallow clone (latest commit only)

git clone --depth 1 <url>

Clone a specific branch

git clone -b <branch> <url>

Staging & Committing

Stage all changes

git add .

Stage all tracked and untracked files

git add -A

Interactively stage hunks

git add -p

Stage a specific file

git add <file>

Commit with a message

git commit -m "message"

Amend last commit message

git commit --amend

Amend commit without changing message

git commit --amend --no-edit

Show working tree status

git status

Show short status

git status -s

Show unstaged changes

git diff

Show staged changes

git diff --staged

Diff against last commit

git diff HEAD~1

Branching

List local branches

git branch

List all branches (local + remote)

git branch -a

Create a new branch

git branch <branch-name>

Delete a branch (safe)

git branch -d <branch-name>

Force delete a branch

git branch -D <branch-name>

Rename current branch

git branch -m <new-name>

Switch to a branch

git checkout <branch-name>

Create and switch to new branch

git checkout -b <branch-name>

Switch to previous branch

git checkout -

Switch branch (modern)

git switch <branch-name>

Create and switch branch (modern)

git switch -c <branch-name>

Merging & Rebasing

Merge a branch into current

git merge <branch>

Merge with no fast-forward

git merge --no-ff <branch>

Abort a merge in progress

git merge --abort

Squash merge (combine commits)

git merge --squash <branch>

Rebase current branch onto another

git rebase <branch>

Interactive rebase (last N commits)

git rebase -i HEAD~<n>

Rebase onto a specific base

git rebase --onto <new-base> <old-base> <branch>

Abort a rebase in progress

git rebase --abort

Continue rebase after resolving conflicts

git rebase --continue

Cherry-pick a commit

git cherry-pick <commit-hash>

Undoing Changes

Soft reset (keep changes staged)

git reset --soft HEAD~1

Mixed reset (unstage changes)

git reset --mixed HEAD~1

Hard reset (discard all changes)

git reset --hard HEAD~1

Reset to specific commit

git reset --hard <commit-hash>

Revert a commit (creates new commit)

git revert <commit-hash>

Restore a file to last commit

git restore <file>

Unstage a file

git restore --staged <file>

Discard all unstaged changes

git checkout -- .

Remote & Collaboration

Add a remote repository

git remote add origin <url>

List remotes with URLs

git remote -v

Remove a remote

git remote remove <name>

Change remote URL

git remote set-url origin <new-url>

Fetch from all remotes

git fetch --all

Fetch and prune deleted branches

git fetch --prune

Pull latest changes

git pull

Pull with rebase

git pull --rebase

Push and set upstream

git push -u origin <branch>

Force push safely

git push --force-with-lease

Delete a remote branch

git push --delete origin <branch>

Push all branches

git push --all origin

Stashing

Stash current changes

git stash

Stash with a message

git stash push -m "description"

Apply latest stash

git stash pop

List all stashes

git stash list

Show stash contents

git stash show -p

Apply specific stash without removing

git stash apply stash@{0}

Drop a specific stash

git stash drop stash@{0}

Clear all stashes

git stash clear

Viewing History & Diffs

View commit log

git log

Compact one-line log

git log --oneline

Log with graph visualization

git log --oneline --graph --all

Log with diff patches

git log -p

Commits since a date

git log --since="2024-01-01"

Commits by a specific author

git log --author="name"

Show a specific commit

git show <commit-hash>

Show who changed each line

git blame <file>

Summary of contributors

git shortlog -sn

Search commit messages

git log --grep="keyword"

Tagging

List all tags

git tag

Create an annotated tag

git tag -a v1.0.0 -m "Release v1.0.0"

Create a lightweight tag

git tag v1.0.0

Delete a local tag

git tag -d v1.0.0

Push all tags to remote

git push --tags

Push a specific tag

git push origin v1.0.0

Delete a remote tag

git push --delete origin v1.0.0

Describe current commit with tags

git describe --tags

Advanced & Cleanup

Remove untracked files and dirs

git clean -fd

Dry-run clean (preview)

git clean -n

Garbage collect loose objects

git gc

View reflog (history of HEAD)

git reflog

Start binary search for a bug

git bisect start

Mark current commit as bad

git bisect bad

Mark current commit as good

git bisect good <commit>

End bisect session

git bisect reset

Add a submodule

git submodule add <url> <path>

Initialize submodules after clone

git submodule update --init --recursive

Add a worktree

git worktree add <path> <branch>

List worktrees

git worktree list

Remove a worktree

git worktree remove <path>

Why Use a Git Cheatsheet?

Git is the foundation of modern software development, used by virtually every engineering team for version control, collaboration, and code deployment. While Git's distributed architecture is powerful, its command-line interface has hundreds of commands and flags that even experienced developers need to reference regularly. A well-organized cheatsheet helps you quickly find the right command for branching, merging, rebasing, stashing, and resolving conflicts.

This git cheatsheet is organized by workflow rather than alphabetically, making it faster to find commands when you know what you want to do but not the exact syntax. DevOps engineers use git commands throughout CI/CD pipelines, infrastructure-as-code workflows, and GitOps deployment patterns. Whether you are resolving a merge conflict during code review, cherry-picking a hotfix to a release branch, or bisecting to find a regression, having these commands at your fingertips accelerates your entire development workflow.

Frequently Asked Questions

How to undo the last git commit?

Use git reset --soft HEAD~1 to undo the commit but keep your changes staged, or git reset --mixed HEAD~1 to undo the commit and unstage changes (files remain modified). Use git reset --hard HEAD~1 to completely discard the commit and all changes (destructive). If the commit is already pushed, use git revert HEAD to create a new commit that undoes the changes safely.

What is the difference between git merge and rebase?

git merge creates a new merge commit that combines two branch histories, preserving the complete history. git rebase rewrites commits on top of another branch, creating a linear history without merge commits. Rebase produces a cleaner log but rewrites history (never rebase shared/pushed branches). Many teams use rebase for local feature branches and merge for integrating into main.

How to delete a remote branch?

Use git push origin --delete branch-name to delete a remote branch. To clean up your local references to deleted remote branches, run git fetch --prune. To delete a local branch, use git branch -d branch-name (safe, only if merged) or git branch -D branch-name (force delete regardless of merge status). Always verify you are not deleting a branch that others depend on.