0

I started with the master branch along with few other developers and we are following a nice workflow where we create a new issue specific branch, merge it back to develop and after few features merge develop to master.

Some times we also do hotfixes in master but then merge master into develop to avoid future merge conflicts.

The Problem

At some point, I merged master back to develop but while solving merge conflicts, removed some code that was done in develop . Now we realized the problem that was created so Now we need to undo that specific merge.

So, let me share some info with you.

master

  • hotfix-1
  • hotfix-2
  • hotfix-3
  • merge develop
  • revert merge develop

current state : ok

develop

  • started from master
  • work a
  • work b
  • merge work from test_1
  • merge work from test_2
  • merge master into develop
  • work c
  • work d
  • work e

If you are concerned by the chronological order, the last 2 commits of the master are the latest changes.

What I wish

I hope there is an option to revert the merge as I am completely blank.

Options:

  1. I tried reverting, but turns out you cannot revert a merge commit
  2. I thought I can create a new branch from the commit before the merge and then replay the changes after the merge and then merge the new branch back to develop. Is this possible ? will it allow me to continue our workflow ??

An important point is that I might not be getting the full picture, as I just read that after merge both branches share the history and when I go to commit hash in bitbucket I see master in all the commits. I am guessing commits have been transferred to master.

Any help or insights will be greatly helpful. thanks.

Rishiraj Purohit
  • 447
  • 5
  • 17
  • You can revert a merge commit, however, because a merge commit contains multiple parents, you need to specify which one. Most of the cases, you will want `git revert MERGE_COMMIT_HASH -m 1` (1 being the first parent, the branch in which you merged). I refer you to `man git-revert` for more details. – padawin May 08 '19 at 13:50

1 Answers1

0

You're in the right direction. Git is one-way path and there is no way to step back.

Either you can use 2nd option specified by you. You have to create a branch just before marge commit. then merge the master in this branch. After that you should delete the develop branch and rename this newly created branch.

Another way is you can create a new branch from master and cherry-pick the changes(you can also merge the branches you created for the features/bug rather than doing cherry-pick) you did in develop branch and again have a backup of developing, delete the develop and rename the newly created branch as develop.

I would recommend using git rebase for this kind of merging for a better and clean history https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase.

Shivang Agarwal
  • 1,825
  • 1
  • 14
  • 19
  • okay, one-way got it. current problem cannot cherry-pick a merge commit. secondly, would I be able to continue after all this, because I hate the error from git that says cannot merge unrelated histories. any thoughts on that / – Rishiraj Purohit May 08 '19 at 11:21
  • There will be no unrelated history. you can have a branch from master and merge all the branches you created which are not in the master into the new branch. – Shivang Agarwal May 08 '19 at 11:30
  • I don't think you will get such kind of exceptions.I used these techniques multiple times :) – Shivang Agarwal May 08 '19 at 11:32
  • Thanks for the help, you gave me some confidence for what I was doing and some knowledge that will really help. I am trying option b now, so created a branch from history and now cherry picking my way to the top – Rishiraj Purohit May 08 '19 at 11:43
  • Why don't you directly merge your branches rather than cherry pick? you mentioned that if you create new issue specified branch right? then you can directly merge that branches right? – Shivang Agarwal May 08 '19 at 11:50
  • 1
    I would just like to add that `Git is one-way path and there is no way to step back. ` is not quite correct. You could perfectly reset hard to a commit in the past of your log and create a brand new log of commits. However, if you work with a remote and/or in a team, it will requires a minimum of knowledge and communication to handle these situations without consequences. But that's off topic for here. – padawin May 08 '19 at 13:53