2

I want to make an java application. This is stored on gitHub (so uses git for versioning). Now this programm uses apikeys in example from Google. I want to hide this Api key in the versioning system (so that no one other can use it).

I've read something about the .gitignore file, but i don't want to delete the whole file from versioning. I only want to hide the apiKey from others to see.

I read following thread and I think this is what I'm searching for, but I really have no idea how to do this.

Can git automatically switch between spaces and tabs?

Community
  • 1
  • 1
Sonnenhut
  • 4,073
  • 5
  • 21
  • 21

1 Answers1

2

You cannot version the file and hide it from others. The ID of a commit is based in part on the ID of the commit file tree, which is based on the contents of that tree -- file names and contents. If you change the contents of a file, you change its identity in Git. When you change its identity, you change the identity of the tree it's contained in, all the way up to the commit.

In short, no. The ID of every commit containing this file will be based upon the contents of that file. The only way to hide the file from others who will have access to the repository is to never add it to the repository in the first place.

(If you have not yet published the repository, you can rewrite history using git filter-branch to remove all occurrences of the file. Note that this will, as described above, change the identity of many commits.)

cdhowie
  • 158,093
  • 24
  • 286
  • 300
  • ok, thanks. Anyway I don't want to hide the whole file, only the Api key in the file, because the api key is related to me and others are not allowed to use it. But if thats the only possible solution, I can live with that. Thank you. – Sonnenhut Aug 28 '11 at 08:29
  • 1
    There's a standard solution to that problem. Say the file is called `config.txt`. Create a copy of this file, `config.txt.template`. Replace all of the configuration data (including the API key) with placeholder example data in this template file. Then add `config.txt` to the `.gitignore` file. Now, you can commit the template file without adding any real and/or sensitive data to the repository. To set up a new clone, you just have to copy all of the `foo.template` files to `foo` and edit their contents appropriately. – cdhowie Aug 28 '11 at 08:32