GRobb
GRobb

Reputation: 303

Git pull but never push file

I couldn't find anything specific to this but if I missed it please let me know.

I have a file in git which needs to be there. Once pulled down each individual will need to edit it to add their own credentials to it. When edited by the individual I do not want it to show up as having been changed or anything because we want to keep the generic file without credentials in our repo. Is there an option to have a file set up as "Pull this file but never push it"? Currently the setup we have is the file is named Upload.bat_COPY_EDIT_RENAME, which hopefully makes it obvious what to do, and I have Upload.bat in the .gitignore. I'd like something more idiot proof.

I know there are options like --assume-unchanged but I don't want to have to run that every time and it seems kind of like a bandaid fix which could potentially cause problems later.

Thanks for your help!

Upvotes: 18

Views: 1985

Answers (4)

jacmoe
jacmoe

Reputation: 1687

One approach that does not require you to add Upload.batto your gitignore is to use batch file include.

You can add a file called credentials.txt.dist to the repository with the following contents:

username=username_here
password=password_here

And then, in your batch file:

for /f "delims=" %%x in (credentials.txt) do (set "%%x")

Shamelessly taken from Batch file include external file for variables

And not tested :)

The batch script should probably do some error checking. At the very least it should generate a descriptive error message if credentials.txthas not been provided.

Upvotes: 1

Kevin Burdett
Kevin Burdett

Reputation: 2982

I would suggest a slightly different approach... You can write your Upload.bat to read from a settings file using your format of choice. Say you call it, secrets.txt. Then you can add secrets.txt to your .gitignore and commit Upload.bat with impunity. When users pull the repo, they will get Upload.bat and can create their own secrets.txt.

You might make secrets.txt have just the username and password, separated by a newline. Should be trivial to parse this way.

Upvotes: 5

8bittree
8bittree

Reputation: 1799

To my knowledge, git does not have a permanent setting to pull, but never push a file.

You could get a more reliable workaround by storing the template file in git (as you already do), and having your build script (you do have one, right?) check for the customized file. If the customized file exists, great, carry on with the build. If it does not exist, prompt the developer for their credentials and set it up. Then carry on with the build. The customized version of the file is, of course, in .gitignore.

This is essentially what you have now, but with the addition of the build script serving a reminder to the developer, instead of relying on the forgetful, oblivious human to hopefully notice and understand a funnily named file somewhere in their file system.

As an added benefit of having the script handle the creation of the customized file, it can potentially update it automatically if the template changes.

Depending on the credentials, you may want to double check to make sure that they aren't recorded to any logs when running the build script. This includes piping the output to a file.

Upvotes: 2

DeveloperDemetri
DeveloperDemetri

Reputation: 208

If each person is going to rename the file, you could add Upload.bat* to your .gitignore file, and then git shouldn't track any new files with that naming scheme. More info on gitignore

Upvotes: 2

Related Questions