Git is one of the most popular distributed version control system that enable software developers and multiple team members in various industries to work simultaneously on similar projects. It helps the developers as well as non-technical professionals to keep track of their project files. It makes it easier for multiple individuals to work together, and it plays a major role in big projects that involve large teams.
When it comes to working with git one of the most intimidating elements of the entire learning curve is how to manage git conflicts. Since many users work simultaneously from various locations on the same file, however, you may end up with a merge conflict. To understand this concept let’s take one example where consider you and your friend are working on a similar project and your friend made some changes to your repository and pushed the changes to git remote. At the same time you also made some changes on same line of code. When you pull their changes in your local repository, you will notice a conflict. This happens because Git has no idea that whether your friend’s version is the updated version or yours. This is what we call a git conflict. When this happens the developer generally sees following error during merge.
Auto-merging [filename1] CONFLICT (content): Merge conflict in [filename1] Automatic merge failed; fix conflicts and then commit the result.
Resolving git conflicts can take a minute or days. So it’s always recommended to sync your code multiple times a day by committing, pushing, pulling and merging. So in order to resolve or minimize git conflicts you can follow some simple rules as follows.
✓ Rule 1 : Keep your changes small
This is very simple rule to avoid git conflict. Usually the conflict occurs when two team members make changes to same file or same line of code. Make a rule that each pull request can have at most 20 files changed within 200 line addition. When changes are less, there is less chance of having things overlapped. So this will reduce the chances of having conflicts.
✓ Rule 2: Reduce the size of your commits
This means you make a commit for every small thing you do. Commit is free, so make more commits. This is very helpful because it is easy to resolve a small conflict. If you have a conflict that is hundreds of lines long , it will be hard to resolve.
✓ Rule 3: Use short branches
Many big conflicts are result of long-living branches. If you work in your own branch for several days, or even weeks, the risk is high that someone else will change parts of the code you touched in another branch. That would lead to merge conflicts. On the other hand, if you have a short-living branch, that means if it is merged back into the master branch after a few hours, there will probably be no conflict. Although there is a conflict, it can generally be resolved very quickly because there is far less code to think about.
At the end of the day always do the following when you are working on your branch
git checkout master git fetch git pull --rebase origin master git checkout - git rebase master
Now you get the latest master, so go back to your previous branch and merge with latest master.
✓ Rule 4: Strong Communication
If all your teammates know what you are working on and which parts of the code you are using they will try to not use the same code at the same time. If another developer wants to change the line of code which you are working on then it would be a good idea to work together.
✓ Rule 5: Mob Programming
This is the most powerful rule of all because it can eliminate conflicts completely. It simply means that whole team is working together on same computer. It makes the conflict impossible because all are working on the same branch.
✓ Useful tip
If you are still facing the git conflicts after trying all the above rules then, you always have an option to undo a merge conflict to the state before. For this most commands come with –abort option. For example. git merge –abort, git rebase –abort
✓ You can use them like :
$ git merge --abort $ git status On branch main nothing to commit, working tree clean
this will give you the confidence that you can always abort, return to clean state and start over. I hope you face less git conflicts or completely avoid them.