I have a question regarding the GitHub flow and specifically about how to manage multiple features developed at the same time.
Let assume we have a simple CSS file.
.container{
height: 500px;
width: 500px;
background-color: blue;
color: white;
}
Let assume developer A is in charge of changing the background color to red while developer B is in charge of changing the font color to black.
Developer A creates its feature branch "red-background", updates the CSS file and commits the change in its branch. The CSS file in "red-background" branch is now:
.container{
height: 500px;
width: 500px;
background-color: red;
color: white;
}
Developer B creates its feature branch "black-font", updates the CSS file and commits the change in its branch. The CSS file in "black-font" branch is now:
.container{
height: 500px;
width: 500px;
background-color: blue;
color: black;
}
Now if the reviewer merges "red-background" to main and then "black-font" to main, a git-merge conflict arises :
.container{
height: 500px;
width: 500px;
<<<<<<< HEAD
background-color: red;
color: white;
=======
background-color: blue;
color: black;
>>>>>>> black-font
}
How to avoid conflict in those types of situations?
Don't collaborate using Git.
Seriously, this is what Git is / does. If you want to "avoid" it, avoid Git, except for degenerate uses like only one person has access to the repo. If you're going to collaborate, you're going to get merge conflicts sometimes. One person changing a line and another person changing an adjacent line, that's a conflict. That's just life.
But conflicts are not bad. They are not things to be avoided. So, ignoring your actual question, another solution is: don't avoid them. Resolve the conflict. Git is asking for your help. Help Git, and go on with life.
A common technique for making the history cleaner is to reverse merge just before merging the PR. That way, there might still be a conflict to resolve, but you resolve it during the reverse merge, in a commit on the PR branch, and the actual merge to the primary branch, closing the PR, has no conflict. But then, once again, you are not "avoiding" the conflict. You are managing it, which is not what you asked to do.