Merge Conflicts
What is a Merge Conflict
It is when your local copy of your project is older then the GitHub version. How does this happen, you ask? If one person commits a change on the same file you are working on, that change doesn't automatically sync with your local copy. If you attempt to commit changes on the same file, a merge conflict happens because GItHub doesn't know which version is correct. Clicking "Sync" before commiting can fix miner merge conflicts, but sometimes it can't be avoided.
Step-By-Step
Example of a Merge Conflict
- Sync a local copy from the GitHub Desktop
- Open a file from your project using a text editor or IDE of choice.
- Let's say that a team member decides to work on the same file and commits a few changes to the existing code.
- In the meantime, you just finished writing an update to your local copy.
- Type a summary of your update.
- Click "Commit to master".
- Click "Sync" on the top right-hand side.
- An error message will indicate a merge conflict.
- Click "Close"
Note: This is a local copy that you are working on.
How to fix
- In the center of the application, you will see two options. I recommend clicking "Show in Folder", so you can open the file in conflict from a text editor. You can use the other option if it is a webpage to see the conflict.
- For now click, "Show in Finder".
- Right-click the file showing merge conflict.
- Open with your favorite text editor.
- You should see something simliar to this.
- <<<<<< HEAD: Represents the begining of a merge conflict.
- ======: Represents where two merged files are different.
- >>>>>>> origin/master: Represents where the merge conflict ends.
- Check through the GitHub commit history to see who commited last, then talk with the team members involved in the merge conflict.
- After discussion, correct the conflict by deleting the no longer used code and remove the headers.
- Type a summary of update.
- Click "Commit to master".
- Click "Sync" on the top right-hand side.
- Now the merge conflict is resolved.
Notes: You probably notice some weird looking markers. These markers represent the merge conflict.
Markers
In our example between the <<<<<< HEAD and ====== this is our version of the code and between ==== and >>>>> is the new version.
Note: Not all merge conflicts look the same, so the key is communicating with your team and reviewing the code in conflict.