DevOps11/19/2025⏱️ 5 min read
Git Command Cheat Sheet (with Explanations)
GitVersion ControlDevOpsCheat SheetCLI

Git Command Cheat Sheet (with Explanations)

Introduction

Git is a distributed version control system used to track code changes, collaborate with teams, and manage releases. This cheat sheet covers the most useful commands with short explanations and examples.

Setup & Configuration

Configure your identity and defaults.

# Set your identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# Helpful defaults
git config --global init.defaultBranch main
git config --global pull.rebase false
git config --global core.autocrlf input   # on macOS/Linux (use true on Windows)

# View config
git config --list --show-origin

Create, Clone, and Inspect

Start a repo, clone existing, and inspect status.

# Initialize a new repository in current folder
git init

# Clone a remote repository
git clone https://github.com/user/repo.git

# Current state
git status

# Show tracked files and changes summary
git status -s

Stage, Commit, and History

Add changes to the index and commit with messages. Explore history.

# Stage files
git add fileA.js           # stage specific file
git add .                  # stage all changes

# Commit
git commit -m "feat: add new API"

# Amend last commit message or staged content
git commit --amend

# View history
git log --oneline --graph --decorate --all
git show <commit>          # details of a commit

Compare Changes

See diffs between working tree, index, and commits.

# Working tree vs index
git diff

# Index vs last commit
git diff --cached

# Between commits/branches
git diff main..feature/login

Branches

Create, list, switch, and delete branches.

# List branches
git branch                # local
git branch -r             # remote
git branch -a             # all

# Create and switch
git switch -c feature/login
# (older)
# git checkout -b feature/login

# Switch branches
git switch main
# git checkout main

# Rename current branch
git branch -m main

# Delete merged branch
git branch -d feature/login
# Force delete (unmerged)
git branch -D feature/login

Sync with Remote

Connect remotes, fetch/pull updates, and push.

# Add remote
git remote add origin https://github.com/user/repo.git

# Fetch updates (no merge)
git fetch origin

# Pull updates (fetch + merge)
git pull origin main

# Push current branch
git push -u origin main

Merge and Rebase

Integrate changes using merge or rebase.

# Merge a branch into current branch
git merge feature/login

# Rebase current branch onto main
git fetch origin
git rebase origin/main

# Abort/continue rebase
git rebase --abort
git rebase --continue

Stash Work-in-Progress

Temporarily save uncommitted work and restore later.

# Save working changes
git stash
# Save with message
git stash push -m "WIP: refactor user service"

# List, show, apply, drop
git stash list
git stash show -p stash@{0}
git stash apply stash@{0}
git stash drop stash@{0}

Tags and Releases

Mark specific commits.

# Lightweight tag
git tag v1.0.0

# Annotated tag (recommended)
git tag -a v1.0.0 -m "Initial release"

# Push tags
git push --tags

Undo, Reset, and Revert

Safely undo or rewrite history.

# Revert creates a new commit that undoes a specific commit
git revert <commit>

# Reset moves the branch pointer (use with care)
# Soft: keep changes staged
git reset --soft <commit>
# Mixed (default): keep in working tree
git reset <commit>
# Hard: discard changes
git reset --hard <commit>

# Recover lost refs
git reflog

.gitignore Essentials

Ignore files you don't want tracked.

# Example .gitignore
node_modules/
.env
*.log
.DS_Store
dist/

Cherry-pick

Apply a specific commit from another branch.

git cherry-pick <commit>

Submodules (Advanced)

Embed another repository as a subdirectory.

# Add submodule
git submodule add https://github.com/user/lib.git libs/lib

# Initialize and update
git submodule update --init --recursive

# Pull submodules for all
git submodule foreach git pull

Troubleshooting

Fix common issues quickly.

# Resolve merge conflicts: edit files, then
git add <file>
git commit

# Abort a merge in progress
git merge --abort

# Replace local changes with remote (destructive)
git fetch origin
git reset --hard origin/main

Conclusion

With these commands, you can handle most day-to-day Git workflowsβ€”from branching and syncing to release tagging and recovery. Keep this cheat sheet handy and evolve it with team-specific conventions.

Share this article

Comments