Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / DevOps

Git – SourceTree Custom Actions for “skip-worktree” Option

5.00/5 (2 votes)
3 Jul 2023CPOL2 min read 5.5K  
We show how to add to SourceTree custom menus to manage “skip-worktree” option
Very popular Git GUI SourceTree can be extended with custom actions for easy use of “skip-worktree” Git option. This method, different from .gitignore, is planned to be applied to files that you want TRACKED, LOCAL CHANGES IGNORED.

1. Problem

If you have a config file like web.config that needs to be TRACKED by Git, but want LOCAL CHANGES IGNORED, that situation cannot be solved with .gitignore. The appropriate method is the usage of 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].

2. 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

3. SourceTree Git GUI – Adding Custom Actions

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

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

Image 1

3.1. Ignore Local Changes (skip-worktree)

Here is how you setup this option:

Image 2

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

Image 3

3.2. UnIgnore Local Changes (skip-worktree)

Here is how you setup this option:

Image 4

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

Image 5

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

Image 6

Image 7

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

Image 8

4. 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 the usage of skip-worktree option in Git.

Very popular Git GUI SourceTree can be extended with custom actions for easy use of skip-worktree Git option.

5. References

6. History

  • 3rd July, 2023: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)