Hacker News new | ask | show | jobs
by chronial 966 days ago
Funny, I did just exactly that at work yesterday. If your branches have linear histories, here's what to do:

1. Make sure all branches touch separate files. I would strongly recommend git-filter-repo over git-filter-branch. It's way simpler to use and orders of magnitudes faster.

2. Generate the list of commits in the correct order:

git rev-list --date-order --reverse branchA BranchB BranchC > revisions.txt

3. Go to any branch and: git rebase -i --root --force-rebase

Paste the contents of revisions.txt into the sequence editor and add "p " at the bigging of every line. Run the rebase and you are done.

2 comments

I figured out a way to not have to paste in manually to the sequence editor. I wrote a script like this:

-----

#!/bin/bash

if [[ ! -f "$1" ]]; then

exit 1

fi

git rev-list --no-merges --date-order --reverse $(git branch -a | grep -v '*' | grep master) | awk '{print "p " $0}' > $1

exit 0

-----

Then I can just:

-----

pushd monorepo

GIT_SEQUENCE_EDITOR=../inject_reflist.sh git rebase -i --root --force-rebase

popd

-----

And off we go! Apparently there were a few branches + merges, and we have some ongoing feature branches, so I need to find a way to bring in branches other than master as well, preferably while conserving the merges as-is, instead of skipping them like I do above.

Thank you! This looks like exactly what I want. I actually am already using `git-filter-repo` - I just confused the two names in my comment there. Super useful tool!