Git Commands — The Essential Cheat Sheet for Developers

Master the most important Git commands: branching, merging, rebasing, stashing, resolving conflicts, and advanced workflows.

General Beginner 📖 10 min

Getting Started

git init                    # Initialize a new repo
git clone <url>             # Clone a remote repo
git status                  # Check working tree status
git add .                   # Stage all changes
git commit -m "message"     # Commit staged changes
git push origin main        # Push to remote

Branching

git branch                  # List local branches
git branch <name>           # Create a branch
git checkout <name>         # Switch to branch
git checkout -b <name>      # Create and switch
git branch -d <name>        # Delete merged branch
git branch -D <name>        # Force delete branch

Viewing History

git log                     # Full commit history
git log --oneline           # Compact history
git log --graph --oneline   # Visual branch graph
git diff                    # Unstaged changes
git diff --staged           # Staged changes
git show <commit>           # Show specific commit

Undoing Changes

git restore <file>          # Discard unstaged changes
git restore --staged <file> # Unstage a file
git reset --soft HEAD~1     # Undo last commit, keep changes
git reset --hard HEAD~1     # Undo last commit, discard changes
git revert <commit>         # Create a new commit that undoes a commit

Stashing

git stash                   # Stash current changes
git stash list              # List stashes
git stash pop               # Apply and remove stash
git stash apply             # Apply without removing
git stash drop              # Delete latest stash

Advanced

git rebase main             # Rebase current branch onto main
git cherry-pick <commit>    # Apply a specific commit
git bisect start            # Binary search for bugs
git reflog                  # Find lost commits
git clean -fd               # Remove untracked files

Ready to Level Up Your Coding Skills?

Join Coderz Club and learn to code with challenges, battles, and an AI mentor.

Join Coderz Club Free →