Note: this article is published on 06/03/2024.
A - Introduction
When a team works against a repository, team members work parallelly with the same file. When a member merges the change back to master or merges to other branches, the file is changed by more than one user could cause conflict that the merger has to handle. Sometimes, when one user makes the merge, when conflict, he/she does not want to use other changes, just want to keep what he/she has. This article will discuss this issue.
This is the solution from Git Document
![]()
This is an implementation from GitHub [ref] for Git envieonment. This article will demostrate the same procedure in Visual Studio environment.
- A - Introduction
- B - Setup Project and Branches
- C - Problem: Merge with Conflict
- D - Solution
- D-1 - Add a .gitattributes File in the Merged Branch
- D-2 - Setup the Environment
- E - Demo
B - Setup Project and Branches:
Create a project with one file: index.txt
![]()
write context into the file: "Base", save to master Branch:
![]()
Based on master Branch, create a new Branch: A, and add "+A" into the index.txt file:
![]()
Based on master Branch, create a new Branch: B, and add "+B" into the index.txt file:
![]()
Based on master Branch, create a new Branch: C, and add "+C" into the index.txt file:
![]()
C - Problem: Merge with Conflict
Merge A to B:
![]()
we got conflict:
![]()
The conflict:
![]()
D - Solution
As the Git Document suggested, you need two things:
- Add a .gitattributes file in the merged branch
- Setup up the environment
D-1 - Add a .gitattributes File in the Merged Branch
The content will be the merged file name (that we do not want it changed in merge) + merge-ours
index.txt merge=ours
we put the .gitattributes file under Branch C.
D-2 - Setup the Environment:
You can run the command either locally or globally:
Locally:
![]()
Open the file: config
![]()
Run command:
![]()
After run: git config --global merge.ours.driver true
![]()
We can run the command globally:
git config --global merge.ours.driver true
This will change the global setting for Git: previously
![]()
After run: git config --global merge.ours.driver true
![]()
In general, the Git config files are located at
![]()
or
![]()
E - Demo
Remember, we put the .gitattributes file under Branch C. We merge "B" to "C":
![]()
there is still a conflict wrning:
![]()
the merge is completed without conflict, while the index.txt in Branch C is kept the same as previously.
References: