1

I have a repo, used by developers working on my project. In this repo, there is a file ".your_local_variable.php", where there are paths that have to be filled by the developer, relative to their machines, before starting working on the project. I want the developers to pull the "template" of this file only once when they first clone the repo on their machines, but I want to prevent them to send this file on a merge request.

I tried to put it in the .gitignore, and push .gitignore remotely, so they can retrieve it, but the file ".your_local_variable.php" is still showing up on command "git status" as "changes not staged for commit".

Is there any way to prevent pushing this file, and to hide it from git status, while keeping a "template version" of this file on the remote repo?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Jerem Lachkar
  • 1,008
  • 11
  • 24

5 Answers5

3

If the file was already checked into git, just adding it to .gitignore won't resolve the problem.

You will need to delete the file from git with git rm + add the file to .gitignore, then push this commit - which contains the delete + ignore. Everyone will need to pull this change so that it stops showing up in your git status.

You can also keep the template version on git, and just tell everyone to copy + rename the file into the one ignored. This is what it should look like on git :

  • template-for-everyone.file (checked into git so people can retrieve, no one should change this file, unless you want to change the template)
  • my-local-template.file (add this file into .gitignore, everyone should copy the template and edit it with this name)
  • .gitignore (which contains my-local-template.file)
HRK44
  • 2,382
  • 2
  • 13
  • 30
2

I think the most common solution for that is to create a template file, commit it and ignore the actual file.

Let's say your file name is foo.bar, you may commit a file called foo.bar.example with your template, then in your .gitignore file you add foo.bar.

To remove your file from your repo you just run git rm --cached foo.bar and commit it.

Then your collaborators will copy the foo.bar.example file on their local environment to a new foo.bar file.

xyzale
  • 745
  • 8
  • 14
2

Another solution would be for the file to pick values from environment variables, sometimes with default values:

<?php
// This variable represents the path for blablabla
$somePath = getenv("PROJECT_SOME_PATH");
// This variable represents the time to wait before contacting the customer (in hours)
$timeToAnswer = getenv("PROJECT_TIME_TO_ANSWER", 42);
// .....

This way, the developers define their own values in their environment, container, other, without touching the file (which is then here for reference).

The first step towards that would be first to update your file with environment variables, push it and communicate to the other devs that they need to set up their env.

padawin
  • 4,230
  • 15
  • 19
1

This is typically handed outside of version control. You'd describe the needed file in a Readme (or provide a template with a .txt extension, for example) and have the developer add it manually. It should be in .gitignore, however.

isherwood
  • 58,414
  • 16
  • 114
  • 157
1

In one commit:

  • Delete the file.
  • Add it to .gitignore.
  • Add a non-working version of the file to the repo. For example:

    .your_local_variable.phpREMOVEME

Then each developer has the a template file which won't cause any problems until they interact with it.

Ryan Lundy
  • 204,559
  • 37
  • 180
  • 211
  • So there is no way in git to have "read only" files that you can pull, update locally but cannot push ? If my developpers are not very confortable with git, and they do unintentionnaly "git add .", will this command add the files registered in gitignore to the commit anyway ? – Jerem Lachkar May 14 '19 at 12:57
  • 1
    No, if the files are in .gitignore, and they've been removed from the source, they can't be added again without removing them from .gitignore first. – Ryan Lundy May 14 '19 at 13:02