Skip to content

Git Commands

Setup & Config

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"
git config --list

Daily Workflow

# Clone
git clone https://github.com/org/repo.git
git clone --depth 1 https://github.com/org/repo.git  # shallow clone

# Status and diff
git status
git diff
git diff --staged

# Stage and commit
git add .
git add -p              # interactive / partial staging
git commit -m "message"
git commit --amend      # amend last commit (local only)

# Push / pull
git pull --rebase origin main
git push origin feature/branch
git push --force-with-lease  # safe force push

Branching

# List branches
git branch -a

# Create and switch
git switch -c feature/my-feature
git checkout -b feature/my-feature  # older syntax

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

# Delete branch
git branch -d feature/done       # safe delete
git branch -D feature/abandoned  # force delete
git push origin --delete feature/old-branch

Merging & Rebasing

# Merge branch into current
git merge feature/my-feature
git merge --no-ff feature/my-feature  # preserve merge commit

# Rebase onto main
git rebase main
git rebase -i HEAD~3  # interactive rebase last 3 commits

# Squash via merge
git merge --squash feature/my-feature
git commit -m "squashed: feature description"

Undoing Things

# Unstage file
git restore --staged file.txt

# Discard working tree changes
git restore file.txt

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

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

# Revert a commit (creates new commit, safe for shared branches)
git revert abc1234

# Hard reset to remote state (DESTRUCTIVE)
git fetch origin
git reset --hard origin/main

Stashing

git stash
git stash push -m "wip: description"
git stash list
git stash pop
git stash apply stash@{1}
git stash drop stash@{0}
git stash clear

Log & History

# Pretty log
git log --oneline --graph --decorate --all

# Search commits by message
git log --grep="fix:" --oneline

# Who changed a line
git blame file.txt

# Show what changed in a commit
git show abc1234

# Find when something was introduced
git bisect start
git bisect bad HEAD
git bisect good v1.0.0

Tags

git tag v1.2.3
git tag -a v1.2.3 -m "Release 1.2.3"
git push origin v1.2.3
git push origin --tags
git tag -d v1.2.3
git push origin --delete v1.2.3