Rohit Mishra
Rohit Mishra

Reputation: 281

Git change source of new branch to older commit of source branch

I have a branch mainline with commit history 1->2->3->4->5. I have created a local branch test from mainline using

git checkout -b "test"

I made some commits into test branch say 6->7.

This is mapped from head of mainline which is commit no 5 (i.e 5->6->7).

I want the test branch to map head of mainline from commit no 1. (i.e 1->6->7)

How to do this ?

Upvotes: 4

Views: 996

Answers (2)

Surajano
Surajano

Reputation: 2688

Using Cherry Pick

git checkout commit_id_7
git checkout -b repair new_test_branch
git cherry-pick commit_id_6
git cherry-pick commit_id_1
git checkout test
git reset --hard commit_id_7
git merge repair
git push --hard origin test

Note :Git rebase & cherrypick are dangerous but powerful solutions that should only be used as a last option

Upvotes: 1

hspandher
hspandher

Reputation: 16753

You can use rebase --onto to do that.

>>> git checkout test
>>> git checkout -b test2  # [optional] in case you have already pushed the test branch to remote, you can use test2 branch instead
>>> git rebase --onto <commit_id_of_1> <commit_id_of_5> <commit_id_of_7>

Example:-

>>> git checkout test
>>> git log --oneline
c05a4af  commit 7
b0e5f24  commit 6
74c9673  commit 5
...
6712bec  commit 1

>>> git rebase --onto 6712be 74c9673 c05a4af

rebase documentation

Upvotes: 2

Related Questions