1

We have three branches: prod, dev, next.

One of our feature is delayed to the next release, so one commit in the middle of dev branch needs to move to next branch. What should I do?

P.S. I want to move the commit rather than copy.

bydsky
  • 1,604
  • 2
  • 14
  • 30

1 Answers1

2

You can't move a commit, but you can copy it with git cherry-pick and remove it with git revert.

First, get the commit ID of the commit. Let's say it's abc123.

To copy the commit to next, checkout the next branch and cherry pick. There may be conflicts, deal with them as instructed by git.

  • git checkout next
  • git cherry-pick abc123

To remove the commit from dev, checkout dev and revert it.

  • git checkout dev
  • git revert abc123

This will record a new commit undoing abc123.

You can also use git rebase -i abc123^ instead of revert to completely remove the commit from history. This is overkill for this situation. Since presumably that branch has gone through testing and development, you want to retain the record of that commit having existed through that process. Using rebase will also change all your commit id's from abc123 forward which will require everybody to git pull --force and is generally to be avoided if possible.

Schwern
  • 153,029
  • 25
  • 195
  • 336