Git Cheatsheet
A practical reference for everyday Git commands.
Installation & Setup
Check Git Version
git --versionSet Global Username & Email
git config --global user.name "Your Name"
git config --global user.email "[email protected]"View Config
git config --listRepository Initialization
Initialize a New Repository
git initClone an Existing Repository
git clone <repository-url>Basic Workflow
Check Status
git statusAdd Files to Staging
Add Single File
git add filenameAdd All Files
git add .Commit Changes
git commit -m "Your commit message"Add & Commit in One Step
git commit -am "Your commit message"Viewing Changes
Show Unstaged Changes
git diffShow Staged Changes
git diff --stagedView Commit History
git logCompact Log
git log --onelineGraph View
git log --oneline --graph --allBranching
List Branches
git branchCreate New Branch
git branch branch-nameSwitch Branch
git checkout branch-nameModern Way (Recommended)
git switch branch-nameCreate & Switch in One Step
git checkout -b branch-nameModern Way
git switch -c branch-nameDelete Branch
git branch -d branch-nameMerging
Merge Branch into Current Branch
git merge branch-nameRemote Repositories
Add Remote
git remote add origin <repository-url>View Remotes
git remote -vPush to Remote
git push origin branch-namePush First Time (Set Upstream)
git push -u origin branch-namePull Latest Changes
git pull origin branch-nameFetch Without Merging
git fetchUndo & Reset
Unstage File
git restore --staged filenameDiscard Changes in File
git restore filenameAmend Last Commit
git commit --amendReset to Previous Commit (Soft)
git reset --soft HEAD~1Reset to Previous Commit (Hard)
git reset --hard HEAD~1Hard reset deletes changes permanently.
Stashing
Save Changes Temporarily
git stashList Stashes
git stash listApply Stash
git stash applyApply & Remove Stash
git stash popTags
Create Tag
git tag v1.0.0Push Tag
git push origin v1.0.0List Tags
git tagCleanup
Remove Untracked Files
git clean -fRemove Untracked Files & Directories
git clean -fdHelpful Shortcuts
Show Who Changed a Line
git blame filenameShow Specific Commit
git show commit-hashRename Branch
git branch -m new-branch-nameBest Practices
- Commit small, meaningful changes
- Use clear commit messages
- Pull before pushing
- Use branches for features
- Never force push to shared branches
Happy Coding