Keep file in a Git repo, but don't track changes

Git

Git Problem Overview


I have several files in a CodeIgniter site that I will want to have in the repo but not track any changes on.

For example, I deploy a new installation of this framework to a new client, I want the following files to be downloaded (they have default values CHANGEME) and I just have to make changes specific to this client (database credentials, email address info, custom CSS).

// the production config files i want the files but they need to be updated to specific client needs
application/config/production/config.php
application/config/production/database.php
application/config/production/tank_auth.php
// index page, defines the environment (production|development)
/index.php
// all of the css/js cache (keep the folder but not the contents)
/assets/cache/*
// production user based styling (color, fonts etc) needs to be updated specific to client needs
/assets/frontend/css/user/frontend-user.css

Currently if I run

git clone [email protected]:user123/myRepo.git httpdocs

and then edit the files above, all is great. Until I release a hotfix or patch and run git pull. All of my changes are then overwritten.

Git Solutions


Solution 1 - Git

git has a different solution to do this. First change the file you do not want to be tracked and use the following command:

git update-index --assume-unchanged FILE_NAME

and if you want to track the changes again use this command:

git update-index --no-assume-unchanged FILE_NAME

git-update-index documentation

Solution 2 - Git

To pull an answer out of the comments of another answer:

$ git update-index --skip-worktree FILENAME

Appears to be a better option than --assume-unchanged

More can be read about this here: https://stackoverflow.com/questions/13630849/git-difference-between-assume-unchanged-and-skip-worktree#

Solution 3 - Git

For my Code Igniter projects, I keep database.php.example and config.php.example in the repo.

Then I add config.php and applications/config/database.php to the .gitignore file.

So, finally, when I deploy, I can copy the .example files (to config.php and database.php), customize them, and they won't get tracked by GIT.

Solution 4 - Git

The recommended way, as of git version 2.25.1, is to track templates of the files in the repository, copying them into new files and modifying them locally (ignoring the new filename in .gitignore) (from here):

> Users often try to use the assume-unchanged and skip-worktree bits to > tell Git to ignore changes to files that are tracked. This does not > work as expected, since Git may still check working tree files against > the index when performing certain operations. In general, Git does not > provide a way to ignore changes to tracked files, so alternate > solutions are recommended. > > For example, if the file you want to change is some sort of config > file, the repository can include a sample config file that can then be > copied into the ignored name and modified. The repository can even > include a script to treat the sample file as a template, modifying and > copying it automatically.

Solution 5 - Git

I would put them in your .gitignore file and just copy them manually as needed.

So the skeleton filename would be in git, the ignore filenames would not be in git (but would be in .gitignore). That way, when the manual step to copy them to 'actual' from 'template' (better name than skeleton perhaps) is done they are immediately ignored.

Solution 6 - Git

The answers given are solving the problem just partially, but introducing even more problems.

I needed to preserve the"Web.Local.config" file. The only solution that worked for me was to:

1. Exclude the file from the project

2. Create a separate copy of the file as Web.LocalTemplate.config

Now that "Web.LocalTemplate.config" is tracked by Git, all developers can use it as a starting point. However, the "Web.Local.config", not being part of the project will be automatically ignored.

Solution 7 - Git

Make sure to add "full path for filename" (relative path from root)

git update-index --skip-worktree FILENAME.

If you do not give the full path for the file. It will give an error

fatal: Unable to mark file response.json

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNDBoostView Question on Stackoverflow
Solution 1 - GitnasirkhanView Answer on Stackoverflow
Solution 2 - GitAnthonyView Answer on Stackoverflow
Solution 3 - GitsuperiggyView Answer on Stackoverflow
Solution 4 - GithoutanbView Answer on Stackoverflow
Solution 5 - GitMichael DurrantView Answer on Stackoverflow
Solution 6 - GitHrvoje MatićView Answer on Stackoverflow
Solution 7 - GitHarsh BorichaView Answer on Stackoverflow