How to resolve conflicts in Git?
GIT is a powerful version control system. During any software development, source code merging is an unavoidable process. Git merges automatically into the local copy when you pull the changes from the central repository. But git merge may result in conflicts if something is updated at the same place in the central and local repository. In this tutorial, we will discuss how to resolve conflicts in Git if occurred.
What will we cover in git resolve conflicts?
- What is the merge in Git?
- What are the merge conflicts?
- How can we avoid merge conflicts?
- How can we resolve merge conflicts with an example?
What is the merge in Git?
While working on a project, multiple developers may add/modify a common file. Merge is the automatic process in GIT to keep all changes in the file. Suppose a developer has added an additional method in a file named math.java and another developer added a subtract method in math.java. After the merge, the math.java file will have both functions.
What is a merge conflict?
Automatic merging is fine until there are no conflicts. When conflicts occur, it can not merge the files. The manual intervention of the developer is required to resolve the disputes. Here we will discuss what merge conflicts are.
When two or more developers have modified the same lines in the same file, Git cannot understand which code is the final code when merging the file. This is the merge conflict.
How can we avoid merge conflicts?
While developing code in parallel may lead to conflicts at some point. But still, there are ways where we can reduce the number of conflicts or may avoid them completely. To avoid merge conflicts is simple. The following are a few guidelines to prevent conflicts.
- Update the local copy frequently to have all other’s codes. This will reduce updating the same lines, those already edited by the others.
- Push your changes more frequently. So that others will have the latest copy. Before pushing the code, make sure this will not break any compilation.
How can we resolve merge conflicts with an example?
Once a git automatic merge failed. The only option is to merge changes manually in the files having conflicts. The example will show the following.
- We have a repository with a master with two empty files
- We have cloned the repository on a local Linux machine.
- A new branch name
mybranchhas been created.
firstFile.cis modified(added text at the beginning of the file). The changed file is committed and pushed into a central repository.
- Switch to the
first file.cis empty. Add a text in the file at the beginning, do commit, and push.
git checkout mybranch Switched to branch 'mybranch' [root@CentOS_6_64-160 myrepository]# ls -lrt total 0 -rw-r--r-- 1 root root 0 Jan 14 04:07 secondFile.c -rw-r--r-- 1 root root 0 Jan 14 04:25 firstFile.c [root@CentOS_6_64-160 myrepository]# vim firstFile.c [root@CentOS_6_64-160 myrepository]#
- Merge the master into
[root@CentOS_6_64-160 myrepository]# git merge master Auto-merging firstFile.c CONFLICT (content): Merge conflict in firstFile.c Automatic merge failed; fix conflicts and then commit the result. [root@CentOS_6_64-160 myrepository]# vim firstFile.c <<<<<<< HEAD Text written by mybranch ======= This text is written by master >>>>>>> master /* After editing file . Text written by mybranch and master;
[root@CentOS_6_64-160 myrepository]# git add firstFile.c [root@CentOS_6_64-160 myrepository]# git commit -m "Merged master fixed conflict." [mybranch 0419487] Merged master fixed conflict. Committer: root <root@CentOS_6_64-160.(none)> Your name and email address were configured automatically based on your username and hostname. Please check that they are accurate. You can suppress this message by setting them explicitly: git config --global user.name "Your Name" git config --global user.email firstname.lastname@example.org If the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name <email@example.com>' [root@CentOS_6_64-160 myrepository]#
The above commit is for the merged file.