Home > Back-end >  How can I remove an old Git commit from master and turn it into its own branch?
How can I remove an old Git commit from master and turn it into its own branch?

Time:03-18

There is a single commit C in my master branch history that we have decided needs more work. It is not the latest commit. There have been 10 commits since then. Fortunately, none of the subsequent commits touched any of the files changed by C.

I want to remove this commit from the master branch and simultaneously create a new branch test-c from the changes in C. This way, more work can be done on it and then later on it can be merged into master.

So I want to go from this:

---A---B---C---D---E (master)

to this

---A---B---D---E (master)
                \
                 '---C (test-c)

or even just to this would be fine (I can merge later)

---A---B---D---E (master)
        \
         '---C (test-c)

The enter image description here

Is any of these what I want? Or must I do all that rebasing? If the latter is the case, I'd just as soon add a reverse-commit to master, then create a branch with the changes and let the history look cluttered)

CodePudding user response:

You can revert the commit (leaving it in the history), create a new branch and then cherry-pick the original commit. Rewriting published history has a lot of problems, therefore I suggest to revert the changes of the commit.

Cherry-picking is required because otherwise the reverted changes will not be merged again.

  1. git checkout master
  2. git revert bad-commit
  3. git checkout -b more-work
  4. git cherry-pick bad-commit
  5. Work on the new branch and eventually merge back

If you really need to get rid of the original commit (removing it from the history, not just undoing its changes), you can use an (interactive) rebase. Interactive:

  1. git branch more-work bad-commit
  2. git rebase -i bad-commit^ master
  3. Change the line with bad commit from pick to drop (or delete it altogether`
  4. Save and exit the file

Non-interactive:

  1. git branch more-work bad-commit
  2. git rebase --onto bad-commit^ bad-commit master

CodePudding user response:

I would do it like this

git rebase -i C~
# in the list of revisions, take the first line (C) and move it to be the last line. Save / exit

rebase should run and now the branch should be A <- B <- D <- E <- C with master pointing to C (well, C', actually. It's not really the original C revision).

As usual in this cases, consider that you are rewriting history of the branch and if this branch has already been shared (other people are already working on top of it, then trying to pull this off is perfectly possible but painful so that other people start using the newly rebased branch).

  • Related