I will use some rather dangerous operations here, so just pay attention.
There are several assumptions made in this answer:
Your last master branch should look like this: A - B - D - E
Your last new-feature branch should look like this: A - B - C
The absence of commit C will not affect the use of commits D and E over commit B
If not, please update here.
First, in the master branch, we create a new branch as a backup. Let me just call it "backup".
git branch backup
This will allow us to easily restore the main branch if something goes wrong later. Of course, we could use git reflog , but it is much more convenient.
Then, on the main branch, do the following:
git rebase -i HEAD~3
This should launch a text editor for git rebase (interactive mode). Find the line to commit C and delete it.
Your main branch should now look like this:
A - B - D' - E'
Don't worry about me writing A - B - D' - E' instead of A - B - D - E D and D' are essentially equivalent in terms of sets of changes. The same goes for E and E' .
We get there. I assume that commit C on the new-feature branch should be forked with commit B Find the SHA1 commit to fix B , then (lower the angle brackets at the bottom):
git checkout -b new-feature <SHA1 of commit B>
This will create a branch called new-feature that starts with commit B and validates it for you. Now we are on the new-feature branch, and it looks like this:
A - B
The final step is to figure out the SHA1 commit C commit. We have this in the backup branches. Using git log backup (or some other method), find commit SHA1 commit C in the backup branches. After that, in the new-feature branch, do:
git cherry-pick <SHA1 of commit C>
Of course, leave the angle brackets.
The new-feature branch should now look like this:
A - B - C'