Resolve merge conflicts

This page is still a work in progress. The published sections are valid but more documentation is on the way.

Find the best method for you to resolve merge conflicts. Because no matter how careful you are, if you're working with branches, sooner or later you're going to run into a merge conflict.

 

 

このページの内容

関連ページ

Resolving a merge conflict in Git from the command line

Starting from a pull request

Working with branches and pull requests is one of the best ways to get the most you can out of both Git and Bitbucket Cloud. However, it does occasionally lead to the dreaded merge conflict.

When you create a pull request Bitbucket automatically compares the two branches (or repositories in the case of forks) and will notify you of any conflicts before you attempt to merge. It looks like someone modified the destination (development) branch in the same file I modified in the source (new-names) branch.

Resolving the conflict

In the following example we want to resolve a conflict between the destination (development) branch and the source (new-names) branch which was detected during a pull request without merging and closing the pull request.

  1. Make sure you're in your repository directory.

    cd ~/repo-directory

    The result might look something like this:

    computer:~ emmap$ cd ~/mars-repository
    computer:mars-repository emmap$
  2. Check out and pull the most recent version of the destination (development) branch from the remote.

     git checkout destination-branch-name 
     git pull origin destination-branch-name 

    The result will look something like this:

    computer:mars-repository emmap$ git pull origin development
    remote: Counting objects: 6, done.
    remote: Compressing objects: 100% (6/6), done.
    remote: Total 6 (delta 4), reused 0 (delta 0)
    Unpacking objects: 100% (6/6), done.
    From bitbucket.org:teamsinspace/mars-repository
     * branch            development -> FETCH_HEAD
       9a5f37c..fffba53  development -> origin/development
    Updating 9a5f37c..fffba53
    Fast-forward
     team_contact_info.txt | 2 +-
     1 file changed, 1 insertion(+), 1 deletion(-)

    Now you have all history for both branches. You want to resolve the conflict in the development branch, this way it will show up as another commit in the pull request.

  3. Check out the source (new-branch) branch.

    git checkout source-branch-name

    The result will look something like this:

    computer:mars-repository emmap$ git checkout new-branch
    Switched to branch 'new-branch'

    You might want to do a quick pull from the remote (origin) repository to be sure you have all the commits from the source branch.

    computer:mars-repository emmap$ git pull origin new-branch
    From .bitbucket.org:teamsinspace/mars-repository
     * branch            new-branch  -> FETCH_HEAD
    Already up-to-date.
  4. Merge the master branch into the new-names branch.

    git merge destination-branch-name

    The result will look something like this:

    computer:space-repository dstevens$ git merge master
    Auto-merging team_contact_info.txt
    CONFLICT (content): Merge conflict in team_contact_info.txt
    Automatic merge failed; fix conflicts and then commit the result.

    Part of the git process is that it will annotate your file with the conflict markers so you can open the file in your favorite editor or IDE and resolve the conflicts.

  5. Open the file and resolve the conflict. You can do this using the command line or you can navigate to the file.
    The result will look something like this:

    A. The beginning of the change in the HEAD (new-names) branch. In this case, HEAD represents the active branch into which you are merging. 
    B. The end of the change in the active or source branch (new-names) and the beginning of the change in the new names branch.
    C. The end of the change in the destination (development branch).

  6. Resolve the conflict by doing the following:
    1. Correct the content.
    2. Remove the change designations added by Git (1, 2, and 3).
    3. Save the file.
      The result will look something like this:
  7. Add and commit the change.

    git add filename.txt
    git commit -m'commit message'

    The result will look something like this:

    computer:mars-repository emmap$ git add team_contact_info.txt 
    computer:mars-repository emmap$ git commit -m 'resolving conflicts'
    [new-names a541942] resolving conflicts
    
  8. Push the change to the remote.

    git push origin source-branch-name

    The result will look something like this:

    computer:mars-repository emmap$ git push origin new-branch
    Counting objects: 1, done.
    Compressing objects: 100% (1/1), done.
    Writing objects: 100% (1/1), 219 bytes | 0 bytes/s, done.
    Total 1 (delta 0), reused 0 (delta 0)
    To git-@bitbucket.org:teamsinspace/mars-repository.git
       590891f..a541942  new-names -> new-names
  9. Check your pull request (if you are working within a pull request) and you should see the conflicts resolved but the pull request still open.

最終更新日 2017 年 7 月 17 日

この翻訳に満足しましたか?

はい
いいえ
この記事についてのフィードバックを送信する

お探しの情報が見つかりませんか?

コミュニティへの質問

Powered by Confluence and Scroll Viewport.