Essential Git commands cheat sheet
Enter a command name in the search field, or filter by category (Basic, Branching, etc.) to find the command you need.
Check the command description, common options, and practical usage examples.
Click on an example command to copy it to your clipboard for immediate use in your terminal.
git add .Stage all changesgit add -pInteractively select changes to stagegit add file.txtStage a specific filegit commit -m "feat: add new feature"Commit with a messagegit commit --amendAmend the last commitgit commit -am "fix: bug fix"Auto-stage and commitgit statusShow current statusgit status -sbShort format with branch infogit diffShow unstaged changesgit diff --stagedShow staged changesgit diff HEAD~1Compare with previous commitgit log --onelineShow compact historygit log --oneline --graphShow history with graphgit log -5Show last 5 commitsgit push origin mainPush main to origingit push -u origin featurePush and set upstreamgit push --force-with-leaseSafe force pushgit pullFetch and mergegit pull --rebaseFetch and rebasegit pull origin mainPull from origin maingit fetch originFetch from origingit fetch --allFetch from all remotesgit fetch --pruneClean up deleted branchesgit branchList local branchesgit branch feature/newCreate new branchgit branch -d feature/oldDelete merged branchgit checkout mainSwitch to main branchgit checkout -b feature/newCreate and switch branchgit checkout -- file.txtRestore file to last commitgit switch mainSwitch to main branchgit switch -c feature/newCreate and switch branchgit switch -Switch to previous branchgit merge feature/newMerge feature/newgit merge --no-ff feature/newMerge with commitgit merge --abortAbort conflicted mergegit rebase mainRebase onto maingit rebase -i HEAD~3Edit last 3 commitsgit rebase --abortAbort and restoregit showShow latest commitgit show HEAD~1Show previous commitgit show abc1234Show specific commitgit blame file.txtShow line-by-line authorsgit blame -L 10,20 file.txtBlame lines 10-20git blame -w file.txtIgnore whitespace changesgit reflogShow HEAD historygit reflog -10Show last 10 entriesgit checkout HEAD@{2}Restore previous stategit shortlog -snShow contributors by countgit shortlog -sneShow with emailsgit shortlog HEAD~100..HEADLast 100 commits contributorsgit reset --soft HEAD~1Undo commit, keep changesgit reset HEAD file.txtUnstage a filegit reset --hard HEAD~1Discard last commitgit revert HEADRevert last commitgit revert abc1234Revert specific commitgit revert -n HEAD~3..HEADRevert multiple commitsgit restore file.txtRestore file to last commitgit restore --staged file.txtUnstage filegit restore --source HEAD~1 file.txtRestore from previous commitgit clean -nPreview what will be deletedgit clean -fdRemove untracked files and dirsgit clean -fdxRemove including ignored filesgit stashSave changes temporarilygit stash popRestore saved changesgit stash listList all stashesgit remote -vList remotes with URLsgit remote add upstream https://github.com/user/repo.gitAdd upstream remotegit remote rename origin old-originRename remotegit config --global user.name "Your Name"Set user namegit config --global user.email "you@example.com"Set email addressgit config --listList all settingsgit initInitialize current directorygit init my-projectCreate new repo in directorygit init -b mainInitialize with main branchgit clone https://github.com/user/repo.gitClone a repositorygit clone --depth 1 https://github.com/user/repo.gitShallow clonegit clone -b develop https://github.com/user/repo.gitClone develop branchgit tag v1.0.0Create lightweight taggit tag -a v1.0.0 -m "Release 1.0.0"Create annotated taggit tag -d v1.0.0Delete taggit cherry-pick abc1234Apply specific commitgit cherry-pick abc1234 def5678Apply multiple commitsgit cherry-pick --abortAbort cherry-pickgit bisect startStart binary searchgit bisect bad HEADMark HEAD as badgit bisect good v1.0.0Mark v1.0.0 as goodgit grep "TODO"Search for TODOgit grep -n "function"Search with line numbersgit grep -l "import"Show matching file namesGit Commands is a cheat sheet compiling frequently used Git commands. It comprehensively covers commands needed for development, from basic operations to branching, history viewing, and undo operations. Each command includes descriptions, common options, and practical examples that can be copied with one click.
git fetch only downloads remote changes to local without affecting your working branch. git pull performs a fetch and then automatically merges or rebases to incorporate changes into your working branch.
git reset rewrites commit history to return to a previous state. git revert creates a new commit that undoes changes, preserving history. revert is safer for shared branches.
switch was added in Git 2.23 specifically for branch switching. checkout handles both branch switching and file restoration, mixing responsibilities. Using the newer commands (switch/restore) is recommended.
--force pushes unconditionally, while --force-with-lease only pushes if the remote is in the expected state. This reduces the risk of accidentally overwriting others' changes.
Use git reflog to view the history of HEAD movements. You can find the hash of the deleted commit and potentially recover it using git checkout or git cherry-pick.