0

We have a develop branch and a release branch.

I was asked to make a change, but was later told that the change had to be a hotfix and so I should have branched off the release branch.

The current situation is this (I drew the diagram myself):

git branching

I really don't want to loose all my work that is currently sitting on my feature-branch. How do I move all my changes in feature-branch so that they sit in a new branch but one that is branched off release branch?

What have I tried?

I tried using rebase --onto but I got the following error:

$ git rebase --onto release-branch feature/1 feature/1a

fatal: fatal: no such branch/commit 'feature/1a'

Community
  • 1
  • 1
J86
  • 14,345
  • 47
  • 130
  • 228
  • 2
    Create a branch from Release and then merge the Feature branch into that branch (and then delete the Feature branch that's from Develop) – auburg Sep 26 '19 at 15:06
  • 1
    Possible duplicate of [How to move certain commits to be based on another branch in git?](https://stackoverflow.com/questions/2369426/how-to-move-certain-commits-to-be-based-on-another-branch-in-git) – CodeCaster Sep 26 '19 at 15:07
  • Plenty of existing questions for this exact scenario exist; what have you tried? – CodeCaster Sep 26 '19 at 15:08
  • 2
    @auburg if OP does that, a later feature > release pull request will merge develop into release, which I guess is unwanted. No, you'd have to rebase the feature branch on release. – Romain Valeri Sep 26 '19 at 15:08
  • @RomainValeri is right, I don't want anything else going into the release branch, just my changes. – J86 Sep 26 '19 at 15:09
  • @J86 Ahh, point taken - then a rebase is what you need – auburg Sep 26 '19 at 15:17
  • @J86 No need for `--onto`. Check Kerim's answer. – Romain Valeri Sep 26 '19 at 15:19

3 Answers3

4

I think the best way to do this is to rebase your Feature branch onto Release. The git rebase command is quite flexible. You can tell it where to move your branch, and how much of your branch to move.

This command will move the Feature branch to branch off Release, starting from the commit after the Develop branch:

git rebase --onto Release Develop Feature

or alternatively:

git checkout Feature
git rebase --onto Release Develop

No need to create a new branch or delete your original one.

David Sugar
  • 1,042
  • 6
  • 8
  • Note that this does rewrite history. I doubt that is important in this case. If it is, then you will want to create a new branch that you will rebase and keep your original Feature branch as is. – David Sugar Sep 26 '19 at 17:17
2

To duplicate the commits from feature, and only them (no commit from develop before or after feature forked from it), you can use the following commands:

git checkout -B feature release      # Checkout `release` and bring `feature` along
git cherry-pick develop..feature@{1} # Retrieve commits from `feature` which are not on `develop`
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • I am currently on the release branch @Quentin. I'm not sure what `git checkout -B feature release` will do. I know you've said that it will checkout `release` but I'm already on that branch? I already have that branch checked out? – J86 Sep 27 '19 at 08:09
  • @J86 no harm done. It's just a fast way to attach to `feature` and reset it to the current tip of `release`. – Quentin Sep 27 '19 at 08:49
  • Do I have to cherry-pick commit by commit? I believe this is cherry-picking one single commit? `git cherry-pick develop..feature@{1}` – J86 Sep 27 '19 at 10:03
  • Thank you, I also watched [this video](https://www.youtube.com/watch?v=-ndmel-4wsk) before doing anything. Things are now how I wanted them. – J86 Sep 27 '19 at 10:22
  • @J86 I guess you've found out already, but `develop..feature@{1}` is the set of commits that are reachable from the previous position of `feature`, but not from `develop`. – Quentin Sep 27 '19 at 11:08
  • The comment on this is misleading: 'git checkout -B feature release # Checkout release and bring feature along'. More accurate would be: "create a new feature branch pointing to the tip of the release branch and check that out." – David Sugar Oct 07 '19 at 11:57
  • @DavidSugar since `feature` supposedly exists already, it is moved (hence `-B` rather than `-b`). – Quentin Oct 07 '19 at 11:59
1

Try this commands (pseudo-code: apply your naming correctly)

git checkout release
git rebase feature

After these commands, you will have work from feature branch in release branch.

However if develop has accepted commits after feature branch was checked out, those commits will not be present nor in feature nor in release.

You will need to rebase your release branch agains master in order to have them in release branch. By running:

git checkout release
git rebase develop

P.S. Here is the link that describes the rebase command https://git-scm.com/docs/git-rebase