After a Git merge conflict, a lot of files I didn't touch become changes to be committed

前端 未结 6 1383
忘掉有多难
忘掉有多难 2020-12-05 09:47

So I\'m working in a branch, make some changes, and run git merge master. I get a merge conflict on one of the files I modified (which I know how to deal with),

6条回答
  •  我在风中等你
    2020-12-05 10:17

    The reason this happened is because you most likely did the opposite of what you were intending to do.

    Let's suppose your working branch is called topic-branch.

    Instead of doing:

    $ git merge master

    you could have done:

    $ git checkout master
    $ git merge topic-branch
    

    In English, instead of merging the master branch into topic-branch you could have merged topic-branch into master.

    To understand why this achieves the desired result we can examine the statement made in a previous answer:

    When you attempt a merge, all files that can be automatically merged (e.g. where you don't have any changes in your local branch but which have been modified on the source branch), are automatically merged and staged.

    The problem you are having is simply merge trying to do its job. The files aren't changed in your topic branch, but they are on the branch you are merging into it. If you look at this in the opposite direction of merging the topic-branch into master the problem goes away because it only considers the files you've modified.

    Conceptually, here is what merge is doing (more here):

    Let the current head be called current, and the head to be merged called merge.

    1. Identify the common ancestor of current and merge. Call it ancestor-commit.
    2. Deal with the easy cases. If the ancestor-commit equals merge, then do nothing. If ancestor-commit equals current, then do a fast forward merge.
    3. Otherwise, determine the changes between the ancestor-commit and merge.
    4. Attempt to merge those changes into the files in current.
    5. If there were no conflicts, create a new commit, with two parents, current and merge. Set current (and HEAD) to point to this new commit, and update the working files for the project accordingly.
    6. If there was a conflict, insert appropriate conflict markers and inform the user. No commit is created.

提交回复
热议问题