Git Command Line Cheat Sheet

Accurate, print‑friendly reference for everyday Git operations

Setup

Configure identity and view configuration.
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

git config --list
git config user.name
git config user.email

Create / Get a Repo

git init                       # Initialize new repo in current dir
git init <dir>                 # Initialize in a new directory
git clone <url>                # Clone via HTTPS/SSH
git clone --depth 1 <url>      # Shallow clone for speed

Status & Inspection

git status
git log                        # Full log
git log --oneline --graph --decorate --all
git log -p                     # Show diffs per commit
git show <commit>              # Show details of a commit

git diff                       # Working tree vs index
git diff --staged              # Index vs HEAD

Add / Stage Files

Classic and modern equivalents.
# Classic
git add <file>
git add .                      # Track all changes in current dir
git add -A                     # Track all changes repo-wide (incl. deletes)
git reset <file>               # Unstage a file (leave working changes)

# Modern
git restore --staged <file>    # Unstage (modern)
git restore <file>             # Discard working changes to file

Commit

git commit -m "message"
git commit -am "message"       # Stage tracked files and commit
git commit --amend             # Edit last commit (message + content)
git commit --amend --no-edit   # Amend content only, keep message

Branching & Switching

# Classic
git branch                     # List branches
git branch <name>              # Create branch
git checkout <branch>          # Switch
git checkout -b <new-branch>   # Create + switch

# Modern
git switch <branch>            # Switch branches
git switch -c <new-branch>     # Create + switch

# Delete
git branch -d <branch>         # Safe delete (merged only)
git branch -D <branch>         # Force delete

Merging & Rebasing

git merge <branch>             # Merge into current branch
git merge --no-ff <branch>     # Always create a merge commit

git rebase <base>              # Rebase current branch onto <base>
git rebase -i <base>           # Interactive rebase (squash, reorder)
git rebase --continue
git rebase --abort

Remote Repositories

git remote -v
git remote add origin <url>
git remote remove <name>
git remote rename <old> <new>

Fetch / Pull / Push

git fetch                      # Update remote-tracking branches
git fetch --all --prune

git pull                       # fetch + merge
git pull --rebase              # fetch + rebase (cleaner history)

git push origin <branch>
git push -u origin <branch>    # Set upstream
git push                       # After upstream is set

git push --force-with-lease    # Safer force push

Stash

git stash                      # Stash tracked changes
git stash -u                   # Include untracked
git stash list
git stash show -p stash@{0}
git stash pop                  # Apply latest and drop
git stash apply stash@{n}      # Apply without dropping
git stash drop stash@{n}
git stash clear                # Remove all stashes

Tags

git tag                        # List tags
git tag <name>                 # Lightweight tag
git tag -a <name> -m "msg"     # Annotated tag

git show <tag>
git push origin <tag>
git push origin --tags         # Push all tags
git tag -d <name>
git push origin :refs/tags/<name>  # Delete remote tag

Undo / Recovery

# Unstage or discard
git restore --staged <file>    # Unstage
git restore <file>             # Discard working changes to file

# Reset (move HEAD)
git reset --soft <commit>      # Keep index & working tree
git reset --mixed <commit>     # Default; keep working tree, unstage
git reset --hard <commit>      # WARNING: discard all changes

# Revert (safe public undo)
git revert <commit>            # Create a new commit that undoes <commit>

# Restore file from a commit
git checkout <commit> -- <path>    # Classic
git restore --source=<commit> <path>

# Reflog (find lost commits/branches)
git reflog
git reset --hard HEAD@{1}

Ignoring Files

# .gitignore examples
node_modules/
*.log
.env
# Apply ignore to already tracked files
git rm -r --cached .
git add .
git commit -m "Apply .gitignore"

Submodules

git submodule add <url> <path>
git submodule update --init --recursive
git clone --recursive <url>
git submodule foreach git pull origin main

Common Workflows

# Start a feature branch
git switch -c feature/cool-thing
git add -A
git commit -m "Implement cool thing"
git push -u origin feature/cool-thing
# Sync with main
git fetch origin
git switch main
git pull --rebase
git switch feature/cool-thing
git rebase main
git rebase --continue
git push --force-with-lease
# Hotfix on main
git switch main
git pull
git add -A
git commit -m "Hotfix: correct production bug"
git push
git tag -a v1.0.1 -m "Hotfix release"
git push origin v1.0.1

Safety Notes

  • Prefer git revert over git reset --hard on shared/public branches.
  • Use git push --force-with-lease instead of --force to avoid overwriting others’ work.
  • Run git fetch --all --prune regularly to keep remote-tracking branches tidy.