Skip to main content

Git Cheatsheet

DevOps 17 views Apr 2026

Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"    # VS Code
git config --list                                 # view all settings

Starting a Repo

git init                        # new local repo
git clone https://github.com/user/repo.git
git clone repo.git my-folder   # clone into specific folder

Basic Workflow

git status                      # working tree status
git diff                        # unstaged changes
git diff --staged               # staged changes

git add file.txt                # stage specific file
git add .                       # stage everything
git add -p                      # interactively stage hunks

git commit -m "feat: add login"
git commit -am "fix: typo"      # stage tracked files + commit

git log --oneline --graph       # compact history
git log --author="Alice" --since="2 weeks ago"
git show abc1234                # show a commit

Branching

git branch                      # list local branches
git branch -a                   # list all (including remote)

git branch feature/login        # create branch
git checkout feature/login      # switch to it
git checkout -b feature/login   # create + switch (classic)
git switch -c feature/login     # create + switch (modern)
git switch main                 # switch to main

git branch -d feature/login     # delete (merged only)
git branch -D feature/login     # force delete

git branch -m old-name new-name # rename branch

Merging & Rebasing

# Merge feature into current branch
git merge feature/login
git merge --no-ff feature/login # always create merge commit
git merge --squash feature/login # squash all commits into one

# Rebase (rewrite commits on top of another branch)
git rebase main                 # rebase current onto main
git rebase -i HEAD~3            # interactive: squash/reword last 3

# After fixing conflicts
git add resolved-file.txt
git rebase --continue
git rebase --abort              # cancel rebase

# Cherry-pick
git cherry-pick abc1234         # apply specific commit
git cherry-pick abc1234..def567 # apply range

Stashing

git stash                       # stash current changes
git stash save "wip: login form"
git stash list                  # show all stashes
git stash pop                   # apply latest + remove from stack
git stash apply stash@{2}       # apply specific stash (keep it)
git stash drop stash@{0}        # delete a stash
git stash branch feature/xyz    # create branch from stash

Remote Operations

git remote -v                              # list remotes
git remote add origin https://...
git remote set-url origin https://new-url

git fetch origin                           # download without merging
git pull                                   # fetch + merge
git pull --rebase                          # fetch + rebase

git push origin feature/login
git push -u origin feature/login           # set upstream
git push --force-with-lease                # safe force push
git push origin :old-branch                # delete remote branch
git push origin --tags

Undoing Changes

# Unstage
git restore --staged file.txt
git reset HEAD file.txt          # (older syntax)

# Discard working changes
git restore file.txt
git checkout -- file.txt         # (older syntax)

# Undo last commit (keep changes staged)
git reset --soft HEAD~1

# Undo last commit (keep changes unstaged)
git reset HEAD~1

# Undo last commit + discard changes (DANGER)
git reset --hard HEAD~1

# Revert a pushed commit (safe — adds a new commit)
git revert abc1234

# Amend last commit (message or add forgotten files)
git add forgotten.txt
git commit --amend -m "corrected message"

Tags

git tag                         # list tags
git tag v1.0.0                  # lightweight tag
git tag -a v1.0.0 -m "Release" # annotated tag
git push origin v1.0.0
git push origin --tags
git tag -d v1.0.0               # delete local
git push origin :refs/tags/v1.0.0  # delete remote

Useful Tricks

# Find which commit introduced a bug
git bisect start
git bisect bad HEAD
git bisect good v1.0
# git bisect good/bad until found
git bisect reset

# Search commit messages
git log --grep="login"

# Find who changed a line
git blame file.txt

# Clean untracked files
git clean -fd               # remove untracked files/dirs
git clean -n                # dry run

# Aliases
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"

Found this helpful? Share it!

Tweet LinkedIn WhatsApp
Translate Page