Quick reference guide for Git commands and workflows. All your notes are saved locally in your browser.
Display Git version information
git --versionSet up your Git identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
git config --list # View all settingsCreate a new Git repository
git init
git init <directory-name>Copy a repository to your local machine
git clone <repository-url>
git clone <repository-url> <directory-name>
git clone --depth 1 <repository-url> # Shallow cloneAccess Git command documentation
git help
git help <command>
git <command> --helpView the status of your working directory
git status
git status -s # Short formatStage files for commit
git add <file>
git add . # Add all files
git add *.js # Add all JS files
git add -p # Interactive stagingSave staged changes to repository
git commit -m "Commit message"
git commit -am "Commit message" # Add and commit
git commit --amend # Modify last commit
git commit --amend --no-edit # Amend without changing messageRemove files from staging area
git reset <file>
git reset # Unstage all files
git restore --staged <file>Revert working directory changes
git checkout -- <file>
git restore <file>
git checkout . # Discard all changesShow all branches
git branch
git branch -a # Include remote branches
git branch -r # Remote branches onlyCreate a new branch
git branch <branch-name>
git checkout -b <branch-name> # Create and switch
git switch -c <branch-name> # Create and switch (newer)Change to a different branch
git checkout <branch-name>
git switch <branch-name> # Newer commandRemove a branch
git branch -d <branch-name> # Safe delete
git branch -D <branch-name> # Force delete
git push origin --delete <branch-name> # Delete remote branchCombine branches
git merge <branch-name>
git merge --no-ff <branch-name> # No fast-forward
git merge --squash <branch-name> # Squash commitsChange branch name
git branch -m <old-name> <new-name>
git branch -m <new-name> # Rename current branchShow remote repositories
git remote
git remote -v # Show URLsAdd a new remote repository
git remote add <name> <url>
git remote add origin https://github.com/user/repo.gitDownload remote changes without merging
git fetch
git fetch <remote>
git fetch --all # All remotesFetch and merge remote changes
git pull
git pull <remote> <branch>
git pull --rebase # Rebase instead of mergeUpload local commits to remote
git push
git push <remote> <branch>
git push -u origin <branch> # Set upstream
git push --force # Force push (use carefully!)
git push --force-with-lease # Safer force pushDelete a remote connection
git remote remove <name>
git remote rm <name>Display commit logs
git log
git log --oneline # Compact view
git log --graph --all --oneline # Visual graph
git log -n 5 # Last 5 commits
git log --since="2 weeks ago" # Time-based filterShow specific commit information
git show <commit-hash>
git show HEAD # Latest commit
git show <commit-hash>:<file> # File in specific commitCompare changes between commits/branches
git diff
git diff <file>
git diff --staged # Staged changes
git diff <commit1> <commit2>
git diff <branch1>..<branch2>View commits affecting a specific file
git log <file>
git log -p <file> # With diffs
git blame <file> # Line-by-line attributionFind commits by message or content
git log --grep="keyword"
git log -S "code string" # Search code changes
git log --author="name"Remove commit but keep modifications
git reset --soft HEAD~1
git reset --soft <commit-hash>Remove commit and discard changes
git reset --hard HEAD~1
git reset --hard <commit-hash>Create new commit that undoes changes
git revert <commit-hash>
git revert HEAD # Revert last commitRemove untracked files and directories
git clean -n # Dry run
git clean -f # Remove files
git clean -fd # Remove files and directories
git clean -fX # Remove only ignored filesFind and restore lost commits
git reflog
git reflog show
git checkout <commit-hash> # Restore specific commitTemporarily save changes
git stash
git stash save "message"
git stash -u # Include untracked filesShow all stashed changes
git stash listRestore stashed changes
git stash apply
git stash apply stash@{n} # Specific stash
git stash pop # Apply and remove stashView what's in a stash
git stash show
git stash show -p # With diff
git stash show stash@{n}Remove stashed changes
git stash drop stash@{n}
git stash clear # Remove all stashesReapply commits on top of another branch
git rebase <branch>
git rebase main
git rebase --continue # After resolving conflicts
git rebase --abort # Cancel rebaseEdit commit history interactively
git rebase -i HEAD~n # Last n commits
git rebase -i <commit-hash>
# Options: pick, reword, edit, squash, fixup, dropCombine multiple commits into one
git rebase -i HEAD~3
# Change 'pick' to 'squash' or 's' for commits to mergeShow all tags
git tag
git tag -l "v1.*" # Filter tagsMark a specific commit
git tag <tag-name>
git tag -a v1.0.0 -m "Version 1.0.0" # Annotated tag
git tag v1.0.0 <commit-hash> # Tag specific commitUpload tags to remote
git push origin <tag-name>
git push origin --tags # Push all tagsRemove a tag
git tag -d <tag-name> # Delete local tag
git push origin --delete <tag-name> # Delete remote tagSwitch to a tagged commit
git checkout <tag-name>
git checkout tags/<tag-name> -b <branch-name>Apply specific commits to current branch
git cherry-pick <commit-hash>
git cherry-pick <commit1> <commit2>
git cherry-pick --continue
git cherry-pick --abortBinary search to find commit that introduced bug
git bisect start
git bisect bad # Current commit is bad
git bisect good <commit-hash> # Known good commit
git bisect reset # End bisectManage repositories within repositories
git submodule add <repository-url> <path>
git submodule init
git submodule update
git submodule update --remoteManage multiple working trees
git worktree add <path> <branch>
git worktree list
git worktree remove <path>Create archive of repository
git archive --format=zip HEAD > archive.zip
git archive --format=tar main | gzip > archive.tar.gzExample .gitignore file
# Dependencies
node_modules/
vendor/
# Build outputs
dist/
build/
*.log
# Environment files
.env
.env.local
# IDE files
.vscode/
.idea/
*.swp
# OS files
.DS_Store
Thumbs.dbCommon .gitignore patterns
*.txt # All .txt files
!important.txt # Exception
folder/ # Directory
**/logs # logs in any directory
file?.txt # Single character wildcard
file[0-9].txt # Character rangeStop tracking a file that's already committed
git rm --cached <file>
git rm -r --cached <directory>
# Then add to .gitignore and commit.gitignore to exclude build artifacts, dependencies, and sensitive filesgit diff before committinggit stash when switching branches with uncommitted changesThis Git Cheat Sheet is designed for developers of all skill levels to quickly reference essential version control commands. Whether you are initializing a new repository, resolving merge conflicts, or cleaning up your commit history, having these commands readily available can drastically improve your development workflow.
git reset and git revert to fix accidental commits.git checkout and git stash.git fetch and git pull.git fetch downloads updates from the remote repository but does not apply them to your working directory. git pull is a combination of git fetch followed by a git merge, updating your local code immediately.
Use git restore <file> to discard changes in a specific file, or git checkout . to discard all uncommitted local changes.
Rebasing rewrites your commit history by moving your feature branch commits to the tip of the main branch, creating a cleaner, linear history instead of a tangled merge commit.
Explore more tools to boost your productivity: