Git Commands Cheat Sheet

Command What it Does Example
๐Ÿ”ง Git Configuration
git config --global user.name "Name"Set your Git username globallygit config --global user.name "Manoj"
git config --global user.email "email@example.com"Set your Git email globallygit config --global user.email "manoj@example.com"
๐Ÿ“ Start a Working Area
git initInitialize a new Git repositorygit init
git clone <repo-url>Clone a remote repository locallygit clone https://github.com/user/repo.git
๐Ÿ› ๏ธ Work on the Current Change
git statusShow current repo status (changes, branch)git status
git add <file>Stage a file for commitgit add README.md
git add .Stage all changesgit add .
git mv <old> <new>Move or rename a file, directory, or symlinkgit mv old.txt new.txt
git restore <file>Restore working tree filesgit restore index.html
git rm <file>Remove files from the working tree and indexgit rm index.html
โœ… Committing Changes
git commit -m "message"Commit staged changes with messagegit commit -m "Initial commit"
git commit -m "msg"Record changes to the repositorygit commit -m "Add new feature"
๐Ÿ“œ History & State
git logShow commit historygit log
git log --oneline --allShow all commits in all branches, briefgit log --oneline --all
git showShow various types of objects (commits, tags, etc.)git show HEAD
git diffShow changes between commits or working treegit diff HEAD~1 HEAD
git grep "text"Search tracked files for a string or patterngit grep "function"
git reflogShow recent HEAD movements (recover commits)git reflog
git bisectFind the commit that introduced a bug using binary searchgit bisect start
๐ŸŒฟ Branching
git branchList all local branchesgit branch
git branch -aList all branches (local + remote)git branch -a
git branch -d <branch-name>Delete a local branchgit branch -d feature-x
git switch <branch>Switch branchesgit switch feature
git checkout <branch-name>Switch to a branchgit checkout master
git checkout -b <new-branch>Create and switch to new branchgit checkout -b feature-x
๐Ÿ”€ Merging & Rebasing
git merge <branch>Join two or more development histories togethergit merge dev
git merge <branch-name>Merge another branch into currentgit merge feature-x
git rebase <base>Reapply commits on top of another base tipgit rebase main
โ™ป๏ธ Reset, Revert & Stash
git reset --soft <commit-id>Move branch pointer, keep staged changesgit reset --soft 5b5139a
git reset --mixed <commit-id>Move branch pointer, keep unstaged changesgit reset --mixed 5b5139a
git reset --hard <commit-id>Move branch pointer, discard changesgit reset --hard 5b5139a
git reset --hard <commit>Reset current HEAD to the specified stategit reset --hard HEAD~1
git revert <commit-id>Undo a commit by creating a new commitgit revert 5b5139a
git stashTemporarily save uncommitted changesgit stash
git stash popApply stashed changes backgit stash pop
๐Ÿท๏ธ Tagging
git tag <tagname>Create a taggit tag v1.0 + git push origin v1.0
๐ŸŒ Remote Setup
git remote add origin <url>Adds a remote named origin pointing to your repogit remote add origin https://github.com/username/repo.git
git remote -vVerifies the remote URL(s)git remote -v
git push -u origin <branch>Pushes code to remote and sets upstream trackinggit push -u origin master
git remote remove originRemoves the remote named origingit remote remove origin
git remote rename <old> <new>Renames a remotegit remote rename origin upstream
git remote set-url origin <new-url>Changes the URL of a remotegit remote set-url origin https://new.url/repo.git
๐Ÿš€ Push / Pull / Fetch
git fetchDownload objects and refs from another repositorygit fetch origin
git pull origin <branch>Fetch & merge remote changesgit pull origin master
git pullFetch from and integrate with another repositorygit pull origin main
git push origin <branch>Push a branch to remotegit push origin master
git push -u origin <branch>Push & track remote branchgit push -u origin feature-x
git push --all originPush all local branches to remotegit push --all origin
git push origin --delete <branch>Delete a remote branchgit push origin --delete feature-x
๐Ÿ”„ Backfill
git backfillDownload missing objects in a partial clonegit backfill
๐Ÿ”Ž Checkout Specific Commit
git checkout <commit-id>Switch to a specific commit (detached HEAD)git checkout 5b5139a
โš™๏ธ Git Configuration Utilities
git config --listList all Git configurationsgit config --list
๐Ÿงน Cleaning Up
git clean -nShow files that would be removed (dry run)git clean -n
git clean -fRemove untracked files from the working directorygit clean -f
git fetch -pPrune deleted remote branchesgit fetch -p
โœ๏ธ Amend a Commit
git commit --amendModify the last commit (message or files)git commit --amend -m "Updated message"
๐Ÿ’ Cherry Pick
git cherry-pick <commit-id>Apply a commit from another branchgit cherry-pick a1b2c3d
git revert HEAD~2..HEADRevert a range of commitsgit revert HEAD~2..HEAD
๐Ÿท๏ธ Git Aliases
git config --global alias.co checkoutCreate a shortcut (alias) for a commandgit co master