Adjusting commits on master branch

A colleague has a problem the other day…

A developer has committed two commits to master that should have been on the start of a development-branch.

The problem
This is how the problem looks in a testbed:
8592-Capture-01

Two branches:

  • master with commits “B” and “C”.
  • And the branch “develop” with commits “D” and “E”.

What was wanted was

  • master with only commit “A”
  • develop with “B” through “E”

How to do it
The first thing to remember is that branches are nothing more than pointers into the commit-tree. So all we have to do is move some pointers…

First move the commits to the develop-branch. That means rebasing the develop-branch on top of master (since that’s where the two commits are).

git checkout develop
get rebase master
git pull
git push

8592-Capture-02

Then reset master

git checkout -B master master^^
git push –force

8592-Capture-03

Note that the commits “D” and “E” are still there, above the master-branch “pointer”. We can’t get rid of them, but they’ll not influence anything.

git checkout master
git merge develop
git push

8592-Capture-05

The full testbed can be downloaded here:
8592-setup

This entry was posted in Git. Bookmark the permalink.

Leave a Reply