GIT CHEAT SHEET

Version Control System
v1.0
Basic Commands
  • git initInitialize repo
  • git clone <url>Clone repo
  • git statusShow status
  • git add <file>Stage changes
  • git add .Stage all
  • git commit -m "msg"Commit
Branch Commands
  • git branchList branches
  • git branch <name>Create branch
  • git checkout <branch>Switch branch
  • git checkout -b <name>Create & switch
  • git merge <branch>Merge branch
  • git branch -d <name>Delete branch
Remote Commands
  • git remote -vList remotes
  • git remote add originAdd remote
  • git pushPush changes
  • git push -u origin mainPush & set upstream
  • git pullPull changes
  • git fetchFetch only
History & Diff
  • git logShow history
  • git log --onelineCompact log
  • git log --graphGraph view
  • git diffShow changes
  • git diff --stagedStaged changes
  • git show <commit>Show commit
Undo & Reset
# Unstage file (keep changes) git reset HEAD <file> # Discard local changes git checkout -- <file> # Undo last commit (keep changes staged) git reset --soft HEAD~1 # Undo last commit (keep changes unstaged) git reset HEAD~1 # Undo last commit (discard changes) git reset --hard HEAD~1 # Create new commit that undoes a previous commit git revert <commit>
Stash Commands
# Save changes to stash git stash git stash save "message" # List stashes git stash list # Apply most recent stash git stash apply # Apply and remove from stash git stash pop # Apply specific stash git stash apply stash@{2} # Delete stash git stash drop stash@{0}
Configuration
  • git config --global user.nameSet username
  • git config --global user.emailSet email
  • git config --listList config
  • git config --global alias.co checkoutCreate alias
Rebase
  • git rebase mainRebase on main
  • git rebase -i HEAD~3Interactive rebase
  • git rebase --continueContinue rebase
  • git rebase --abortAbort rebase
Tags
  • git tagList tags
  • git tag v1.0Create tag
  • git tag -a v1.0 -m "msg"Annotated tag
  • git push --tagsPush tags
Cherry Pick
  • git cherry-pick <commit>Apply commit
  • git cherry-pick --no-commitWithout commit
  • git cherry-pick --abortAbort pick