Resolve merge conflicts

When you create a pull request Bitbucket automatically compares the source with your update and the destination with the original code. If anyone else has made changes in the destination to the same code you touched, we'll notify you of conflicts when you attempt to merge.

When you have merge conflicts, you can't click the Merge button from the pull request to merge. To resolve these conflicts, you pull the changes to your local repository and fix them there. 

Resolving the conflict between Git branches

These steps include details for resolving conflicts between two branches in a Git repository. You'll see references to these placeholders:

  • The directory to the forked repository as <repo_directory>

  • The destination branch as <destination_branch>

  • The source branch as <feature_branch>

  • The file with resolved conflicts as <filename>

Expand for details

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

    1 $ cd ~/<repo_directory>

    For example, if your repository name is my-repository, the result might look something like this:

    1 2 computer:$ cd ~/my-repository computer:my-repository emmap$
  2. Pull the most recent version of the repository from Bitbucket.

    1 $ git pull
  3. Checkout the source branch.

    1 $ git checkout <feature_branch>
  4. Pull the destination branch into the source branch. At this point, pulling the destination will try to merge it with the source and reveal all the conflicts.

    1 $ git pull origin <destination_branch>

    For example, if your destination branch is main, the result will look something like this:

    1 2 3 4 5 6 computer:my-repository emmap$ git pull origin main * branch main -> FETCH_HEAD 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.

    When you merge two branches with conflicts locally, you'll get conflict markers in the file when you open your editor.

  5. Open the file to resolve the conflict. You can do this using the command line or you can navigate to the file.

  6. Resolve the conflict by doing the following:

    • Remove the change designations added by Git

    • Correct the content

    • Save the file

  7. Add and commit the change.

    1 2 $ git add <filename> $ git commit -m'commit message'
  8. Push the change to the remote.

    1 git push origin <feature_branch>

When you check the pull request, the pull request will still be open and you'll no longer see any merge conflicts.

Resolving the conflict between Git forks

These steps include details for resolving conflicts between a forked repository and its original Git repository. You'll see references to these placeholders:

  • The directory to the forked repository as <repo_directory>

  • The URL of the original repository as <original_repo_URL>

  • The file with resolved conflicts as <filename>

  • The destination branch of the forked repository as <destination_branch>

These instructions describe how to resolve conflicts in the forked repository by pulling updates from the original repository. Alternatively, you can pull the forked repository into the original repository to resolve conflicts there, which automatically merges the pull request when you push.

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

    1 $ cd ~/<repo_directory>

    For example, if your repository name is my-repository, the result might look something like this:

    1 2 computer:$ cd ~/my-repository computer:my-repository$
  2. If anyone else has been working on the forked repository, make sure you've got the most recent version of the repository from Bitbucket.

    1 $ git pull

     

  3. Pull the destination repository to your local repository to merge it with the forked repository.

    1. From the repository, copy the URL.

    2. Enter the repository URL with git pull at the command line:

      1 $ git pull <original_repo_URL>

      The result will look something like this:

      1 2 3 4 5 6 7 8 9 10 11 computer:my-repository emmap$ git pull git@bitbucket.org:user/myteamquotes.git remote: Counting objects: 219, done. remote: Compressing objects: 100% (219/219), done. remote: Total 219 (delta 137), reused 0 (delta 0) Receiving objects: 100% (219/219), 565.11 KiB | 382.00 KiB/s, done. Resolving deltas: 100% (137/137), completed with 2 local objects. From bitbucket.org:tutorials/tutorials.git.bitbucket.org * branch HEAD -> FETCH_HEAD Auto-merging editme.html CONFLICT (content): Merge conflict in editme.html Automatic merge failed; fix conflicts and then commit the result.
  4. Open the file to resolve the conflict. You can do this using the command line or you can navigate to the file.

  5. Resolve the conflict by doing the following:

    • Remove the change designations added by Git

    • Correct the content

    • Save the file

  6. Add and commit the change.

    1 2 $ git add <filename> $ git commit -m'commit message'
  7. Push the change to the destination repository.

    1 $ git push origin <destination_branch>

When you check the pull request, the pull request will still be open and you'll no longer see any merge conflicts.

Still need help?

The Atlassian Community is here for you.