A number of months in the past, I wrote a DEV put up about resolving merge commits. The resounding suggestions I acquired from readers was: “JUST AVOID THEM.” Whereas I agree, I wrote the put up for technologists who had been previous the purpose of prevention.
Merge conflicts are largely inevitable. You’ll expertise multiple merge battle in your profession, however with good communication and planning, you may cut back the variety of merge conflicts you encounter. Let’s focus on how we will do this!
Perceive why merge conflicts occur
I discussed this in an earlier DEV put up, however I believe it is value repeating.
Model management programs, like git, auto-magically handle code contributions. It identifies the change, when it occurred, who made it, and on what line in order that builders can simply observe the historical past of their codebase. Nevertheless, git generally will get confused within the following conditions:
- When multiple particular person adjustments the identical line in a file and tries to merge the change to the identical department
- When a developer deletes a file, however one other developer edits it, and so they each attempt to merge their adjustments to the identical department.
- When a developer deletes a line, however one other developer edits it, and so they each attempt to merge their adjustments to the identical department
- When a developer is cherry-picking a commit, which is the act of selecting a commit from a department and making use of it to a different
- When a developer is rebasing a department, which is the method of shifting a sequence of commits to a base commit
Git is uncertain which change to use, so it leans on the developer for assist and notifies them of a merge battle. Your job is to assist git decide which proposed change is most correct and updated.
The advantages of stopping merge conflicts
Generally, doing that upfront work in your group or your self to keep away from merge conflicts could appear tedious, however it’s worthwhile. Utilizing guard rails to forestall merge conflicts will save time and improve developer happiness. Fixing a bug, writing a brand new characteristic, or scripting automation is time-consuming. Including the barrier of debugging and resolving a merge battle means it takes longer to merge to ‘principal’ and longer to deploy to manufacturing. Additionally, in case your group is continually experiencing conflicts, they will finally really feel disenchanted with their work.
4 methods to forestall merge conflicts
Standardize formatting guidelines
Many instances conflicts happen due to formatting discrepancies. For instance, a technologist inadvertently added further white area on the identical line the place one other developer added new code. Additionally, individuals have completely different coding types. As an illustration, some JavaScript builders use semicolons, however some do not. In the event you’re engaged on a group, implement code formatters and linting guidelines. Make certain everybody on the group is conscious of those guidelines and instruments to cut back the variety of merge conflicts you expertise as a result of formatting.
Make small commits and continuously evaluation pull requests
I’ll admit one thing tremendous embarrassing. Again within the day (~3 years in the past), I might make pull requests altering over 50 recordsdata, after which I might get aggravated that I had so many merge conflicts. In hindsight, I used to be incorrect. Altering over 50 recordsdata in lower than two weeks elevated the probabilities that different builders additionally made updates to these recordsdata.
Additionally, creating giant pull requests discouraged my teammates from totally and rapidly reviewing my code. My pull request would sit longer than needed as a result of it was a behemoth my teammates most popular to keep away from.
Take my errors as a lesson to make small adjustments, commit them, and have of us evaluation the pull request as quickly as they’re obtainable. This manner, you will have fewer possibilities to alter recordsdata that different teammates are concurrently engaged on.
Rebase, rebase, rebase (early and infrequently)
(I write this hesitantly, and I am making ready for the feedback part to disapprove. Clearly, I do not need peace; I need issues at all times. I am being dramatic and referencing a meme. See under:)
I realized about rebasing in 2019 from a coworker that my group dubbed the “Git-tator” (Git and dictator mixed). He was earnest about enhancing our startup’s git historical past and decreasing the variety of blockers we skilled as a result of merge conflicts. Arguments ensued as a result of many builders had been usually engaged on intensive options concurrently. We even argued over easy conflicts when individuals imported new dependencies for various options in the identical file on the identical line. Thus, he launched us to rebasing, which considerably improved our software program growth workflow.
What’s rebasing?
The git rebase command reapplies adjustments from one department into one other, which is similar to the git merge command. Nevertheless, on this case, git rebase rewrites the commit historical past to provide a straight, linear succession of commits.
How rebasing helps forestall merge conflicts
Rebasing shouldn’t be going to magically take away all merge conflicts. Actually, you could encounter conflicts whereas rebasing. Generally, you’ll have to repeatedly resolve the identical battle whereas rebasing. Nevertheless, merge conflicts occur as a result of a number of adjustments occur to the identical chunk of code concurrently. In the event you rebase your native working department with the default department (principal or grasp), you are rewriting your native commit historical past with the default department’s historical past after which reapplying your adjustments. In lots of conditions, rebasing first after which merging could make teamwork simpler. Rebasing is an choice, however not the one answer.
Watch out with rebasing
Be warned – there are moments when rebasing shouldn’t be beneficial. Rebasing is harmful since you’re rewriting historical past. When rebasing, take note of the adjustments you are accepting since you threat overwriting teammates’ adjustments or together with recordsdata your teammates supposed to delete. You additionally most likely would not wish to rebase a public repository on the primary department as a result of that may inaccurately rewrite the historical past.
In the event you’re new to rebasing or nervous about rebasing, pair with a teammate to sanity test.
How I rebase to keep away from merge conflicts
There isn’t any right workflow – simply most popular ones. Here is the workflow I observe as decided by groups and codebases I’ve labored in:
- If I am working with multiple particular person, I will create a characteristic department from the default department. This manner, we will contribute to the characteristic department in tandem.
- I will make a brand new native department from the default or characteristic department
- I will add and commit new adjustments to my native department
- I will rebase updates from the default or characteristic department
- I will merge adjustments from my native department to the default or characteristic department
Concentrate and talk
No git command or software program instrument can exchange the necessity for communication in engineering groups. Being a superb software program developer and collaborator goes past writing code. Good software program builders talk with teammates. Maintain your group conscious of what recordsdata you may be touching and coordinate together with your Product Supervisor and SCRUM Grasp to keep away from engaged on options that battle with different options.
In the event you’re working alone, faux you are engaged on a group by:
- creating branches
- creating pull requests
- Keep away from permitting pull requests to turn into stale
- Ensure you’re not altering the identical strains of code earlier than merging a previous change
- Set up and observe white formatting guidelines
Working alone should not cease you from training wholesome coding habits, so attempt to preserve your codebase clear! Your codebase might flip into an open supply mission or a mission that you just exhibit in interviews.
In the event you’re studying this and also you’re pondering, “I simply need assistance resolving a battle,” head over to this put up.
Please be aware: I did not exhaust all of the choices for stopping merge conflicts, so remark under with strategies you utilize to keep away from merge conflicts.
Additionally, observe GitHub and me on DEV for extra superior content material.
Keep tuned for a put up I am engaged on that identifies the variations between a merge commit, squash, and rebase.