Git 2.38 introduced the --update-refs flag to the rebase command. If you have a chain of branches, it will update all the branches in the chain. I've found this incredibly useful when breaking a large PR down into more easily reviewed PRs.
However, after git rebase --update-refs dev, I need to push all the refs to GitHub to update the PRs. This requires a set of git checkout branch1 && git push --force-with-lease && git checkout branch2 && git push --force-with-lease && ....
Is there a way built into git to handle this automatically? I basically want to push --force-with-lease automatically for any branches updated during a rebase --update-refs.
I'm not sure that you can do this "automatically", but you can get pretty close.
When you use the --update-refs option with rebase, the output will tell you all of your local branches that were updated:
$ git rebase origin/main my-branch --update-refs
Successfully rebased and updated refs/heads/my-branch.
Updated the following refs with --update-refs:
refs/heads/some-branch-1
refs/heads/some-branch-2
From there you can use that output to build a single command to force push all of your modified branches:
git push --force-with-lease origin my-branch some-branch-1 some-branch-2
Tip: when using the --update-refs option and there is at least one branch to update, you'll notice that the number of actions in the output is higher than the number of commits you are rebasing. This is because every instruction line is counted (including blank lines). You can add the -i option (for interactive rebase) to see exactly what instructions are used. This is also a good way to see what branches will be updated when you run the command, if you wish to know that beforehand.
To save from copy/pasting, if you have a branch naming convention, such as fix/a-bug or project/beta, it's possible to target all branches branched from a target using git branch --contains:
git branch --list 'project/beta*' --format='%(refname:lstrip=2)' | xargs -I "{}" git push origin {}
The branch pattern (in the example above project/beta*) is optional.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With