user3539959
user3539959

Reputation: 383

Create new branch with all commits after a specific commit

I was working on a git branch with multiple commits (Release-X). Now I'm being asked to create a new branch for all commits after a specific commit (Release-Y). I have seen questions (and answers) about creating a new branch with a specific commit. But if I do, will it copy all the commits after that commit to the new branch?

How do I create a new branch from a specific commit (say commit-x) so that all my commits after commit-x are copied into this new branch?

Also once this is done, how do I change the existing branch so that commit-x is the last commit on this branch

Thanks.

EDIT

Based on the comments, it looks like what I need is not possible. Is it possible to rename an existing branch to something else? In that case I can create a new branch with that commit (Release-X1) and rename the existing branch to Release-Y?

Upvotes: 2

Views: 1534

Answers (1)

karthick
karthick

Reputation: 12176

For your original question

Its kind of difficult if the commits before x and after x are changing the file history of same file. The below points are some idea's which you can try. But you have to merge them carefully.

But what you can try is create new branch from your originalbranch, revert that to the shacommit you want. Then create another branch from your originalbranch and simply git revert the commits before commit-x till you reach your desired commit or till you reach your last stable branch.

(or)

create one local branch(lb1) from your original branch and reset it to the sha commit you want. And then create another local(lb2) from master and merge only the commits you want from original branch to (lb2)

For your edit question

Yes you can rename the branch git checkout -b Release-x1 old_branch, will create newbranch where you can use git reset --hard shacommit, to rollback to that commit.

And then goto oldbranch

git branch -m old_branch release_y

Upvotes: 1

Related Questions