Git Cheatsheet

A comprehensive reference of commonly used Git commands and keyboard shortcuts to boost your productivity.

CommandDescriptionExampleNote
git init
Initialize a new Git repository
git init my-project
Creates a new .git subdirectory in the current directory
git clone
Clone a repository into a new directory
git clone https://github.com/username/repository.git
Clone the repository to your local machine
git add
Add file contents to the index
git add filename.js
Use "git add ." to add all changes
git commit
Record changes to the repository
git commit -m "Add new feature"
Use -a flag to commit all tracked files
git status
Show the working tree status
git status
Shows modified files in working directory
git diff
Show changes between commits, commit and working tree, etc
git diff
Use git diff --staged to see staged changes
git log
Show commit logs
git log --oneline
Shows the commit history
git branch
List, create, or delete branches
git branch feature-branch
Use -d flag to delete a branch
git checkout
Switch branches or restore working tree files
git checkout feature-branch
Use -b flag to create a new branch and switch to it
git switch
Switch branches (newer alternative to checkout)
git switch feature-branch
Use -c flag to create a new branch and switch to it
git merge
Join two or more development histories together
git merge feature-branch
Merges the specified branch into the current branch
git rebase
Reapply commits on top of another base tip
git rebase main
Rewrites commit history to create a linear history
git tag
Create, list, delete or verify a tag object
git tag v1.0.0
Use -a for annotated tags with messages
git remote
Manage set of tracked repositories
git remote -v
Lists all remote connections
git fetch
Download objects and refs from another repository
git fetch origin
Fetches changes but doesn't integrate them
git pull
Fetch from and integrate with another repository
git pull origin main
Combination of git fetch and git merge
git push
Update remote refs along with associated objects
git push origin main
Push local commits to the remote repository
git show
Show various types of objects
git show commit-hash
Shows the commit data and diffs
git blame
Show what revision last modified each line of a file
git blame filename.js
Shows who changed what and when in a file
git grep
Print lines matching a pattern
git grep "function"
Search for specific strings in tracked files
git diff-tree
Compare content and mode of blobs in trees
git diff-tree HEAD HEAD~1
Shows all changes between two commit points
git restore
Restore working tree files
git restore filename.js
Modern replacement for git checkout to undo changes
git reset
Reset current HEAD to the specified state
git reset HEAD~1
Use --soft to keep changes, --hard to discard changes
git revert
Create a new commit that undoes previous changes
git revert commit-hash
Safer than reset for shared branches
git clean
Remove untracked files from the working tree
git clean -fd
Use -n flag to see what would be removed
git stash
Stash changes in a dirty working directory
git stash save "WIP: feature"
Use git stash pop to apply and remove the stash
git cherry-pick
Apply changes from existing commits
git cherry-pick commit-hash
Applies the changes from specific commits to current branch
git bisect
Use binary search to find a commit that introduced a bug
git bisect start git bisect bad git bisect good commit-hash
Helps locate which commit caused a bug
git worktree
Manage multiple working trees attached to the same repository
git worktree add ../path branch-name
Create a linked working copy to work on multiple branches
git submodule
Initialize, update or inspect submodules
git submodule add https://github.com/username/repo.git
Include other Git repositories within your repository
git reflog
Manage reflog information
git reflog
Shows a log of changes to the HEAD in local repository
Keyboard Shortcuts

Git Keyboard Shortcuts

These keyboard shortcuts work in various Git environments and can boost your productivity.

ShortcutDescription
TabAutocomplete Git commands, branch names, and file paths
Ctrl + RSearch through command history
/ Navigate through previously used commands
Ctrl + LClear the terminal screen
Ctrl + CCancel current command or operation
qExit from log, diff, or help pages
Pro Tips

Git Pro Tips

Custom Git Configuration

Setup global .gitconfig for personalized experience:

[user]
  name = Your Name
  email = [email protected]
[core]
  editor = code --wait
  autocrlf = input
[color]
  ui = auto
[pull]
  rebase = true