Git Cheatsheet

This cheatsheet provides a comprehensive and practical reference for common Git commands. It covers basic usage, branch management, remote operations, undo actions, advanced command combos, and more. Use it to boost your productivity and master version control workflows.

Basic Commands

git init
Initialize a new Git repository
git clone [url]
Clone a remote repository
git add [file]
Add file to staging area
git add .
Add all files in the current directory
git status
Show working tree status
git commit -m 'msg'
Commit staged changes with a message
git commit --amend
Amend the last commit
git config --global user.name 'Name'
Set global username
git config --global user.email '[email protected]'
Set global email
git config --list
List all config settings
git rm [file]
Remove file from working directory and staging area
git mv [old] [new]
Rename or move a file or directory
git help [command]
Show help for a command

Branch Operations

git branch
List all local branches
git branch [name]
Create a new branch
git checkout [branch]
Switch to specified branch
git switch [branch]
Switch branches (modern)
git checkout -b [name]
Create and switch to a new branch
git merge [branch]
Merge specified branch into current branch
git branch -d [branch]
Delete a branch
git branch -D [branch]
Force delete a branch
git branch -a
List all branches (local and remote)
git branch -r
List remote branches
git stash
Stash changes in a dirty working directory
git stash pop
Apply and remove the latest stash
git stash list
List all stashes

Remote Operations

git remote add origin [url]
Add a remote repository
git remote -v
View remote repository information
git fetch
Fetch updates from remote
git pull
Fetch and merge changes from remote
git pull origin [branch]
Pull updates from remote branch
git push
Push changes to remote repository
git push -u origin [branch]
Push branch and set upstream
git push origin --delete [branch]
Delete remote branch
git clone --depth 1 [url]
Shallow clone a repository
git remote show origin
Show information about the remote 'origin'

Undo Operations

git reset [file]
Unstage changes
git checkout -- [file]
Discard working directory changes
git restore [file]
Restore file to last commit (modern)
git revert [commit]
Revert specified commit
git reset --hard [commit]
Reset to specified commit
git clean -fd
Remove untracked files and directories
git reflog
Show history of HEAD and branch references
git cherry-pick [commit]
Apply the changes introduced by some existing commits

View Information

git log
View commit history
git log --oneline
View concise commit history
git log --graph --oneline --all
View branch history as a graph
git diff
View unstaged changes
git diff --staged
View staged changes
git show [commit]
View detailed commit information
git blame [file]
Show who changed what and when in a file
git shortlog -sn
Show commit count per author
git describe --tags
Show the most recent tag reachable from a commit
git tag
List all tags

Staging & Index

git stash
Stash changes in a dirty working directory
git stash pop
Apply and remove the latest stash
git stash list
List all stashes
git stash apply [stash]
Apply a specific stash
git stash drop [stash]
Remove a specific stash
git reset HEAD~1
Uncommit the last commit, keep changes staged
git reset --soft HEAD~1
Uncommit the last commit, keep changes in working directory
git reset --hard HEAD~1
Uncommit the last commit, discard changes

Tagging

git tag [name]
Create a new tag
git tag
List all tags
git tag -d [name]
Delete a tag
git push origin [tag]
Push a tag to remote
git push origin --tags
Push all tags to remote
git checkout [tag]
Switch to a tag

Command Combos

git log --oneline | head -10
Show the last 10 commits in one line each
git diff master..feature | less
View differences between branches with paging
git branch | grep 'feature'
List all branches containing 'feature'
git log --author='Alice' --since='1 week ago'
Show commits by Alice in the last week
git show $(git rev-parse HEAD^)
Show the previous commit
git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
Custom log format with graph
git diff --name-only master..feature | xargs code
Open all changed files between branches in VS Code
git ls-files | xargs wc -l | sort -nr
Count and sort lines of code in all tracked files
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/ | head
List recently updated branches
git log --stat --since='last month'
Show commit stats for the last month
git log --author=$(git config user.name)
Show commits by the current user
git diff --stat HEAD~3..HEAD
Show stats for the last 3 commits
git grep 'TODO' $(git rev-list --all)
Search for TODO in all commits

Categories

  • Basic Commands

    Essential commands for initializing, configuring, and making basic changes in a Git repository.

  • Branch Operations

    Commands for creating, switching, merging, and deleting branches.

  • Remote Operations

    Commands for working with remote repositories, including pushing, pulling, and fetching.

  • Undo Operations

    Commands for reverting changes, resetting commits, and cleaning up your repository.

  • View Information

    Commands for viewing commit history, diffs, and repository details.

  • Staging & Index

    Commands for managing the staging area and index, including stashing and resetting.

  • Tagging

    Commands for creating, listing, and managing tags.

  • Command Combos

    Powerful multi-step workflows and advanced usage patterns for real-world scenarios.

Features

  • Quick search functionality
  • Organized by categories
  • Clear command descriptions
  • Common and advanced use cases covered
  • Easy to copy commands
  • Responsive design
  • Perfect for quick reference