If you are a developer working on Git there are high chances that you have come across the merge conflict error while merging your changes to git repo.
Let's see how we can resolve git conflicts.
Consider the below example where there are 2 developers (developer1
and developer2) working on the same git branch (developer branch in this
example).
Developer1 is working on the below project in his eclipse workspace.
Developer2 is working on the below project in his eclipse workspace.
(See gitconflict2 next to the project name)
Let’s consider a situation where both developer1 and
developer2 are changing the same line of code (line 6) from the file App.java
Developer2 changes the statement on line 6 to “Developer 2”
from “Developer 1” and will commit his code to git.
Commit and Pushed.
If Developer1 tries to pull the latest from repo now, eclipse
will throw the below error which indicates that there is a conflict.
During the same time Developer1 has also changed the same
line 6 from “Developer 1” to “First Developer” and is trying to check-in the
file to git.
Before doing that lets do Team > Synchronize Workspace
As you can see from the above snapshot, there is a conflict
in App.java as there were changes committed to the repo which you do not have
in your file.
Now, let’s see how we can resolve this conflict and merge
our file.
There are several ways to resolve git conflicts and we are
going to see one of those.
- · Developer1 takes a backup of the file which he has changed.
- · Right click on the project > Team > Reset
Select Remote Tracking and select the required
branch (developer in this example)
(Please note that Hard is selected in Reset
type)
- · Click on Reset.
- · Now merge your changes from the backup file.
- · Stage the changes, provide appropriate Commit message and click on Commit and Push.
Changes will be successfully pushed to git repo.
Now if developer2 pulls the latest from repo, he gets the
commit from developer1.
Nice explanation. We face this issue very often in our team where one team member overrides other team member's code during check-in. Will recommend them to follow these steps. Thanks for the post!
ReplyDelete