6

I do have a project in GIT which currently has 3 branches, master, develop and feature/core-md-topbar.

I was working in the feature/core-md-topbar branch and discovered a bug which is present in the development branch which I accidentally fixed in the feature/core-md-topbar branch.

Now, I don't have anything pushed to the remote yet, but I want my fix to be part of the develop branch.

So, this is my current commit:

enter image description here

So, I want the commit starting with FIX and hash 64376b2 to be placed immediately after the second commit with hash 8f928d0.

Off course, I should not lose any work.

What's the gittish way to solve this problem?

6
  • As a new head/branch out from the history or in between 8f92 and 8b96? Commented Mar 17, 2017 at 13:57
  • 2
    Does remote have develop branch? If yes, which commit is it pointing to?
    – Uzbekjon
    Commented Mar 17, 2017 at 13:58
  • If nothing of this is pushed you can most likely use an interactive rebase to reorder the changesets. Commented Mar 17, 2017 at 13:58
  • Nothing is pushed. You mind sharing the command I require since I've tried alot using rebase and always messed up my repo :-)
    – Complexity
    Commented Mar 17, 2017 at 13:59
  • Do you want to apply the fix commit to development, reorder commits in core-md-topbar, both, or something else? This is not clear. Commented Mar 17, 2017 at 14:12

1 Answer 1

4

Cherry-pick the fix commit from core-md-topbar to development:

git checkout development
git cherry-pick 64376b2

Remove the fix commit from core-md-topbar:

You can try doing an interactive rebase:

git rebase -i HEAD~8

A window looking something like the following should come up. Note that commits are shown from oldest to newest.

pick 2eb670c comment
pick 8f928d0 comment
pick 8b96886 comment
pick 8fa512e comment
pick 995396f comment
pick dd4ab71 comment
pick 64376b2 comment
pick 704c5da comment

Delete the line (DO NOT drop the commit!) containing the fix commit 64376b2, leaving you with this:

pick 2eb670c comment
pick 8f928d0 comment
pick 8b96886 comment
pick 8fa512e comment
pick 995396f comment
pick dd4ab71 comment
pick 704c5da comment

Now save and close the window, which will start the actual rebase. You may get merge conflicts as each commit is reapplied from 8 commits ago.

The first cherry-pick safe is clean and safe. But the interactive rebase runs a potential risk if the core-md-topbar branch is publicly shared and you have published from the fix commit onwards. If you have published already, then you are much safer to revert that fix commit, assuming you don't want in the core-md-topbar branch:

git checkout core-md-topbar
git revert 64376b2

This instructs Git to make a new commit on the HEAD of core-md-topbar which effectively undoes the fix commit. And publishing this won't cause problems for a public branch that others are using.

10
  • Is a rebase safe to do when it's spanning multiple branches?
    – Complexity
    Commented Mar 17, 2017 at 14:08
  • Wait...I don't understand your requirement. You made a fix commit in core-md-topbar but you want that fix in development, but you want to rebase core-md-topbar? This doesn't make any sense. Please explain. Commented Mar 17, 2017 at 14:10
  • The commit must be removed from core-md-topbar and applied to development.
    – Complexity
    Commented Mar 17, 2017 at 14:13
  • This is nothing like what you described. Give me a few minutes, unless someone else beats me. Commented Mar 17, 2017 at 14:14
  • Sorry that my question was not clear. To me, it seems clear :-)
    – Complexity
    Commented Mar 17, 2017 at 14:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.