← Back to cheatsheets
Git

Git Commands

Essential git commands organized by category. From basics to advanced workflows.

Basics

Command / PropertyDescription
git initInitialize a new git repository
git clone <url>Clone a remote repository
git statusShow working tree status
git add <file>Stage file for commit
git add .Stage all changes
git commit -m "msg"Commit staged changes with message
git log --onelineShow compact commit history
git diffShow unstaged changes
git diff --stagedShow staged changes

Branching

Command / PropertyDescription
git branchList local branches
git branch <name>Create a new branch
git checkout <branch>Switch to a branch
git checkout -b <name>Create and switch to a new branch
git merge <branch>Merge branch into current branch
git branch -d <name>Delete a merged branch
git branch -D <name>Force delete a branch
git rebase <branch>Rebase current branch onto another

Remote

Command / PropertyDescription
git remote -vList remote repositories
git remote add origin <url>Add a remote repository
git fetchDownload remote changes without merging
git pullFetch and merge remote changes
git pushPush local commits to remote
git push -u origin <branch>Push and set upstream branch
git push --force-with-leaseForce push safely (checks remote)

Undoing

Command / PropertyDescription
git restore <file>Discard working directory changes
git restore --staged <file>Unstage a file
git reset HEAD~1Undo last commit, keep changes
git reset --hard HEAD~1Undo last commit, discard changes
git revert <commit>Create a new commit that undoes a commit
git commit --amendModify the last commit

Stash

Command / PropertyDescription
git stashStash working directory changes
git stash popApply and remove latest stash
git stash listList all stashes
git stash apply stash@{n}Apply a specific stash without removing
git stash drop stash@{n}Delete a specific stash
git stash clearDelete all stashes