Search code examples
gitgit-commitbranching-and-merginggit-merge-conflict

I have been creating new Git branches from older branch without checking out to the master


I have been checking out to new branches from the older branch, instead of checking out to the master. currently, I have created four new features: home_feature, admin_user-feature, customer_request, blog_feature.

I forgot to checkout to the master before creating the "customer_request" branch, and create the feature by moving into the blog feature. now I am faced with a lot of conflicts. I have tried using 'git rebase' but the commits are up to 80 commits per branch, so it not practical. How can I resolve these conflicts?

enter image description here


Solution

  • From the comments, this appeared to be the solution. You had three branches:

    • Master (M)
    • Old feature branch (O)
    • New feature branch (N)

    N was branched off O accidentally, rather than M, which is what you intended. This means that there is a series of commits starting at a certain point in time in N and you want to recreate the branch with those commits as if you had branched from M.

    The solution was to create a new branch:

    • Newer feature branch (N2 from M)

    Then identify a series of commit hashes from N, and apply them to N2. You can do that manually by cherry-picking each one in turn.

    There was a risk that these would not apply cleanly, because M might have changed some files compared to O, and thus your work in N would have been based on outdated files/folders. Text change merging is a fairly simple algorithm, it does not understand code structure like humans can (in other words, not everything is mergeable).

    However, as it turned out, the cherry-picks applied cleanly. If you had not been able to do that, you might have had to create a diff for each failing one, and see how to apply it manually to N2 given the changes in M. Where this happens, it is worth still trying to cherry-pick following commits, as they might not all need manual conflict resolution.