# Merge feature into main
git checkout main
git merge feature
# Merge with no-fast-forward (always creates merge commit)
git merge --no-ff feature
# Abort a conflicted merge
git merge --abort
# Rebase current branch onto main
git rebase main
# Interactive rebase — squash, reorder, edit commits
git rebase -i HEAD~5
# Abort a conflicted rebase
git rebase --abort
# Continue after resolving conflicts
git rebase --continue
Golden rule: never rebase commits that have been pushed to a shared branch.
Resolving conflicts
# After a conflict, files contain markers:
# <<<<<<< HEAD
# your changes
# =======
# incoming changes
# >>>>>>> feature
# After editing the file to resolve:
git add resolved-file.js
git commit # for merge
git rebase --continue # for rebase
Remote Operations
Command
Description
git clone url
Clone a repository
git clone --depth 1 url
Shallow clone (latest commit only)
git remote -v
List remotes with URLs
git remote add origin url
Add remote
git remote set-url origin url
Change remote URL
git fetch
Download remote changes (no merge)
git fetch --prune
Fetch and remove stale tracking branches
git pull
Fetch + merge
git pull --rebase
Fetch + rebase (cleaner history)
git push
Push to remote
git push -u origin feature
Push and set upstream tracking
git push origin --delete feature
Delete remote branch
Stashing
Command
Description
git stash
Stash working changes
git stash -m "description"
Stash with a message
git stash -u
Include untracked files
git stash list
List all stashes
git stash pop
Apply latest stash and remove it
git stash apply stash@{2}
Apply specific stash (keep in list)
git stash drop stash@{0}
Delete a specific stash
git stash clear
Delete all stashes
git stash show -p
Show stash diff
Log & History
# Compact one-line log
git log --oneline
# Graph view with branches
git log --oneline --graph --all --decorate
# Log with diff
git log -p
# Last 5 commits
git log -5
# Commits by author
git log --author="Alex"
# Commits in date range
git log --after="2026-01-01" --before="2026-03-01"
# Search commit messages
git log --grep="fix"
# Search code changes (pickaxe)
git log -S "functionName"
# Show specific commit details
git show abc1234
# Who changed each line
git blame file.js
# Short blame with commit dates
git blame -s --date=short file.js
Undoing Changes
Scenario
Command
Discard unstaged changes in a file
git checkout -- file or git restore file
Unstage a file (keep changes)
git reset HEAD file or git restore --staged file
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)
git reset --hard HEAD~1
Create a new commit that undoes a previous one
git revert abc1234
Discard all local changes
git checkout -- . or git restore .
Reset to remote state
git fetch origin && git reset --hard origin/main
git reset --hard is destructive. Use git reflog to recover if needed — it tracks HEAD movements for ~30 days.
Tags
# Create lightweight tag
git tag v1.0.0
# Create annotated tag (recommended)
git tag -a v1.0.0 -m "Release version 1.0.0"
# Tag a specific commit
git tag -a v0.9.0 -m "Beta release" abc1234
# List tags
git tag -l
git tag -l "v1.*"
# Show tag details
git show v1.0.0
# Push a tag to remote
git push origin v1.0.0
# Push all tags
git push origin --tags
# Delete local tag
git tag -d v1.0.0
# Delete remote tag
git push origin --delete v1.0.0