2

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?

user3539959
  • 383
  • 3
  • 15

1 Answers1

1

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
karthick
  • 11,998
  • 6
  • 56
  • 88
  • What if I just create `newbranch` from `oldbranch` and then revert to commit-x on `oldbranch`. That would do the same thing right? – user3539959 Jun 29 '17 at 06:21
  • Yes. But ideally its good to have the original branch intact, so if something goes wrong you can still try the same thing again – karthick Jun 29 '17 at 06:22