Home > OS >  Track files without including them in the commits
Track files without including them in the commits

Time:07-29

So it happened a couple of times when I would be working on connected projects that this scenario took place:

  1. I would make a minor change to one of the config files or comment out something in a file am not tracking to test stuff.
  2. Forget to revert the changes.
  3. Come back after a while to test things that work with the initial version of the file.
  4. Waste lots of time debugging to find out step 2 was the problem.

Now I was wondering, is it possible to track some files but automatically ignore them when committing ? Think of it as a more lenient version of the .gitignore where I can choose some files to track while am developing locally, but they would never be committed. I was thinking I could make a script that unstages those specific files in the then runs the commit. However, is there something I should be paying attention to ? Or perhaps there's already a way my quick search didn't pick up?

CodePudding user response:

Keep a template for a configuration file in source control. When it comes time to actually configure a service, edit a copy of the template to provide a valid configuration. The actual configuration file never gets tracked.

CodePudding user response:

What you want here is to enable --skip-worktree bit for the configuration file.

First, ensure that git tracks the file existence by committing it at least once:

% git add Config.file
% git commit -m 'Configuration added'

Then flip the --skip-worktree bit:

% git update-index --skip-worktree Config.file

From this point forward all changes you make to the file will be ignored by git.

In order to see the changes and restore the file you can get use of the following git aliases:

[alias]
    diffsw = !git update-index --no-skip-worktree $1 && git diff -- $1 && git update-index --skip-worktree $1 && :
    restoresw = !git update-index --no-skip-worktree $1 && git restore -- $1 && git update-index --skip-worktree $1 && :

The first one shows the changes of the untracked file and can be used like this:

% git diffsw Config.file

The second one restores the file to the state it was last time commited:

% git restoresw Config.file

P.S. This, however, affects only local repo, you cannot push this bit.

  • Related