Git – SourceTree custom actions for “skip-worktree” option

Abstract: Very popular Git GUI SourceTree can be extended with custom actions to easily use the “skip-worktree” Git option. This method, different from .gitignore, is planned to be applied to files that you want TRACKED, LOCAL CHANGES IGNORED.

Problem

If you have a config file like web.config that needs to be TRACKED by Git but want LOCAL CHANGES IGNORED, that situation can not be solved with .gitignore. The appropriate method is using the “skip-worktree” option in Git.

The usage of the .gitignore file is very popular and well-described elsewhere, so I will not go into details. Just to mention, this method is planned to be applied to files that you want GLOBALY UNTRACKED. File in .gitignore is completely ignored by Git on all systems using that repository.

The skip-worktree option is a less-known Git option. This method is planned to be applied to files that you want TRACKED, LOCAL CHANGES IGNORED. Use case for having files TRACKED, LOCAL CHANGES IGNORED is very strong. Just to mention the web.config file that every developer ASP.NET will have locally modified with at least a database connection string. You want that file TRACKED since that is an important part of your application, but you want changes to your local system to be ignored since your local version will contain your local settings that are different for each developer on the project.

For more discussion on methods to ignore files in Git, see [1].

Usage of skip-worktree from the command line

Here are the commands you will need for this method:

To ignore local changes to tracked files:
git update-index --skip-worktree  [<file>...]

To track local changes again:
git update-index --no-skip-worktree [<file>...]

To list all files marked with skip-worktree:
git ls-files -v | grep ^S

SourceTree Git GUI – Adding custom actions

A very popular GUI for Git is SourceTree. Out of the box, it does not support the skip-worktree option. Fortunately, it allows for defining custom actions, which we will use to extend its GUI with skip-worktree management tools.

Our goal is to add custom actions like those in this picture:

SourceTree custom actions for “skip-worktree” option 

3.1 Ignore local changes (skip-worktree)

Here is how you setup this option:

SourceTree custom actions for “skip-worktree” option 

And here is a sample execution (via the context menu):

SourceTree custom actions for “skip-worktree” option 

3.2 UnIgnore local changes (skip-worktree)

Here is how you setup this option:

SourceTree custom actions for “skip-worktree” option 

And here is a sample execution (via the context menu):

SourceTree custom actions for “skip-worktree” option 

3.3 List all files marked with skip-worktree

This setup is a bit complicated. It involves the creation of a script ListFilesSkipWorktree.bat.

"c:\Program Files\Git\bin\git.exe" ls-files -v | "C:\WINDOWS\system32\findstr.exe" /b S

SourceTree custom actions for “skip-worktree” option 

SourceTree custom actions for “skip-worktree” option 

And here is a sample execution (via the context menu):

SourceTree custom actions for “skip-worktree” option 

Conclusion

Use case for having files TRACKED, LOCAL CHANGES IGNORED is very strong. Just to mention the web.config file that every developer ASP.NET will have locally modified with at least a database connection string. Usage of .gitignore, in this case, will not solve the problem. The appropriate method is using the “skip-worktree” option in Git.

Very popular Git GUI SourceTree can be extended with custom actions to easily use the “skip-worktree” Git option.

References

  1. https://www.codeproject.com/Articles/5363864/Git-4-Ways-to-Ignore-Files


Similar Articles