Search code examples
gitgithubmergegit-fork

Conflicts after merge from Master multiple times


I am new to Git and GitHub and tried to contribute to a project, so I made a fork. The project contains tons of folders with files and the only thing I did is, to add two folders with some files in them and modify one single file. After a while I wanted to keep up to date with the original master and merged from upstream as recommended in https://help.github.com/articles/syncing-a-fork/ - so I did a did a git merge upstream/master. This introduced lots of changes into master of my fork.

Now, some months later I decided to sync my fork again and did a git merge upstream/master again. This brought me in like 10 conflicts, which are on files, which I did not touch myself. Of course I could try to merge them manually, but I want to understand, what's going on, so that I do not destroy others work.

Questions:

  • Why do those conflicts occur on the 2nd merge from upstream?

  • Could I have somehow changed the base of my fork to the top-most (is that what rebase does?)

  • How can I find out the "change paths" of the two conflicting versions (where did they come from?)

If it helps, the following is the fork I am talking about: https://github.com/0815fox/DefinitelyTyped


Solution

    • Likely the original repo was rebased, or your local repo was, and you would need to do a fetch and hard reset to get back in sync.

    • It's very unlikely you unknowingly did this. It's not normally possible, and the only way I can fathom it is to create a bare branch by resetting back to first commit and reverse cherry pick commits one at a time. That is not what rebase does. Rebase can be used to squash commits together and to move new commits behind ancestral commits on an upstream branch for a fast forward merge.

    You can inspect the merge paths of all branches with git log.

    git log --oneline --graph --decorate --all
    

    If you're interested in when a particular line in a file was made, such as when investigating a conflict, use git blame.

    git blame -- filename
    

    My suggestion to fix after you're done investigating is to create a new branch with your changes in tact, reset your upstream branch, then cherry pick your range of commits over.

    git checkout -b mybranch
    git checkout master
    git reset --hard origin/master
    git cherry-pick ^your1stSHA..yourLastSHA