Skip to content

Instantly share code, notes, and snippets.

@atima
Last active July 12, 2025 04:15
Show Gist options
  • Select an option

  • Save atima/6e11a155fffa43f9184a775f69bf5ad7 to your computer and use it in GitHub Desktop.

Select an option

Save atima/6e11a155fffa43f9184a775f69bf5ad7 to your computer and use it in GitHub Desktop.
Procedure to move only specific commits to a branch while maintaining linear history.

How to moving commits from one branch (e.g., dev) to another branch (e.g., main), then rebasing the dev branch to reflect the new branching point.

Goal:

Before                            After
A---B (main)                      A---B---C'---D' (main)
     \                                          \
      C---D---E (dev)                            E' (dev)

Step 1: Apply C and D to main

  1. Switch to the main branch:

    git checkout main
    
  2. Identify the commit hashes for C and D on the dev branch.

    Use git log dev to see the commit history of dev. Look for the commit messages corresponding to C and D and note down their full commit hashes. Let's assume:

    • Commit C hash: commit_C_hash
    • Commit D hash: commit_D_hash
  3. Cherry-pick C and D onto main:

    git cherry-pick commit_C_hash
    git cherry-pick commit_D_hash
    

    Now, the main branch will look like: A---B---C'---D'

    Before                            After
    A---B (main)                      A---B---C'---D' (main)
         \                                 \
          C---D---E (dev)                   C---D---E (dev)
    

Step 2: Make dev know it's branched from main at D

This is achieved by rebasing dev onto the new head of main (which is now D).

  1. Switch back to the dev branch:

    git checkout dev
    
  2. Identify the commit hashes for D' on the main branch.

  3. Rebase dev onto main:

    git rebase --onto commit_D'_hash commit_D_hash
    

    More on rebase --onto: https://womanonrails.com/git-rebase-onto

    Now, the goal is reached.

    Before                            After
    A---B---C'---D' (main)            A---B---C'---D' (main)
         \                                          \
          C---D---E (dev)                            E (dev)
    

Step 3: Push changes to remote repositories

❗Do not force push until you are sure about the result.

git push --force-with-lease dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment