Resolving a merge conflict using the command line – GitHub Docs

Merge conflicts occur when competitive changes are made to the same line in a file, or when one person edits a file and another person deletes the same file. For more information, see “About Merge Conflicts.”

Competitor Line Change Merge Conflicts

To resolve a merge conflict

caused by competing line changes, you must choose which changes to incorporate from different branches in a new commit.

For example, if you and someone else edited the styleguide.md file on the same lines in different branches of the same Git repository, you will get a merge conflict error when you try to merge these branches. You must resolve this merge conflict with a new commit before you can merge these branches.

  1. Open TerminalTerminalGit Bash

  2. .

  3. Navigate to the local Git repository that has the

  4. merge conflict. cd REPOSITORY NAME

  5. Generate a list of files affected by the merge conflict. In this example, the styleguide.md file has a merge conflict.

    $ git status > # In the branch b-b > # You have unmerged paths. > # (troubleshoot conflicts and run “git commit”) > # > # Unmerged paths: > # (use “git add…” to mark the resolution) > # > # both modified: styleguide.md > # > No changes were added to commit (use “git add” and/or “git commit -a”)

  6. Open your favorite text editor, such as Visual Studio Code, and navigate to the file that has merge conflicts

  7. .

  8. To see the beginning of the merge conflict in the file, look in the file for the conflict marker <<<<<<<. When you open the file in your text editor, you will see the changes of the HEAD or base branch after the <<<<<<< HEAD line. Next, you’ll see =======, which splits your changes from the changes in the other branch, followed by >>>>>>> BRANCH-NAME. In this example, one person typed “open a problem” in the base branch or HEAD and another person typed “ask your question on IRC” in the comparison branch or branch-a.

    If you have questions, please <<<<<<< HEAD open a problem ======= ask your question on IRC. >>>>>>> branch-a

  9. Decide whether you want to keep only changes from your branch, keep only changes from the other branch, or make a new change, which can incorporate changes from both branches. Delete the conflict markers <<<<<<<, =======, >>>>>>> and make any changes you want to the final merge. In this example, both changes are incorporated into the final combination: If you

    have questions, open a problem or ask in our IRC channel if it is more urgent.

  10. Add or organize

  11. changes. $ git add .

  12. Confirm the changes with a comment.

    $ git commit -m “Resolved the merge conflict by incorporating both suggestions.”

You can now merge branches on the command line or push your changes to your remote repository on GitHub and merge your changes into a pull request.

Merge conflicts of deleted

files

To resolve a merge conflict caused by competing changes to a file, where one person deletes a file in a branch and another person edits the same file, you must choose whether to delete or keep the deleted file in a new confirmation.

For example, if you have edited a file, as README.md, and someone else deleted the same file in another branch in the same Git repository, you will get a merge conflict error when you try to merge these branches. You must resolve this merge conflict with a new commit before you can merge these branches.

  1. Open TerminalTerminalGit Bash

  2. .

  3. Navigate to the local Git repository that has the

  4. merge conflict. cd REPOSITORY NAME

  5. Generate a list of files affected by the merge conflict. In this example, the README.md file has a merge conflict.

    $ git status > # In the main branch > # Its branch and ‘origin/main’ have diverged, > # and have 1 and 2 different commits each, respectively. > # (use “git pull” to merge the remote branch with yours) > # It has unmerged paths. > # (troubleshoot conflicts and run “git commit”) > # > # Unmerged paths: > # (use “git add/rm…” as appropriate to mark resolution) > #># removed by us: README.md > # > # No changes were added to commit (use “git add” and/or “git commit -a”)

  6. Open your favorite text editor, such as Visual Studio Code, and navigate to the file that has merge conflicts

  7. .

  8. Decide if you want to keep the deleted file. You may want to see the latest changes made to the deleted file in your text editor.

    To

    add the deleted file back

    to the repository: $ git add README.md

    To delete this file from the repository

    : $ git rm README.md > README.md: you need to merge > rm

  9. ‘README.md’

  10. Confirm the changes with a comment.

    $ git commit -m “Merge conflict was resolved by keeping README.md file.” > [branch-d 6f89e49] Merge branch ‘branch-c’ into branch d

You can now merge branches on the command line or push your changes to your remote repository on GitHub and merge your changes into a pull request.

Further reading

  • About Merge Conflicts
  • ” “

  • Checking pull requests locally”

Contact US