Melvin S.
Melvin S.

Reputation: 105

Stop git from updating remote file

I got a git repository where I got several files. (Actually it's my Advent of Code repository)

In this repository is a file which I want to keep in the GitHub remote but I don't want git to update it, even if this file changes locally. Also I want this behavior automatically on every machine where I clone this repository to.


So basically these steps:

  1. Create file and add initial content (v1)
  2. Commit and push file to remote branch
  3. Do something that lets git ignore all changes to this file on every machine
  4. Change the file content (v2)

Now I got the file in the remote (v1) and locally (v2) but I don't want to see anything about this file when I run git status. Also if I clone this repository (with file v1) to another machine and make changes to the file (now v3), I don't want to see anything in git status about this file on the other machine.

To provide an example: I want to add the file which will contain my session cookie to GitHub. Obviously I don't want to push my session cookie to GitHub, just the a placeholder or something. But when I run the program, it must contain the right session cookie, so I have to update the file with the actual cookie. Now I don't want git to update this file to GitHub. And when I clone this repository to another machine, I want it to contain the placeholder like in the remote which I can then update manually. Also git should not want to update the file from this machine either.


First I tried adding it to .gitignore and running git rm --cached myfile but then git wants to delete this file from the remote as well.

I tried with git update-index --assume-unchanged myfile. Which technically does what I want, just not on all other machines because it only affects my local git repository.

Is there even a way I can accomplish what has to be done in step 3?

Upvotes: 0

Views: 960

Answers (1)

bk2204
bk2204

Reputation: 76459

You're asking how to ignore changes to a tracked file, and the answer is that Git doesn't do that. From the entry in the Git FAQ:

Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

Any sort of secret should be passed in through the environment or a config file. It's fine to create a template for this purpose and check that in, then have a script to copy it into an ignored location so that you can edit it and add the secret. For most major production systems, secrets are typically passed in through the environment so they aren't written to disk.

Upvotes: 1

Related Questions