Home/Engineer/Git / CI·CD/git rebase · cherry-pick: A …
Git / CI·CDAdvancedUbuntumacOSWindowsgitrebasecherry-pick

git rebase · cherry-pick: A Practical Guide

A practitioner-focused walkthrough of rebase and cherry-pick—from cleaning up commit history and syncing branches to applying specific commits.

Contents
  1. rebase vs merge
  2. Basic rebase
  3. interactive rebase — cleaning up commits
  4. squash example — cleaning up WIP commits
  5. cherry-pick — applying specific commits
  6. Practical patterns
  7. Caveats

rebase vs merge

mergerebase
HistoryCreates a merge commit; keeps the branchRewritten into a linear history
Conflict resolutionOncePer commit
Public branchesRecommendedDo not use
Local workFineRecommended

Basic rebase

Bash
# Apply the latest main onto your feature branch
git checkout feature/my-feature
git rebase main

# If a conflict occurs
# 1. Edit files to resolve the conflict
# 2. git add the resolved files
# 3. git rebase --continue

# Abort and restore the original state
git rebase --abort

interactive rebase — cleaning up commits

Bash
# Edit the last 3 commits
git rebase -i HEAD~3

Commands shown in the editor:

CODE
pick a1b2c3 first commit
pick d4e5f6 second commit
pick g7h8i9 third commit

Change pick to one of the following:

CommandAction
pickKeep as-is
rewordEdit the commit message
editEdit the commit contents
squashFold into the previous commit (combine messages)
fixupFold into the previous commit (discard this message)
dropDelete the commit

squash example — cleaning up WIP commits

Bash
git rebase -i HEAD~4

# In the editor:
pick a1b2c3 feat: login feature
fixup d4e5f6 wip
fixup g7h8i9 wip 2
reword h0i1j2 fix: token refresh bug

Result: 4 commits are cleaned up into 2.


cherry-pick — applying specific commits

Bash
# Find the commit hash
git log --oneline feature/hotfix

# Apply a single commit
git cherry-pick a1b2c3

# Apply a range
git cherry-pick a1b2c3..g7h8i9

# Stage only, without committing
git cherry-pick -n a1b2c3

# After resolving conflicts
git cherry-pick --continue

# Abort
git cherry-pick --abort

Practical patterns

Bash
# Apply a hotfix to both main and the release branch
git checkout main
git cherry-pick fix-commit-hash

git checkout release/v2.0
git cherry-pick fix-commit-hash

# Clean up commits before opening a PR
git fetch origin
git rebase origin/main
git rebase -i origin/main

Caveats

  • Never rebase commits that have already been pushed — it breaks collaborators' history
  • If you must force-push, use git push --force-with-lease (rejected if someone else pushed in the meantime)
#git#rebase#cherry-pick
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)Git documentation ↗

Questions & answers

Ask a question about this guide. We'll review and reply.