Git - Change “origin commit” of a branch

2019-08-16 21:00发布

I have some difficult to found a Git generic worflow to remove some commits (between 2 refs) from a branch ; here is a little example :

My current state are like this :

* a878646 (develop) C8a
* 070acb7 C7a
| * 7937ce7 (HEAD, F1) C9b
| * fb4add2 C8b
| * 30456de C7b
|/  
* 6a0999e C6
* 45ae978 C5
* e8a2eeb (tag: T3, master) C4
* 24b98b2 (tag: T2) C3
* c874fc7 (tag: T1) C2
* a853900 C1

I want to test "F1" on my plateform, but ONLY the commits of "F1" on master.

In other word, I would like that F1 becomes like this :

* 7937ce7 (HEAD, F1) C9b
* fb4add2 C8b
* 30456de C7b
* e8a2eeb (tag: T3, master) C4
* 24b98b2 (tag: T2) C3
* c874fc7 (tag: T1) C2
* a853900 C1

So I have to remove commits C5 and C6.

After the tests, F1 should be merge on master.

But, these commits have to stay on "develop" branch, because after that I have to do a "git checkout develop && git rebase origin/master"

The result on develop should be :

* a878646 (HEAD, develop) C8a
* 070acb7 C7a
* 6a0999e C6
* 45ae978 C5
* 7937ce7 C9b (tag: T4, master)
* fb4add2 C8b
* 30456de C7b
* e8a2eeb (tag: T3) C4
* 24b98b2 (tag: T2) C3
* c874fc7 (tag: T1) C2
* a853900 C1

Is there a way to accomplish that simply ? This problem is recurrent, and I search another way than doing it with multiple "cherry-pick" one comits by one commits...

I search a way just using "master", "F1", and without knowing number of commits bewteen these branches and in F1 :/

I think I can maybe use "rebase onto" but I don't really understand how...

Many thanks !

1条回答
何必那么认真
2楼-- · 2019-08-16 21:31

Using rebase --onto

git checkout F1
git rebase 6a0999e --onto master

or

git checkout F1
git rebase develop --onto master

Using git rebase -i

git checkout F1
git rebase -i master
# Remove lines with C5 and C6 and save.

Using git cherry-pick

git checkout master
git cherry-pick 6a0999e..7937ce7  # note that 6a0999e commit will not be cherry-picked - this is the commit before split
git branch F1 HEAD -f  # move F1 ref to current commit

Note that commit hashes will be different than on your "result on develop" list (in every version).

查看更多
登录 后发表回答