Let Git ignore modifications to a file

Introduction

Lately, at Codit.eu Belgium, we have been working on an internal project after hours and during a hack day. The project and the technology allow some of us, myself included, to get out of our shell and gives us a chance to work with technology we are unfamiliar with.

It is a really interesting work environment and this has sparked my interest in some of the things I haven’t had a chance yet to become familiar with. One of those things was Git. I have experience using TFS (Team Foundation Server) on-premise and VSTS (Visual Studio Team Services), in the cloud.

While setting up VSTS, one is give the option to choose for TFVC (Team Foundation Version Control) or Git. Until now, the safe choice had always been TFVC. This project finally let me work with Git and I must say I learned a lot from this!

2-comparison

This post is not an overview on what the differences between the two are (you can find that here), but rather a difficulty I had during development.

Don’t check-in credentials in web.config

The setup and baseline of the projects were in .NET and were done by a colleague of mine familiar with it already. One of the things I encountered was the fact that we worked with Azure DocumentDb and we need to store the DocumentDb URI and key in the web.config file. However, we can’t have them in the file itself as this is in no way secure.

However, when committing or pushing changes in my local branch to the remote server, I always needed to restore the web.config in its original state. Afterwards, to test the local version again, I needed to include them again. A real problem, as this is quite cumbersome and easily forgotten.

Let Git ignore any local changes

I searched the web for a solution and after some testing I found the solution to my problem here, on StackOverflow.

git update-index --skip-worktree <file-name>

Enter the command in a git command prompt. To get one, go to Team Explorer, Changes, Actions and click Open Command Prompt.

git_cmd

The command will update your local git index and mark the file to skip during commits and to assume no changes were made, although you may actually have made them!

Perfect for our web.config which (almost) never changes!
Even more so, since this only marks your local index and the change is never propagated to the remote index! This means that every developer wanting to do so, will have to do it manually his-/herself.
This is not something that a new developer would need to know about immediately, which is a good thing!

However, whenever we need to make an actual change (one that needs to be merged in the master branch), we need to remove the credentials, make our changes and commit and push the changes remotely to create a PR (pull request).

To do so, use this command in another Git command prompt:

git update-index --no-skip-worktree <file>

Hope you enjoyed this, as this made my life a lot easier on this project!

Advertisements

2 thoughts on “Let Git ignore modifications to a file

  1. Another approach to handle this project is by excluding the web.config using gitignore. With a small project file change, you could generate the web.config file using transformations on build. To achieve this, you should create a web.base.config that is a copy of your original web.config file but without any values. Then you will have your own web.debug.config file (part of gitignore) that includes sensitive information and will generate a web.config file with values after transformation (when building in Debug mode). The approach works well because you could never make the mistake to commit sensitive data and it is used in this project: https://github.com/CoditEU/lunchorder/tree/master/backend/WebApi/Lunchorder.Api

    Liked by 1 person

  2. Pingback: TMIL Nov 2016- This Month I Learned | The Great Integration - Pieter Vandenheede

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