0

I have 2 branches: 4 and 5, which represent 4 and 5 versions of my application (5 is continuation of 4). By my fault I created new branch from branch 5 (instead of 4) and do one commit. How to change my branch in that way: ostensibly I created my branch from branch 4 and added one commit?

Now:

4 ->
 \
  5 ->
   \
   my branch with one commit ->

I want:

 my branch with one commit ->
 /
4 ->
 \
  5 ->

2 Answers2

2

Simply run

git rebase 5 my-branch --onto 4
eftshift0
  • 26,375
  • 3
  • 36
  • 60
0

A. Rebase --onto

The official https://git-scm.com/docs/git-rebase covers exactly this scenario:

Here is how you would transplant a topic branch based on one branch to another, to pretend that you forked the topic branch from the latter branch, using rebase --onto.

First let’s assume your topic is based on branch next. For example, a feature developed in topic depends on some functionality which is found in next.

o---o---o---o---o  master
     \
      o---o---o---o---o  next
                       \
                        o---o---o  topic

We want to make topic forked from branch master; for example, because the functionality on which topic depends was merged into the more stable master branch. We want our tree to look like this:

o---o---o---o---o  master
    |            \
    |             o'--o'--o'  topic
     \
      o---o---o---o---o  next

We can get this using the following command:

git rebase --onto master next topic

B. No rebase option

The steps below are more manual but their beauty is that they are very very simple to understand.

I. Get commit X to branch 4

> git checkout 4 
> git cherry-pick commitX

II. Get rid of commit X from branch 5

Option 1. > git reset HEAD~1 --hard

Option 2. > git revert commitX

tymtam
  • 31,798
  • 8
  • 86
  • 126