timhwang21.gitbook.io
  • Intro
  • Blog
    • 2023
    • 2022
    • 2021
    • 2020
    • 2019
  • CLI
    • Git
      • Bulk resolve merge conflicts
      • git rebase-onto
      • Patch from diff
    • Shell
      • Recipes
    • Vim
      • Batch editing
      • Buffers
      • <Ctrl-r>
      • External commands
      • :global
      • Help
      • Registers
      • Splits
    • Tools
  • Programming
    • React
      • Testing Styled Components
    • Typescript
      • curryRecord
      • Exhaustive conditionals with ADTs
      • newtype
      • OmitTypes
      • Safe JSON clone
      • Type inferrers
      • XOR type
    • Ruby
      • Pry
      • RSpec
    • Rails
      • Attributes
      • Cheap many to many
      • (Don't use) counter cache
    • Databases
      • Metrics
      • Testing indexes
  • Personal
    • Uses
    • Github
    • Medium
    • LinkedIn
    • Photography
Powered by GitBook
On this page
  • Problem
  • Solution
  1. CLI
  2. Git

git rebase-onto

Problem

You have a branch feature-a. You finish it and submit it for review, and then branch off of feature-a to start working on feature-b which depends on your latest changes.

Your code is reviewed and some changes are requested. You update feature-a, push, and merge. You want to keep feature-b up to date, so you check it out and run git rebase origin/master. However, your latest revisions lead to a self merge conflict when trying to rebase feature-b off of master.

# Before code review
# origin/master
master
# feature-a
master -> feature-a
# feature-b
master -> feature-a -> feature-b

# After code review
# origin/master (using master as parent for consistency, though HEAD is now feature-a')
master -> feature-a' # revised feature-a is now HEAD
# feature-b (feature-a conflicts with feature-a')
master -> feature-a -> feature-b

Solution

Instead of git rebase origin/master, use git rebase --onto origin/master feature-a.

Specifically, this means "take all commits that are on top of feature-a, and place them on top of origin/master's HEAD." In the common scenario where feature-a is the only difference between origin/master and feature-b, it can be thought of as "drop all changes between origin/master's HEAD and feature-a inclusive."

PreviousBulk resolve merge conflictsNextPatch from diff

Last updated 3 years ago