4

Is it possible to add to .gitignore rules depends on environment variables?

for example

if -e $(ENV_VAR) "AAA" !liba.so else liba.so 

Basically what I want is to have a repository such that if ENV_VAR = "AAA" the local repository will have a.txt and if ENV_VAR isn't set the a.txt will be removed.

12
  • Skimming the git docs for gitignore, the short answer is probably No. Commented Jul 14, 2013 at 16:10
  • Why do you want to set up a gitignore file this way, what is the problem you're trying to solve? Maybe there's a better solution. Commented Jul 14, 2013 at 17:52
  • This would definitely be improved by more context. Commented Jul 14, 2013 at 22:32
  • I want to have a repository such that if ENV_VAR = "AAA" the local repository will have a.txt and if ENV_VAR isn't set the a.txt will be removed. Commented Jul 15, 2013 at 4:42
  • 1
    The reason I'd want to do it is so I can avoid accidentally checking in changes that I've made just so I can debug my code. Let's say I make a temporary change to a file so I can debug something, but I never want to check that temp change in. I'd like to temporarily add that file to .gitignore for my command line session, but I don't want to accidentally check in my change to .gitignore either. Having the ability to export GITIGNORE=path/to/that/file would be helpful in this circumstance. Commented Aug 21, 2014 at 20:19

4 Answers 4

5

The answer is no. Gitignore rules are static. You can turn around it by dynamically creating your .gitignore file. So, you will also have dynamically created static rules.

This can be easily done within the application building system such a Makefile. The .gitignore file can rebuilt each time the project is built.

Example Makefile:

all: my_app .gitignore .gitignore: some deps cp my_static_gitignore_rules .gitignore echo 'dynamically_created_rule1' >> .gitignore echo 'dynamically_created_rule2' >> .gitignore my_app: my_app.c ... 
Sign up to request clarification or add additional context in comments.

1 Comment

It's not a standard way to do it, but very smart and nice.
3

Currently the answer is no Yes, but not using an environment variable.

My use case is simple; I'd like to avoid accidentally checking in changes that I've made to a specific file so I can run my code locally.

Let's say I add my super secret password to the src/assets/secrets.xml file so that I can run my code locally, but I never want my password to leak to github so it can be found with tools like this.

Originally I thought having the ability to export GITIGNORE=path/to/that/file would be helpful in this circumstance.

But since that's not an option, here's how to accomplish the same goal.

git update-index --assume-unchanged src/assets/secrets.xml 

To undo this effect:

git update-index --no-assume-unchanged src/assets/secrets.xml 

After re-reading the OP's question a couple of times, I'm not sure if this will do what he's asking.

1 Comment

@kjhughes I changed my answer to actually answer the question, then modified the rest of my answer to provide a use case where it would be helpful as the OP requested
3

The answer is no.

Gitignore rules are completely static. Further that would not even make any sense. Once a file is in the repo, gitignore does not apply to it anymore – it only prevents new files from being added.

That being said, you can have a local “gitignore”: anything in .git/info/exclude will also be ignored. Within the limits I just explained.

Comments

1

Here there is a nice workaround https://gist.github.com/wizioo/c89847c7894ede628071

by wizioo Julien LIBERT

1 Comment

This link in this solution suggests having different versions of the .gitignore file per git branch. If this is acceptable, then it could work as an alternative. The solution involves writing the different versions of the .gitignore file and naming them differently so they can be picked up by a post-checkout script. Said script is used by the git checkout <branch-name> command. It does not use environment variables.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.