Git merge conflict when folder is missing

I have master branch which contains folder A, B, C. I checkout a feature-branch from master. Then I delete folder B in feature-branch and add new files to folder C in feature-branch. Then, I try merging the feature-branch into master. Will there be merge conflict? If yes, how shall I avoid it.


Solution 1:

A conflict has to be between two things. In version control, we talk about a "merge conflict" between two branches which we want to merge together, which occurs when each of them has changed the same thing in different ways.

If you have two different lines in a file, or two different files, or two different folders, then you can edit them each in a different branch, and the merge will combine the two changes - that's why it's called a "merge". The conflict part comes when there is no way to merge the changes, because they contradict each other, like if you edit a file in one branch, but delete it in the other.

Solution 2:

Will there be a merge conflict?

Strictly speaking no, because you said nothing about committing to the master branch or merging in some other commits (i.e. by fetching from a remote repository) into master while working on you feature branch.

However if you did any of the above then the following operations on the master branch would lead to merge conflicts:

  • modifiying a file in folder B
  • adding a new file in folder C with the same name but different content as one of the files you added in your feature branch

If yes, how shall I avoid it.

Don't have any commits going into master while working on your feature branch.

Of course, any serious workflow will not permit this blocking of master so there is always the chance of conflicts. Personally I like rebasing my feature branches onto master before having them merged. But that only moves the conflict from the merge process to the rebase process. However the advantage is I can rebase commit by commit. If there are five new commits in master and each of those gives me a conflic it is a pain to rebase directly onto master, because I will have to solve all conflicts at once. If I instead rebase onto the first commit, then onto the second and so on, I can solve the conflicts one-by-one.