How to resolve conflicts in git?
GIT is a powerful version control system. During code development, merging is an unavoidable process. A git automatic merge may have conflicts. In this tutorial, we will discuss how to resolve conflicts in git.
What we will cover in git resolve conflicts?
- What is the merge in git?
- What are the merge conflicts?
- How we can avoid merge conflicts?
- How we can resolve merge conflicts with an example?
What is the merge in git?
While working on a project, more than one developer 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, git can not merge the files. The manual intervention of the developer is required to resolve the conflicts. Here we will discuss what is merge conflicts?
When two or more developers have modified the same lines in the same file, git will not able to understand which code is the final code, when merges the file. This is the merge conflicts.
How we can avoid merge conflicts?
While developing code in parallel may lead to conflicts at some point in time. 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 few guidelines to avoid conflicts.
- Update the local copy frequently. So that you will have all other’s codes. This will reduce the updating of the same lines, those already updated 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 we can resolve merge conflicts with an example?
Once a git automatic merge is 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 a 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 start and committed and pushed.
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 email@example.com If the identity used for this commit is wrong, you can fix it with: git commit --amend --author='Your Name <firstname.lastname@example.org>' [root@CentOS_6_64-160 myrepository]#
The above commit the merged file.