git: Ignore local changes on committed (env) file
Whenever I’ve writing scripts that rely on credentials defined as environment variables, I like to create a .env
(or equivalent) file containing those variables.
I then seed that file with placeholder values for each variable and make local changes that aren’t checked in.
I recently created the mneedham/materialize-sandbox/strava repository where I’m using this approach with a .envsettings
file that has the following contents:
export CLIENT_ID="client_id"
export CLIENT_SECRET="client_secret"
I have that file checked in so that anybody else can clone the repository and update this file with their own credentials.
But I don’t want local changes to that file to be tracked, as I might then accidentally commit my credentials.
I’d previously achieved this by adding .envsettings
to the .gitignore
file, but that technique doesn’t seem to work anymore!
Luckily there is a way around this, using git’s update-index
command, which I learnt about from Borealid’s answer on StackOverflow.
So once I’ve committed the .envsettings
file, I can have git ignore any changes by running the following command:
git update-index --skip-worktree strava/.envsettings
And now git will ignore any local changes made to this file, which is exactly what I want!
About the author
I'm currently working on short form content at ClickHouse. I publish short 5 minute videos showing how to solve data problems on YouTube @LearnDataWithMark. I previously worked on graph analytics at Neo4j, where I also co-authored the O'Reilly Graph Algorithms Book with Amy Hodler.