1

I'm trying to fix my git branches, here's what I did.

I had some code that should have been developed in a branch; however, I committed and pushed to master (commit A).

Then I went ahead and reverted that commit and also pushed the revert to master (commit B reverts A).

Then I created a branch off of commit A and continued my development (commit C and D). So this is what my tree looks like currently:

* D (HEAD -> branch_a, origin/branch_a) removed testing code
* C updated
| * B (origin/master, origin/HEAD, master) Revert "new code"
|/  
* A new code

I want to be able to cleanly merge this branch back into master. Normally what I would do is git pull origin master into my branch but I can't do that in this case because master has the revert of A.

What can I do in branch_a to make sure it will be able to merge back into master without losing my work?

FGreg
  • 14,110
  • 10
  • 68
  • 110
  • Is it correct that you simply want to forget about `B` and want your history to look like `A -> C -> D`? – mkrieger1 Feb 23 '17 at 23:42
  • @mkrieger1 once I merge back to master, yes I essentially want to forget about `B`. The initial problem is that `A` never should have been on master in the first place, the only reason `B` is necessary is that so `A` is not on master until I'm ready for it to be. – FGreg Feb 24 '17 at 01:00

1 Answers1

0

After typing up the question I found a similar question that happened to have a command in it which achieved the result I was looking for.

Since the only commit to master is B which is a commit that is unwanted on the branch, it is possible to merge master into branch_a using a strategy that keeps the changes in the branch in case of conflicts.

From branch_a run git merge -s ours master which will merge master into branch_a using branch_a versions in case of conflicts.

The result of this operation changes the tree to look like:

E (HEAD -> branch_a, origin/branch_a) Merge branch 'master' into branch_a
|\  
| * B (origin/master, origin/HEAD, master) Revert "new code"
* | D removed testing code
* | C updated
|/  
* A new code
Community
  • 1
  • 1
FGreg
  • 14,110
  • 10
  • 68
  • 110