There is a project with source codes (in python), which I regularly post on Github. It contains variables (so far one, is a token: TOKEN="..." ), which I do not want to spread for universal access. However, in my debugging and verification process, I fill these variables with the appropriate values.

Before that, I simply manually deleted the value before the commit, then, during the startup process and other things, I inserted the value again. But now I'm wondering, is it possible in Git to reset a certain variable before committing and pushing?

So far, I see 2 exits without using Git features:

  • or have an exported configuration file that can not be used in a common repository; but for the time being, for the sake of one variable, I consider this to be a redundant solution;
  • or just write a one-liner with sed `th, which is not so interesting, unlike learning about the possibilities of Git. Yes, and it is a crutch, in my opinion.
  • five
    Usually such things are simply carried out in environment variables or config. - Nofate ♦
  • one
    @Nofate, you would build a detailed answer - I’m also wondering how to glue it correctly) - Yuriy SPb ♦
  • one
    @Yuriy SPb, but what is there to talk about? You can ask me in the chat if any details are of interest. - Nofate ♦
  • 3
    @Nofate, well, I think it would be useful for many to see an example in the answer ... For example, there is a line in the code or in the resources and how to move it and where to move it so that it can be used easily and does not fall into open access ... It seems I somehow I saw an article on the Habré on this account ... There somehow, through BUILD.CONFIG it was suggested to solve it and in the gradle something gradle there, like ... And the constant in the code took just the value from the build ... - Yuriy SPb ♦
  • one
    It is possible to make a configuration to a separate commit, but it’s possible to support %) - D-side

2 answers 2

In the project folder, create a subfolder for hidden configs. Add it to .gitignore . If the application does not need to read it from another user, restrict the rights.

 mkdir .hidden chmod 0600 .hidden echo '.hidden' >> .gitignore 

Why a folder, and not just a file: because once you can accidentally rename it, commit and launch it. And your token will be lost forever, because the githab is mirrored in real time. I consider the hook option dangerous for the same reason - you can rename the variable in the config file or somehow change the format, after which the hook will silently and silently not work.

Store configs with tokens in any convenient format for you in this folder. Config even for one variable - this is normal. Your ssh-key is, roughly speaking, also one big line in the file.

    Of course, you can write a local pre-commit interceptor of this kind:

     $ cat .git/hooks/pre-commit #!/bin/sh sed -i 's/parol=.*/parol=/' file 

    here, just in the file file (lying in the root of the working copy) in the line containing parol= , everything will be deleted after parol= , before making the commit . Remember to assign the executable bit to the interceptor file: $ chmod u+x .git/hooks/pre-commit .

    main disadvantage: it will only work in your local copy of the repository.


    but, probably, it would be more correct to make the settings in a separate configuration file, which will not be included in the repository (for “beauty” it is not bad to mention it in the .gitignore file).

    • as a result, the first part of the council is the very samopisny solution that is not at all elegant. But I didn't know about pre-commit, thanks. Without knowing Git, I thought that there is a ready-made solution that I simply did not find in the manual. - approximatenumber
    • @approximatenumber, no, “ready-made solution”, as far as I know, does not exist in nature. - aleksandr barakin
    • although what's the difference if the script is just separate or in its local repository ... - approximatenumber