4

Assume I have this git structure:

* hash3 (HEAD -> origin/bN, bN) Message N
|
* ...
|
* hash2 (origin/b2, b2) Message 2
|
* hash1 (origin/b1, b1) Message 1
|
* hash0 (origin/master, master) Message 0

If I want to modify something of b1, I would do git rebase -i hash0 and edit the commit with message Message 1. After the rebase I would have:

* hash6 (HEAD -> bN) Message N
|
* ...
|
* hash5 Message 2
|
* hash4 Message 1
|
| * hash3 (origin/bN) Message N
| |
| * ...
| |
| * hash2 (origin/b2, b2) Message 2
| |
| * hash1 (origin/b1, b1) Message 1
|/
* hash0 (origin/master, master) Message 0

Then, I link every new hash with its corresponding branches and push that to origin in order to have this:

* hash6 (HEAD -> origin/bN, bN) Message N
|
* ...
|
* hash5 (origin/b2, b2) Message 2
|
* hash4 (origin/b1, b1) Message 1
|
* hash0 (origin/master, master) Message 0

I do that with the following commands for every <b1, hash4>, <b2, hash5>, ..., <bN, hash6> (which is a pain in the ass):

git branch -f b1 hash4
git push origin b1 --force

Question: Is there any way to automate this logic?

Damia Fuentes
  • 5,308
  • 6
  • 33
  • 65
  • Just for testing, with a recent Git, can you test those rebase with the `--rebase-merges` option? (https://stackoverflow.com/a/50555740/6309) – VonC Jun 12 '20 at 22:14

2 Answers2

0

Yes. For one-offs you can add the commands to your pick list, that way you don't have to manually hunt up the rewritten id's.

pick hash2
exec git branch -f b2
pick hash1
exec git branch -f b1

To speed it up if there's lots of refs you could pipe the pick list through something like

awk '/^pick/{print $2}'|git log --stdin --no-walk --pretty='pick %h %s%x0a%-D'

and that will annotate the list with all the refs pointing to each picked commit.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Maybe I was not expressing well. I have rewritten my question. – Damia Fuentes Jun 12 '20 at 11:18
  • To preserve the upstreams, you can switch to e.g. `git update-ref refs/heads/b1 @` instead of `git branch -f b1`. What specifically isn't working for you here? – jthill Jun 12 '20 at 13:09
0

I would have to go through some rounds of bash testing to detect the order of the branches in a single shot.... but this is the idea:

git checkout --detach master # so we don't move master
# this is the part that should be turned into a repetitive cycle, 2 steps per branch
git cherry-pick master..b1
git branch -f b1
git cherry-pick b1..b2
git branch -f b2
git cherry-pick b2..b3
git branch -f b3
.
.
.
git cherry-pick bn-1..bn
git branch -f bn
# and you finally push all the branches
git push origin -f b1 b2 b3... bn
eftshift0
  • 26,375
  • 3
  • 36
  • 60