Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a file to a git repository but ignore future changes on it

Tags:

git

I want to add an overrides.json. That allows developers to make changes to a config object locally. I want it added to the repository in a default state but I want future changes on it to be ignored.

It seems like .gitignore forces me to remove it from the branch before I can ignore it. I know about git update-index but each developer would have to do this manually. To clarify, I do not want the file removed; I actually want the file to be added to repository but untracked after that.

I can't simply remove it because GET requests to a missing file throws ugly error messages in the browser's javascript console.

like image 301
Brennan Cheung Avatar asked Nov 10 '14 22:11

Brennan Cheung


People also ask

How do I ignore changes to a file in Git?

In the Git Changes window, right-click any changed file that you want Git to ignore and choose Ignore this local item or Ignore this extension. Those menu options don't exist for tracked files.

How do you exclude a file from a commit?

Set “–assume-unchanged” to a path to exclude to check on git commit and it will exclude your file from git commit. You will need to use the git update-index and –assume-unchanged to exclude files from git commit.

How do I ignore unwanted changes in Git?

Creating an ignore file To avoid having to ignore unwanted files manually, you can create a . gitignore file in the working directory of your project. Inside this file, you can specify simple patterns that Git will use to determine whether or not a file should be ignored.

Does Git add add ignored files?

The git add command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add will fail with a list of ignored files.


1 Answers

After adding the file you can ignore all further changes by doing:

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

If at some point you decide that you need to push more changes you can allow again changes by doing:

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

Instead of adding the files in the repo, why not ignore them and add them to a different location, eg. a setup directory. This way after cloning the repo, the developers will need to execute an init step that will copy the needed files in the proper location, where they are ignored.

like image 171
dan Avatar answered Sep 21 '22 19:09

dan