Working with git – Ignoring Merge Conflicts for a File

7An issue I had to solve recently with git was how to ignore merge-conflicts for a single file in a repository.  The application is an AngularJS application, and as is common with a Single Page Application (SPA) there is a single index.html file that is the starting point of the app.  As part of the client-side build tooling, we are using two gulp plugins called wiredep and inject.  These two plugins automatically add the <script> references to the index.html file for all of the javascript files in your application – including both the referenced libraries (e.g. angular.js) and the application specific js files (controllers, services etc).  As a result, even though the developers almost never directly change the index.html file, it gets modified by the gulp tasks when the <script> references are added.  So with each commit, git considers the index.html file to have been modified.

What the developers were seeing was that each time they synced with the repository, there were merge conflicts in the index.html file in the sections where the <script> references were automatically added that had to be resolved.  However, since these sections were regenerated by the gulp tasks, the merge conflicts could safely be ignored.
What we needed was a way to tell git to ignore specific sections of the file.  However, since there is currently no way to configure git to ignore part of a file, the solution we found was to define a default merge-conflict resolution for the index.html file.  Basically, we needed to tell git that for this one file, whenever there is a conflict, take my version of the file.
The way this is done is in two steps:
  1. Define a custom merge driver for the index.html file in the .gitattributes file:
    web/app/index.html merge=ours
  2. Next, update your .gitconfig file with the definition of the “ours” merge driver: ok.png

     

Now anytime there is a merge conflict with index.html file, git will automatically resolve it by using your changes.
This strategy works well for us because other than the <script> references the index.html file is 1-small, and 2-rarely changes.  However, if there are parts of the index.html file that are frequently changed by the developers (not the gulp tasks) then this solution may not be ideal for you.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s