Git Commands Cheat Sheet

Every git command you need, organized by task. Click to copy any command. Bookmark this page.

Jump To

Setup & Configuration

git config --global user.name "Your Name"
Set your name for all repositories
git config --global user.email "[email protected]"
Set your email for all repositories
git config --global init.defaultBranch main
Set default branch name to "main" for new repos
git config --global core.editor "code --wait"
Set VS Code as the default editor (replace with vim, nano, etc.)
git config --list
Show all git configuration settings
git init
Initialize a new git repository in the current directory
git clone https://github.com/user/repo.git
Clone a remote repository to your machine
git clone --depth 1 https://github.com/user/repo.git
Shallow clone (only latest commit, much faster for large repos)

Basics

git status
Show working tree status (modified, staged, untracked files)
git add file.txt
Stage a specific file for commit
git add .
Stage all changes in current directory and subdirectories
git add -p
Interactively stage chunks of changes (patch mode)
git commit -m "Your commit message"
Commit staged changes with a message
git commit -am "Message"
Stage all tracked modified files and commit in one step
git commit --amend
Modify the last commit (message or add forgotten files)
git diff
Show unstaged changes (working directory vs staging area)
git diff --staged
Show staged changes (staging area vs last commit)
git diff HEAD
Show all changes since last commit (staged + unstaged)
git rm file.txt
Remove a file from working directory and stage the deletion
git mv old.txt new.txt
Rename/move a file and stage the change

Branching

git branch
List all local branches (current branch marked with *)
git branch -a
List all branches (local + remote)
git branch feature-name
Create a new branch (does not switch to it)
git checkout -b feature-name
Create and switch to a new branch
git switch feature-name
Switch to an existing branch (modern alternative to checkout)
git switch -c feature-name
Create and switch to a new branch (modern syntax)
git branch -d feature-name
Delete a branch (only if fully merged)
git branch -D feature-name
Force delete a branch (even if not merged) destructive
git branch -m old-name new-name
Rename a branch
switch vs checkout: git switch (added in Git 2.23) is specifically for switching branches. git checkout does more (file restore, detached HEAD). Use switch for clarity.

Merging & Rebasing

git merge feature-name
Merge feature-name into current branch
git merge --no-ff feature-name
Merge with a merge commit (even if fast-forward is possible)
git merge --squash feature-name
Squash all commits from feature into a single commit
git merge --abort
Abort a merge in progress (return to pre-merge state)
git rebase main
Rebase current branch onto main (replay commits on top)
git rebase --abort
Abort a rebase in progress
git rebase --continue
Continue rebase after resolving conflicts
git cherry-pick abc1234
Apply a specific commit from another branch to current branch
Rebase warning: Never rebase commits that have been pushed to a shared remote branch. Rebase rewrites commit history, which causes problems for other collaborators.

Remote Repositories

git remote -v
List all remote repositories with URLs
git remote add origin https://github.com/user/repo.git
Add a remote repository named "origin"
git remote rename origin upstream
Rename a remote
git remote remove origin
Remove a remote
git fetch
Download objects and refs from remote (does not merge)
git fetch --prune
Fetch and remove remote-tracking branches that no longer exist
git pull
Fetch and merge changes from remote (git fetch + git merge)
git pull --rebase
Fetch and rebase instead of merge (cleaner history)
git push
Push commits to the remote tracking branch
git push -u origin feature-name
Push and set upstream for a new branch
git push origin --delete feature-name
Delete a remote branch
git push --force-with-lease
Force push safely (fails if remote has new commits) use with care

Stashing

Temporarily save changes without committing. Useful when you need to switch branches.

git stash
Stash all modified tracked files
git stash -u
Stash including untracked files
git stash save "work in progress on login"
Stash with a descriptive message
git stash list
List all stashes
git stash pop
Apply the most recent stash and remove it from the list
git stash apply stash@{2}
Apply a specific stash without removing it
git stash drop stash@{0}
Delete a specific stash
git stash clear
Delete all stashes destructive
git stash show -p stash@{0}
Show the diff of a specific stash

Undoing Changes

git restore file.txt
Discard changes in working directory (restore to last commit) destructive
git restore --staged file.txt
Unstage a file (keep changes in working directory)
git reset HEAD~1
Undo last commit, keep changes staged
git reset --soft HEAD~1
Undo last commit, keep changes staged (same as above)
git reset --mixed HEAD~1
Undo last commit, unstage changes (keep in working directory)
git reset --hard HEAD~1
Undo last commit and discard all changes destructive
git revert abc1234
Create a new commit that undoes a specific commit (safe for shared branches)
git clean -fd
Remove untracked files and directories destructive
git clean -fdn
Dry run: show what would be removed (add -n flag)
git reflog
Show history of HEAD changes (use to recover lost commits)
Lost a commit? git reflog shows every place HEAD has been. Find the commit hash you need, then git checkout abc1234 or git reset --hard abc1234 to recover it. Reflog entries expire after 90 days.
reset vs revert: Use git revert on shared/pushed branches (it creates a new commit). Use git reset on local-only branches (it rewrites history).

Log & History

git log
Show commit history
git log --oneline
Compact log (one line per commit)
git log --oneline --graph --all
Visual branch graph of all branches
git log -n 10
Show only the last 10 commits
git log --author="name"
Filter commits by author
git log --since="2 weeks ago"
Show commits from the last 2 weeks
git log --grep="fix"
Search commit messages for "fix"
git log -p file.txt
Show full diff history for a specific file
git log --stat
Show files changed in each commit with insertions/deletions
git blame file.txt
Show who last modified each line of a file
git show abc1234
Show details and diff of a specific commit
git shortlog -sn
Show commit count per author (leaderboard)

Tags & Releases

git tag
List all tags
git tag v1.0.0
Create a lightweight tag at current commit
git tag -a v1.0.0 -m "Release version 1.0.0"
Create an annotated tag with a message
git tag -a v1.0.0 abc1234
Tag a specific past commit
git push origin v1.0.0
Push a specific tag to remote
git push origin --tags
Push all tags to remote
git tag -d v1.0.0
Delete a local tag

Advanced Commands

git bisect start
Start binary search for the commit that introduced a bug
git bisect good abc1234
Mark a known good commit
git bisect bad
Mark the current commit as bad
git bisect reset
End bisect session and return to original branch
git worktree add ../feature-branch feature-branch
Check out a branch in a separate directory (work on two branches simultaneously)
git submodule add https://github.com/user/lib.git
Add a git submodule
git submodule update --init --recursive
Initialize and update all submodules
git archive --format=zip HEAD -o project.zip
Create a zip archive of the current commit
git rev-parse HEAD
Get the full SHA of the current commit
git count-objects -vH
Show repository size and object statistics

Common Workflows

Feature Branch Workflow

  1. git checkout main and git pull
  2. git checkout -b feature/my-feature
  3. Make changes, commit often
  4. git push -u origin feature/my-feature
  5. Open a Pull Request
  6. After merge: git checkout main, git pull, git branch -d feature/my-feature

Resolve Merge Conflicts

  1. git merge feature-branch (conflicts appear)
  2. Open conflicted files, look for <<<<<<< markers
  3. Edit files to resolve conflicts
  4. git add . (mark as resolved)
  5. git commit (finalize the merge)

Squash Commits Before Merge

  1. git checkout feature-branch
  2. git rebase -i HEAD~5 (squash last 5 commits)
  3. Mark commits as "squash" or "fixup" in the editor
  4. Write a clean commit message
  5. git checkout main and git merge feature-branch

Undo a Pushed Commit (Safely)

  1. git revert abc1234 (creates a new undo commit)
  2. git push

Update Fork with Upstream Changes

  1. git remote add upstream https://github.com/original/repo.git
  2. git fetch upstream
  3. git checkout main
  4. git merge upstream/main
  5. git push origin main

Useful Git Aliases

Add these to your ~/.gitconfig under [alias] to save time.

git config --global alias.st status
git st instead of git status
git config --global alias.co checkout
git co instead of git checkout
git config --global alias.br branch
git br instead of git branch
git config --global alias.ci commit
git ci instead of git commit
git config --global alias.lg "log --oneline --graph --all --decorate"
git lg for a beautiful branch graph
git config --global alias.last "log -1 HEAD --stat"
git last to see the most recent commit
git config --global alias.undo "reset --soft HEAD~1"
git undo to undo the last commit (keep changes staged)

Essential .gitignore Patterns

node_modules/ .env .env.local dist/ build/ *.log .DS_Store Thumbs.db .idea/ .vscode/ *.pyc __pycache__/ .coverage *.sqlite3
Common .gitignore entries for web projects
Pro tip: Use gitignore.io to generate .gitignore files for your specific tech stack. Or check GitHub's collection of gitignore templates.

More Developer Tools

JSON formatter, regex tester, JWT decoder, and 40+ other tools. All free, all in your browser.

See All Tools

Related Tools

Back to ToolPipe
Get a free API key for 100+ developer endpoints:
Pro plans
ToolPipe JSON Formatter CSS Minifier JS Minifier UUID Generator Regex Tester JWT Decoder Password Generator Hash Generator Base64 JSON to YAML QR Generator Merge PDF Image to Base64 Color Picker My IP XML Formatter YAML Validator CSV to JSON Diff Checker SQL Formatter Free API Key Pro Plans Quick Start
130+ free developer tools by ToolPipe. No signup, no tracking. Support us