I branched off the wrong branch, how do I move my changes to correct branch?

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):

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'

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`

Branching from the wrong branch in git · GitHub, Depending on the types of changes you make, you may not be able to just switch branches. If that is the case, you can use the git stash command to allow you to  Another answer on the same Stack Overvflow thread, provides a solution for if you have already committed your changes to a branch. The solution goes as follows: Use git log to show the commit logs, and copy the SHA of the commit you want to move. Use git checkout <branch> to checkout the branch you want to move the commit to.

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.

Accidentally worked on wrong branch in Git, How do I fix this? You created the pull request on Github (or GitLab, or whatever) to just realize you have created your feature branch off the  Undo commits on master, checkout new branch myfeature and commit all your changes as one commit. Modify refs manually for master to point back to where you were and for your new branch ref to. The first solution is nice, simple and easy to apply without a relatively small margin for error.

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

I branched off the wrong branch in Git. How do I fix this?, Have you ever branched off of a branch, then rebased? Your In other words, we move it to somewhere else in the history. git will find feature-1 's base—​which is b —and change it to be the commit at master — which is f : Deciding to change your bank branch may be on your list, but it depends on how far you will move when you relocate. If the distance is not too far it’s probably better to leave your account at the same branch, especially with today’s online facilities.

How to Fix Your Git Branches After a Rebase, You can change which branches will be pushed when saying git push . Our recommendation is to set it to current . From the git-config documentation:. Moving a Commit to Another Branch in Git. By Mike Farmer June 21, 2012 Perhaps you’ve made the same mistake I have. You’re right in the middle of developing a feature when a request comes up to fix a different completely unrelated problem. So, you jump right in and fix the issue and then you realize you forgot to start a new git feature branch.

Git: How to rebase your feature branch from one branch to another , Undo and commit to new branch, git reset HEAD~1, or move commits to the other branch? And secondly, you need get your changes on that new branch. Time to push both branches (with --force if needed, i.e. if you had Out of these cookies, the cookies that are categorized as necessary are stored  Let's say i have the correct bank details of the recipient i want to transfer money to; i have the correct account number, full names and bank name. But I don't have the correct branch code.

What to do when you commit to the wrong Git branch, How can I or my team work on multiple features in parallel? There are different branching strategies, but it is useful to think that a branch tells the story HEAD is another pointer, it points to where we are right now (currently master ) attempts to incorporate changes from both branches and records the state of all files in  From the URL, I can see that my copy of foo.c is in the caleb-dev branch. I don't need to do that very often because my local directory has the same name as the branch. A quick look at my command line prompt is usually enough to confirm that I'm in the right directory, and therefore working in the right branch.

  • Create a branch from Release and then merge the Feature branch into that branch (and then delete the Feature branch that's from Develop)
  • Possible duplicate of How to move certain commits to be based on another branch in git?
  • Plenty of existing questions for this exact scenario exist; what have you tried?
  • @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.
  • @RomainValeri is right, I don't want anything else going into the release branch, just my changes.
  • 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 no harm done. It's just a fast way to attach to feature and reset it to the current tip of release.
  • Do I have to cherry-pick commit by commit? I believe this is cherry-picking one single commit? git cherry-pick develop..feature@{1}
  • Thank you, I also watched this video before doing anything. Things are now how I wanted them.
  • @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.
  • 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.
  • That will also bring in all of the commits from develop, though.
  • True, so as an alternative you could possibly do cherry-pick. But that's an even hairier proposition.