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)
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.
git checkout master
git revert bad-commit
git checkout -b more-work
git cherry-pick bad-commit
- 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:
git branch more-work bad-commit
git rebase -i bad-commit^ master
- Change the line with bad commit from
pick
todrop
(or delete it altogether` - Save and exit the file
Non-interactive:
git branch more-work bad-commit
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).