Search code examples
gitrebasegit-merge-conflict

How do I resolve a branch and orgin/branch that have diverged?


Through a reset to an earlier commit, I cannot get my local branch back in order. I keep getting merge conflicts locally, while the version on remote does not have any conflicts and what I want, is to get an exact copy of the fremote in my local branch.

What I can do is git checkout <last commit> and I will essentially get a copy of the remote in my local directory but then I also get:

$ git checkout bd8b876 
Note: checking out 'bd8b876'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

Which I don't want, as I still need to work on this branch.

I prefer not to start a new branch as the branch name is tied in with a script that is linked to the JIRA ticket too. If there's no other way though, I will do that.

What I've tried to resolve the issue:

git reset --hard HEAD

is now at 8dc6510 Update main.py

git status
    On branch OS-4055-header_bar_Drive_parameters
    Your branch and 'origin/branch' have diverged,
    and have 19 and 3 different commits each, respectively.
      (use "git pull" to merge the remote branch into yours)
    nothing to commit, working directory clean

but this would always leave my local files in a merged state. I figured that pull in a combination of fetch and merge.

I also tried (after reset): $ git fetch origin branch:branch but even that would not give me the expected result. I checked the branch out on another syste and the files came in as expected (w/o merge conflicts). How do I get my local branch working copy into that state without the need to entirely delete my source directory? `


Solution

  • I keep getting merge conflicts locally, while the version on remote does not have any conflicts and what I want, is to get an exact copy of the fremote in my local branch.

    So you want to keep whatever is in the remote as the "good" stuff. Just perform a new clone and delete your current working directory.

    How do I get my local branch working copy into that state without the need to entirely delete my source directory?

    So you don't want to do that, but either way you'll have to remove your local commits that diverged, because they diverged and you want to keep whatever is in the remote.

    git commit --amend all local commits that diverged, or even git reset --hard until you effectively deleted all local commits. You can achieve deletion of "commits" in a number of way. Do this until you get to the last common commit your local and remote share.

    Once you are here, if any file is modified, then either git stash it or git checkout -- it to revert it to the HEAD original state.

    Now, git pull will fetch and merge all that is in remote that you so desperately wanted.

    Re-apply any wanted change you stashed, create a new commit and push it to remote. Repeat until Jira issue is closed ;)

    BTW: I'm still unconvinced about how your local and remote histories diverged. I hope you put yourself in that situation and nobody else is pushing to your same branch without a merging strategy...