103

I have a database configuration file that has default values that are unimportant. However, any changes to this file would contain sensitive information that should not be tracked in the repo.

I would like future pulls of the Git repository to include the default version but disregard any changes made by any user.

The following keeps a local configuration but pushes the delete to the repo resulting in issues for future pulls.

cat "app/dir/config.file" >> .gitignore
git rm --cached app/dir/config.file

The following does the job but does not persist past the push to the repo.

git update-index --assume-unchanged app/dir/config.file

This seems like a common requirement for version control around sensitive information but I can't seem to find a solution.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ben Campbell
  • 4,298
  • 2
  • 29
  • 33
  • 4
    I usually create a `config.file.dist` that contains default configuartion values and commit that. Then when a copy is checked out you just copy the `.dist` file to make an actual config file and fill in the values you want to change. – Paul Aug 16 '13 at 15:32
  • 1
    That seems to be the consensus across the board. If you don't want it versioned don't track it to begin with or track a template the user implements. I am starting to think that I am trying hammer a screw the wrong way. – Ben Campbell Aug 16 '13 at 15:54
  • possible duplicate of [Committing Machine Specific Configuration Files](http://stackoverflow.com/questions/1396617/committing-machine-specific-configuration-files) – Senseful Sep 03 '14 at 19:32
  • Does this answer your question? [How to stop tracking and ignore changes to a file in Git?](https://stackoverflow.com/questions/936249/how-to-stop-tracking-and-ignore-changes-to-a-file-in-git) – hestellezg Feb 21 '20 at 17:13

2 Answers2

226

As usual github has a great doc on this.

https://help.github.com/articles/ignoring-files#ignoring-versioned-files

Here's the relevant snippet:

Ignoring versioned files

Some files in a repository change often but are rarely committed. Usually, these are various local configuration files that are edited, but should never be committed upstream. Git lets you ignore those files by assuming they are unchanged.

  1. In Terminal, navigate to the location of your Git repository.
  2. Run the following command in your terminal:

git update-index --assume-unchanged path/to/file.txt

Once you mark a file like this, Git completely ignores any changes on it. It will never show up when running git status or git diff, nor will it ever be committed.

To make Git track the file again, simply run:

git update-index --no-assume-unchanged path/to/file.txt.

Community
  • 1
  • 1
Rob
  • 2,759
  • 1
  • 16
  • 11
  • 68
    The first answer to [this question](http://stackoverflow.com/questions/16818305/git-assume-unchanged-implications) indicates that `--assume-unchanged` sets a local setting. That means any time a new developer clones, they need to `--assume-unchanged` every applicable file. How can this behavior be propogated? – Edward Newell May 09 '14 at 11:33
  • 15
    I'm glad you quoted that section, because GitHub has since removed it from that page. – user2752467 Feb 25 '15 at 00:16
  • Location in docs: https://git-scm.com/docs/git-update-index#git-update-index---no-assume-unchanged – mtpultz Aug 24 '18 at 22:51
  • 2
    @EdwardNewell By protocol. Include the relevant files in a file called `.git-assume-unchanged` (or something). Then in whatever command you use to setup your project configurations run `cat .git-assume-unchanged | xargs git update-index --assume-unchanged`. Probably best to also include aliases to set and unset these in your project configuration. – DylanYoung Jul 31 '20 at 20:55
  • 1
    To get a list of `--assume-unchanged` files, see https://stackoverflow.com/q/2363197/72178. – ks1322 Mar 02 '21 at 15:40
7

Not sure if this is the "best" way... but what I have found to work takes a few steps.

New Laravel Example:

  1. git init
  2. Adjust .gitignore files for desired results like not losing the vendors folders.
  3. Make a copy of the files you don't want tracked (ex: .htaccess, .env, etc)
  4. git add .
  5. git commit -m "first commit"
  6. git rm --cached public /.htaccess then git rm --cached .env
  7. git commit
  8. git status should show those files as deleted.
  9. Now put the file you copied back. Since they are the same name as what you told git to remove from tracking, git will now fully ignore those files.

Now those can be edited, and you can have local and production versions. NOTE: You may have to repeat all these steps the first time on the production set-up as well. So far this has worked well for me.

Barett
  • 5,826
  • 6
  • 51
  • 55
bikermusician
  • 71
  • 2
  • 1