43

I have a config file which I want to keep on the remote repository, but I don't want to track its changes on my computer. Adding it to .gitignore doesn't do the trick.

The reason I don't want to track changes is because it's supposed to differ between computers depending on their environment.

Ben G
  • 26,091
  • 34
  • 103
  • 170
  • 1
    possible duplicate of [git - removing a file from source control (but not from the source)](http://stackoverflow.com/questions/936249/git-removing-a-file-from-source-control-but-not-from-the-source) – Karl Bielefeldt Aug 16 '11 at 03:31

4 Answers4

92

If you want to temporarily stop tracking a file, you can still use

git update-index --assume-unchanged <file>
// track changes again :
git update-index --no-assume-unchanged <file>
theor
  • 1,545
  • 1
  • 9
  • 16
  • 3
    It's worth noting that `git reset --hard` affects these files as well, so if choosing this approach for local configuration files, one has to remember that they would be reset anyway and their local changes lost when invoking `git reset --hard`. – Yoel Sep 23 '14 at 13:03
  • Also I just found that you cannot switch branch when you update set as `git update-index --assume-unchanged ` You must do `git update-index --no-assume-unchanged ` before switching branch. – Sanjay Rawat Feb 27 '16 at 15:14
  • note that --assume-unchanged might not work how you expect, and under certain conditions git could still mark the file as modified and include it in the changes. See http://stackoverflow.com/questions/23097368/git-ignore-vs-exclude-vs-assume-unchanged for more – stackPusher Apr 28 '17 at 16:14
11

If that's the case then you shouldn't have the file versioned at all; you should version a template of the file. For example, if the configuration file is foo/config.txt then you should have a versioned foo/config.txt.template in the repository with example (or blank) configuration settings. foo/config.txt should not be in the repository at all, and should be ignored with .gitignore.

Then, in a new clone, you just copy foo/config.txt.template to foo/config.txt and alter the settings as appropriate.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • But I was thinking that the repository version should correspond to your production code – Ben G Aug 16 '11 at 00:12
  • 2
    @babonk: Configurations should be versioned, but they don't necessarily have to be versioned along with your code. They could be versioned on the production systems. If with your code, you could put it at foo/production-config/config.txt vs. a dev one at foo/dev-config/config.txt which makes use of templating as suggested by cdhowie. – Ryan Stewart Aug 16 '11 at 00:24
3

You will need to do the combination of .gitignore file and git rm --cached. Use the following command :

git rm --cached filename.txt

This will remove the file from indexing but it will keep it in your repository and working area. After applying rm --cached you will need to commit the changes for it to take effect. It will show as Deleted in your tracked changes, but that's only because it has been deleted from indexing. Note: If you make changes to it again in future commits you will have to run: git rm --cached to untrack the changes to the file from the particular commit.

Dhaval Chheda
  • 844
  • 1
  • 8
  • 15
-1

You could commit all the other files, and for the changes to the local configurations alone, you could use the 'stash' feature that git provides.

balajeerc
  • 3,998
  • 7
  • 35
  • 51