Resolve merge conflicts

This page is still a work in progress. The published sections are valid but more documentation awesome 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:~ dstevens$ cd ~/space-repository
    computer:space-repository dstevens$
  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:space-repository dstevens$ git pull origin development
    remote: Counting objects: 3, done.
    remote: Compressing objects: 100% (3/3), done.
    remote: Total 3 (delta 1), reused 0 (delta 0)
    Unpacking objects: 100% (3/3), done.
    From bitbucket.org:teamsinspace/space-repository
     * branch            development -> FETCH_HEAD
       9a5f37c..fffba53  development -> origin/development
    Updating 9a5f37c..fffba53
    Fast-forward
     contributors.txt | 4 +++-
     1 file changed, 3 insertions(+), 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-names) branch.

    git checkout source-branch-name

    The result will look something like this:

    computer:space-repository dstevens$ git checkout new-names
    Switched to branch 'new-names'

    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:space-repository dstevens$ git pull origin new-names
    From .bitbucket.org:teamsinspace/space-repository
     * branch            new-names  -> FETCH_HEAD
    Already up-to-date.
  4. Merge the development branch into the new-names branch.

    git merge destination-branch-name

    The result will look something like this:

    computer:space-repository dstevens$ git merge development
    Auto-merging contributors.txt
    CONFLICT (content): Merge conflict in contributors.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:

  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:space-repository dstevens$ git add contributors.txt 
    computer:space-repository dstevens$ 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:space-repository dstevens$ git push origin new-names
    Counting objects: 9, done.
    Delta compression using up to 8 threads.
    Compressing objects: 100% (3/3), done.
    Writing objects: 100% (3/3), 406 bytes | 0 bytes/s, done.
    Total 3 (delta 1), reused 0 (delta 0)
    To git-@bitbucket.org:teamsinspace/space-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.

Was this helpful?

Thanks for your feedback!

Why was this unhelpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport