GitTip: Skip WorkTree. This tip is simple and powerful in equal parts, what we want to achieve is to modify a local file and that git could not identify it as having been modified despite being already tracked.

 

GitTip: Skip WorkTree

For this example I have a file “config.txt” with the content: “secret”. This file works well in all environments but in local environment I need to change it for testing. No problem, it is modified, the relevant tests are made, it is reverted and it is uploaded.

The problem comes when that file, for various needs, has to be modified in your local environment and has to work and we shouldn’t upload it in order not to affect others, how can we do that?

My Apiumhub colleague (kudos Álvaro @alvarobiz) told me about the command: “git update-index –assume-unchanged”, I have to say that it fulfilled what I needed, it allowed me to modify a file in a local environment and git did not “detect” it , I could keep making changes in other files and committing files with my favourite “git add .” without worrying about anything.

In the following image we can see how doing a “git status” shows us two files, config.txt and otrofichero.sh. As we said, we don’t want to upload config.txt, it is a change in a local environment and we don’t want to worry about it. After the following command “update-index” the file config.txt disappears from the working area.

Skip WorkTree

The file still exists and git is not going to refresh it unless 2 things happen:

  • Manually deactivate the bit with which we have marked that file (assume-unchanged)
  • Pull and that file has been modified via upstream ← ¡DANGER!

Before this last scenario, we worry about the file, we have to review each pull if the bit has been removed or not with the command “git ls-files -v”

Skip WorkTree

Here we see that config.txt is marked with a lowercase “h” while otrofichero.sh with an “H” in uppercase. This letter is the representation of the bit that differentiates between a file cached by git and another that is not.

In short, this command does not work for our use case, reading the documentation, this command serves to mark as not cached large files and improve our performance with git. If what we are looking for is that git shouldn’t review any specific file, the option we are looking for is –skip-worktree:

Skip WorkTree

I’m not going to go in deep with that anymore, this last option is similar to assume change but it keeps the flag before the pulls. Assume-change should be used for large files tracked by git like SDKs and so on while skip-worktree fits more with configuration changes for local environment tests in order not to worry about uploading them to our remote repository.

Having said that, I give you some very useful git aliases related to the article. The name of the alias itself defines the action it does:

[alias]

   hide = update-index –skip-worktree

   unhide = update-index –no-skip-worktree

   unhide-all = ls-files -v | grep -i ^S | cut -c 3- | xargs git update-index

–no-skip-worktree

   hidden = ! git ls-files -v | grep ‘^S’ | cut -c3-

 

If you would like to know more about GitTip: Skip WorkTree, subscribe to our monthly newsletter