{{announcement.body}}
{{announcement.title}}

GitTip: Skip WorkTree

DZone 's Guide to

GitTip: Skip WorkTree

Use the skip-worktree command in git to modify a file in a local environment that git won't detect modification on to avoid affecting other files.

· DevOps Zone ·
Free Resource

hopscotch board

Use skip-worktree to sneak in modifications git won't notice

GitTip: Skip WorkTree

This tip is simple and powerful in equal parts. What we want to achieve is to modify a local file that git could not identify as having been modified despite being already tracked. For this example, I have a file “config.txt” with the content: “secret.” This file works well in all environments, but in the 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 reasons, has to be modified in your local environment and has to work without being uploaded to avoid affecting other files. How can we do that?

You may also enjoy: The 2016 Git Retrospective: Worktrees

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

In the following image we can see how running 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 running the command  update-index  the file config.txt disappears from the working area.

Running update-index

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

  • Manual deactivation of the bit with which we have marked that file (assume-unchanged)
  • Pulling and modifying that file via upstream

Before this last scenario in which we worry about the file, we have to review each pull to see whether the bit has been removed or not with the command  git ls-files -v :

git ls-files

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

In short, this command does not work for our use case. Based on the documentation, this command marks large files as "not cached" and improves our performance with git. If what we wand is for git not to review a specific file, the option we are looking for is  –skip-worktree :

I’m not going to go in deep with that anymore, as 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 so wedon't have to worry about uploading them to our remote repository.

Having said that, herevery useful git aliases related to the article. The name of the alias itself defines the action it does:

Shell




x
11
9


1
[alias]
2
   hide = update-index –skip-worktree
3
   unhide = update-index –no-skip-worktree
4
   unhide-all = ls-files -v | grep -i ^S | cut -c 3- | xargs git update-index
5
–no-skip-worktree
6
   hidden = ! git ls-files -v | grep ‘^S’ | cut -c3-



Further Reading

Tracking File Permissions in Git

Commands and Operations in Git
Topics:
worktrees ,software developer ,git ,git hints ,git commands ,devops ,open source ,file modification

Published at DZone with permission of Oscar Galindo . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}