Skip to content

Instantly share code, notes, and snippets.

@naumvd95
Last active April 14, 2022 10:21
Show Gist options
  • Save naumvd95/96bf437e4e0841e8e8272a640303283f to your computer and use it in GitHub Desktop.
Save naumvd95/96bf437e4e0841e8e8272a640303283f to your computer and use it in GitHub Desktop.
How to rebase a long chain of Pull Requests conditionally
  1. Imagine you have N Pull Requests. Each Pull Request created from the separate branch and each of them rebased on top of the previous one. Overall it will looks like:

    vladislav.naumov@jarvis:~$ git branch | grep vnaumov
    
    key/vnaumov/some-important-feature-7
    keu/vnaumov/some-important-feature-6
    keu/vnaumov/some-important-feature-5
    keu/vnaumov/some-important-feature-4
    keu/vnaumov/some-important-feature-3
    keu/vnaumov/some-important-feature-2
    keu/vnaumov/some-important-feature-1
    keu/vnaumov/some-important-feature-0
  2. You want to make a change somewhere in the middle, lets say in keu/vnaumov/some-important-feature-6.

  3. You need to checkout at the highest branch in chain:

    vladislav.naumov@jarvis:~$ git checkout keu/vnaumov/some-important-feature-7
  4. make changes that related to keu/vnaumov/some-important-feature-6 and commit them as a fixup, using commit SHA from keu/vnaumov/some-important-feature-6, coz fixup should have a parent.

    vladislav.naumov@jarvis:~$ git commit --fixup ff3f6b77325348b5f78324f18e6a34FOOO
  5. Rebase and desired amount of commits and enable autosquash to let fixup be automatically squashed with related branch.

    vladislav.naumov@jarvis:~$  git rebase --autosquash -i HEAD~5
  6. Use exec inside interactive rebase to update branch after each PR!

    pick 57ab6d6aD [K10-XXX] old stuff
    pick c78953ebF [K10-XXX] smth else
    pick 72f4c068D [K10-XXX] also smth else
    pick 0c44f3f1D [K10-XXX] actual stuff: doing some feature
     fixup asdasdasd asdads chained with ^^^^
    exec git branch -f  keu/vnaumov/some-important-feature-6 # HERE we force recreate branch to update it
    pick 2f32db3eb [K10-XXX] also-important!
    exec git branch -f  keu/vnaumov/some-important-feature-7 # HERE dirro
    # Rebase 010335fd1..2f32db3eb onto 010335fd1 (5 commands)
  7. As result you will receive both branches updated, but double check it with git log:

    vladislav.naumov@jarvis:~$  git rebase --autosquash -i HEAD~5
    
    Executing: git branch -f keu/vnaumov/some-important-feature-6
    Executing: git branch -f keu/vnaumov/some-important-feature-7
    Successfully rebased and updated refs/heads/keu/vnaumov/some-important-feature-7.
  8. Force push both branches! (be careful buddy)

    vladislav.naumov@jarvis:~$ git push -u origin keu/vnaumov/some-important-feature-{6,7} --force-with-lease
    
    Enumerating objects: 23, done.
    Counting objects: 100% (23/23), done.
    Delta compression using up to 10 threads
    Compressing objects: 100% (16/16), done.
    Writing objects: 100% (16/16), 3.69 KiB | 3.69 MiB/s, done.
    Total 16 (delta 12), reused 0 (delta 0), pack-reused 0
    remote: Resolving deltas: 100% (12/12), completed with 7 local objects.
    To github.com:kastenhq/k10.git
     + ff3f6bX73...0c44f3fXe keu/vnaumov/some-important-feature-6 -> keu/vnaumov/some-important-feature-6 (forced update)
     + 8b3403X96...2f32db3Xb keu/vnaumov/some-important-feature-7 -> keu/vnaumov/some-important-feature-7 (forced update)
    Branch 'keu/vnaumov/some-important-feature-6' set up to track remote branch 'keu/vnaumov/some-important-feature-6' from 'origin'.
    Branch 'keu/vnaumov/some-important-feature-7' set up to track remote branch 'keu/vnaumov/some-important-feature-7' from 'origin'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment