简体   繁体   中英

git branch -f bN, git checkout bN and git push --force to all commits after an interactive rebase

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM