Git Commands Cheat Sheet
50+ essential Git commands for everyday development.
Setup
Initialize a new Git repository
git init Clone a remote repository
git clone <url> Set your name for commits
git config --global user.name "Your Name" Set your email for commits
git config --global user.email "[email protected]" List all Git configuration
git config --list Basics
Show working tree status
git status Stage a specific file
git add <file> Stage all changes
git add -A Commit staged changes with a message
git commit -m "your message" Amend the last commit (message or files)
git commit --amend Show unstaged changes
git diff Show staged changes
git diff --staged Show commit history
git log --oneline --graph Show commit history for a specific file
git log --follow <file> Branching
List all local branches
git branch List all branches (local + remote)
git branch -a Create a new branch
git branch <name> Switch to a branch
git checkout <branch> Create and switch to a new branch
git checkout -b <name> Delete a merged branch
git branch -d <name> Force delete an unmerged branch
git branch -D <name> Rename the current branch
git branch -m <new-name> Merging
Merge a branch into the current branch
git merge <branch> Merge with a merge commit (no fast-forward)
git merge --no-ff <branch> Abort a merge in progress
git merge --abort Rebase current branch onto another
git rebase <branch> Abort a rebase in progress
git rebase --abort Apply a specific commit to the current branch
git cherry-pick <commit-hash> Remote
List remote repositories
git remote -v Add a remote repository
git remote add <name> <url> Download objects from remote (without merging)
git fetch origin Fetch and merge from remote
git pull origin <branch> Push commits to remote
git push origin <branch> Push and set upstream tracking branch
git push -u origin <branch> Force push (overwrites remote history)
git push --force-with-lease Undoing
Discard changes to a file (unstaged)
git restore <file> Unstage a file (keep changes)
git restore --staged <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 and discard all changes
git reset --hard HEAD~1 Create a new commit that undoes a previous commit
git revert <commit-hash> Remove untracked files
git clean -fd Stashing
Stash current changes
git stash Stash with a descriptive message
git stash push -m "message" List all stashes
git stash list Apply and remove the latest stash
git stash pop Apply a stash without removing it
git stash apply stash@{0} Delete a specific stash
git stash drop stash@{0} Inspection
Show details of a commit
git show <commit-hash> Show who changed each line of a file
git blame <file> Show history of HEAD changes (recovery tool)
git reflog Summarize commits by author
git shortlog -sn Tags
List all tags
git tag Create an annotated tag
git tag -a v1.0.0 -m "Release 1.0.0" Push tags to remote
git push origin --tags Delete a local tag
git tag -d <tag-name>