⬅ Back

Git Commands Cheatsheet

🔧 Setup & Config

git config --global user.name "Your Name"           # Set username
git config --global user.email "your@email.com"     # Set email
git config --global core.editor "code --wait"       # Set VS Code as editor
git config --list                                   # Show configs

📦 Repository Basics

git init                            # Initialize a new repo
git clone <repo-url>                # Clone a remote repo
git status                          # Show changes (untracked/modified/staged)
git add <file>                      # Stage a file
git add .                           # Stage all changes
git commit -m "message"             # Commit staged changes
git commit -am "message"            # Add & commit tracked files
git restore <file>                  # Discard unstaged changes
git restore --staged <file>         # Unstage a file
git rm <file>                       # Delete & stage file
git mv <old> <new>                  # Rename & stage file

🔄 Branching & Merging

git branch                          # List branches
git branch <name>                   # Create a new branch
git checkout <branch>                # Switch to branch
git checkout -b <branch>             # Create & switch to branch
git merge <branch>                   # Merge branch into current
git rebase <branch>                  # Rebase current branch onto another
git branch -d <branch>               # Delete branch (safe)
git branch -D <branch>               # Force delete branch (unmerged)
git stash                            # Stash changes temporarily
git stash pop                        # Apply last stash & remove it

🌍 Remote & Collaboration

git remote -v                       # List remotes
git remote add <name> <url>         # Add a remote
git fetch <remote>                   # Fetch changes (no merge)
git pull <remote> <branch>           # Fetch + merge
git push <remote> <branch>           # Push to remote
git push -u origin main              # Set upstream & push
git push --force                     # Force push (caution!)
git pull --rebase                    # Pull with rebase (clean history)

🕵️‍♂️ Inspecting & Debugging

git log                             # Show commit history
git log --oneline --graph           # Compact history with graph
git show <commit>                   # Show changes in a commit
git diff                            # Unstaged changes
git diff --staged                   # Staged changes
git blame <file>                    # Who changed which line?
git bisect                          # Binary search for bugs

⏪ Undoing Things

git reset --soft HEAD~1             # Undo commit, keep changes staged
git reset --mixed HEAD~1            # Undo commit, unstage changes (default)
git reset --hard HEAD~1             # Undo commit & discard changes
git revert <commit>                  # Create a new commit undoing changes
git checkout <commit> -- <file>     # Restore file from a commit

🚀 Advanced & Pro Tips

git rebase -i HEAD~3                # Interactive rebase (squash/edit commits)
git cherry-pick <commit>             # Apply a specific commit
git reflog                          # Show ALL actions (for recovery)
git submodule add <repo-url>        # Add a submodule
git worktree add ../new-branch      # Parallel working directory
git tag v1.0                        # Create a lightweight tag
git push --tags                     # Push tags to remote

📌 Quick Reference

git commit --amend                  # Fix last commit
git log -p                          # Show patch (diff) in logs
git grep "pattern"                   # Search code in repo
git clean -fd                        # Delete untracked files/folders

Pro Tip: Use git <command> --help for detailed docs!

⬆ Back to Top