Thursday, May 5, 2022

How to stop and start tracking file changes for Git

Summary: Git commands that let you stop and start tracking project file chages.

During application development, there may be situations when you want to make a change in a file (e.g. modify an application setting) without accidentally committing this change to source control. There may be better ways to do this, but one option is to tell Git to stop tracking the file before you make the the change that you do not commit to the repo. Say, there is an appsettings.Developement.json file that you want to stop and start tracking. This is how I do it.

Create two files stop-tracking-appsettings.bat and start-tracking-appsettings.bat files in the solution folder (PROJECT_FOLDER must be replace by the name of the project directory under the solution folder).

stop-tracking-appsettings.bat

@echo off
git update-index --skip-worktree PROJECT_FOLDER\appsettings.Development.json

start-tracking-appsettings.bat

@echo off
git update-index --no-skip-worktree PROJECT_FOLDER\appsettings.Development.json

Now you can either run these files from a console whenever you want to stop and start tracking file changes. Even better, in Visual Studio, you can create a custom tool menu option (e.g. Run batch file) that you can invoke by right clicking the file in the Solution Explorer and selectin the context menu option (see this Stack Overflow answer explaining how to set it up).