Cortlendt
Cortlendt

Reputation: 2240

Moving commits to another branch with Git

I need to make changesets 2-5 to be on the red (leftmost) branch instead of blue branch. Got weird results with rebase since 2-5 are "based" on 1 anyways. Do I need to cherry pick? I'm currently using Source Tree.

enter image description here

Commits 2-5 must only appear on blue branch after the pull request. Meaning they have to appear on red branch, be approved and only then merged into blue one.

Upvotes: 1

Views: 2929

Answers (1)

user229044
user229044

Reputation: 239220

Then just merge the branch. Git will perform a fast-forward merge (there will be no merge commit created), as there are no commits on the red branch, and the result will be that commits 2 through 5 are "on the red branch" - really, the branch pointer will be moved to point to 5, which seems to be what you're after.

$ git checkout <red branch>
$ git merge <blue branch>

# or

$ git merge <commit id of 5>

RE: Your update

It looks like you want to "move" the commits to the red branch. You cannot do that, that isn't how Git works. You can never "move" commits, you only move branch pointers.

If you want the commits to appear "only" on the red branch, you basically just need to swap the branch pointers. You can do this through a series of git reset --hard's, or merge the blue branch into the red branch as above, and then reset the blue branch to the point before commit 2. You'll have to choose which point that is, since commit 2 is a merge commit. You can probably do well by resetting it to the state of origin/blue-branch.

So, do as above, (merge blue into red) and then...

$ git checkout <blue branch>
$ git reset --hard <commit id of red commit>

# OR

$ git reset --hard <commit id of purple commit>

# OR

$ git reset --hard <origin/blue branch>

Upvotes: 3

Related Questions