Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Handle Merge Conflicts | Collaborating with Upstream
Git for Open Source Contributors

bookHandle Merge Conflicts

When collaborating on open source projects, you will often work alongside other contributors who may be making changes to the same files as you. This can sometimes lead to merge conflicts when Git is unable to automatically combine changes from different branches. Understanding why these conflicts happen is essential for maintaining a smooth workflow. The most common causes of merge conflicts in open source workflows include:

  • Multiple contributors editing the same lines in a file;
  • One contributor renaming, deleting, or moving a file that another contributor is editing;
  • Divergent project histories where changes are made in parallel without regular syncing;
  • Large or fast-moving projects where updates happen frequently.

Knowing how to identify and resolve these conflicts is a key skill for any open source contributor.

1234567891011121314151617181920212223242526272829303132
# Suppose you and another contributor both edit README.md on different branches. # You attempt to merge their branch into yours and encounter a conflict. # Git will indicate the conflict: $ git merge feature-branch Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result. # To investigate, use git status: $ git status On branch main You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: README.md # Use git diff to see the conflicting changes: $ git diff diff --cc README.md index e69de29..a3c9e3b --- a/README.md +++ b/README.md @@@ -1,4 -1,4 +1,8 @@@ + <<<<<<< HEAD + Your changes to the README + ======= + Their changes to the README + >>>>>>> feature-branch
copy

When you encounter a merge conflict, Git will mark the conflicting areas in the affected files using special markers like <<<<<<<, =======, and >>>>>>>. To resolve the conflict, follow these steps:

  1. Open the conflicted file and review the changes between the HEAD (your branch) and the incoming branch;
  2. Edit the file to combine or choose the desired changes, and remove all conflict markers;
  3. Save the resolved file;
  4. Use git add <filename> to stage the resolved file, signaling to Git that you have handled the conflict;
  5. Complete the process with git commit, which finalizes the merge and records your resolution.

By following this process, you help maintain a clean project history and prevent confusion for other contributors.

question mark

What is the typical first step when you encounter a merge conflict in Git?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

bookHandle Merge Conflicts

Glissez pour afficher le menu

When collaborating on open source projects, you will often work alongside other contributors who may be making changes to the same files as you. This can sometimes lead to merge conflicts when Git is unable to automatically combine changes from different branches. Understanding why these conflicts happen is essential for maintaining a smooth workflow. The most common causes of merge conflicts in open source workflows include:

  • Multiple contributors editing the same lines in a file;
  • One contributor renaming, deleting, or moving a file that another contributor is editing;
  • Divergent project histories where changes are made in parallel without regular syncing;
  • Large or fast-moving projects where updates happen frequently.

Knowing how to identify and resolve these conflicts is a key skill for any open source contributor.

1234567891011121314151617181920212223242526272829303132
# Suppose you and another contributor both edit README.md on different branches. # You attempt to merge their branch into yours and encounter a conflict. # Git will indicate the conflict: $ git merge feature-branch Auto-merging README.md CONFLICT (content): Merge conflict in README.md Automatic merge failed; fix conflicts and then commit the result. # To investigate, use git status: $ git status On branch main You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: README.md # Use git diff to see the conflicting changes: $ git diff diff --cc README.md index e69de29..a3c9e3b --- a/README.md +++ b/README.md @@@ -1,4 -1,4 +1,8 @@@ + <<<<<<< HEAD + Your changes to the README + ======= + Their changes to the README + >>>>>>> feature-branch
copy

When you encounter a merge conflict, Git will mark the conflicting areas in the affected files using special markers like <<<<<<<, =======, and >>>>>>>. To resolve the conflict, follow these steps:

  1. Open the conflicted file and review the changes between the HEAD (your branch) and the incoming branch;
  2. Edit the file to combine or choose the desired changes, and remove all conflict markers;
  3. Save the resolved file;
  4. Use git add <filename> to stage the resolved file, signaling to Git that you have handled the conflict;
  5. Complete the process with git commit, which finalizes the merge and records your resolution.

By following this process, you help maintain a clean project history and prevent confusion for other contributors.

question mark

What is the typical first step when you encounter a merge conflict in Git?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 4
some-alt