1

I am working on a MVC.NET project and I have external config files that are located in the project's root directory.
At some point, I accidentally committed these files to Git and pushed up the changes.

After several commits after that the tech lead realized my mistake and set it up to be removed from Git, added the files to .gitignore, and persisted the changes.

Now I have the files that are ignored by Git, but, if I change branches, sometimes it will pull in the commit that removed those files.
So I have to re-touch the file and re-add all the contents (very annoying).

Is there a way so that, even if I change branches, that git bash won't remove my external config files?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
David
  • 5,877
  • 3
  • 23
  • 40

1 Answers1

1

I documented in "Git - Difference Between 'assume-unchanged' and 'skip-worktree'" how git update-index --skip-worktree/--assume-unchanged would not be a good solution.
(See the last part, section "Git 2.25.1 (Feb. 2020)")

I would prefer instead use:

  • a config.tpl (template file with placeholder values).
  • a script able to take a template file, and produce the actual file (which remains untracked, private) automatically on checkout
  • a .gitignore which ignores the resulting generated file (you already have that)
  • a .gitattribute declaring a smudge content filter (see below)

The generation of the config file is automated through a content filter driver, using a .gitattributes declaration.

https://i.stack.imgur.com/tumAc.png
(image from "Customizing Git - Git Attributes" from "Pro Git book"))

Once you declare that content filer driver in your local config, it will automatically, on git checkout, generate your (not tracked) file for you.
See a complete example in "Best practice - Git + Build automation - Keeping configs separate".

That way, no amount of git checkout would remove said file.

Also, try the newer git switch command when switching branch: it does respect files better than git checkout.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250