Git: 对于每个遥控器都有不同的.gitignore 文件

我有一个远程回购,我想在其中提交某些文件(在云计算平台上部署它们的编译文件) ,但我不想在 github 上部署它们..。

有没有什么方法可以不同的. gitignore 文件,每个遥控器一个?

22747 次浏览

This doesn't really make sense in git's model. Commits contain sets of files; all .gitignore files do is tell the UI not to automatically add files matching certain patterns. What this would effectively mean is to have parallel sets of commits that are almost the same, but containing only a subset of the files.

It'd be possible to do this with a branching scheme, where you have a "deployment" branch that splits off of master and is the same but contains the additional compiled files. This could even be automated with git hooks to automatically compile the files and add them to the repo. I'm envisioning a structure like this:

master:       A ---> B ---> C ---> D
\      \      \      \
\      \      \      \
deployment:      -> A'  -> B'  -> C'  -> D'

i.e. every time a certain server gets a new commit on master, it builds the project, adds the built files to a new commit from D, and commits that to the deployment branch -- which then doesn't have to be pushed to github.

Another option is git submodules.

This can be useful if, for instance, you want your code and documentation to be in two different repos with independent access control, etc. So you'd have 3 total repos, a submodule repo for docs, another for code, and the "master" (not a git submodule) repo containing both (for pypi upload, perhaps). This is a decent way to organize a CS textbook project. Both projects can proceed merrily ahead independently and sync up at major releases, driven by the master repo maintainer.

Update: you will not be able to replicate this environment using git clone, so I don't think this is a good approach


I figured out a way to do this.

In my case, I needed to synchronize the project with heroku and GitHub (as my public repo).

But I was not interested in sharing some files with private information in a public repository.

Usually a simple project would have the following folder structure

Project folder (remote heroku)
- .git
- .gitignore
- (folders and files)

What I did was add one more level, and in it create another git repository, with a .gitignore that would omit some files from my project.

Project public (remote GitHub)
- .git
- .gitignore
- Project folder (remote heroku)
- .git
- .gitignore
- (folders and files)

So this is not a git repository with two remote repositories with different .gitignores.

There are two different repositories.

On the innermost I only exclude the files generated by the IDE and some files generated at runtime.

At the outermost level, I exclude all files that cannot be make public.

An automated solution to the methods mentioned here:

  1. Set up the root .git to ignore the subfolder(s) you you want transmitted differently
  2. Init new git within the subfolder(s) and assign the remote configuration
  3. Modify the root folder's .git/hooks/pre-push file to exec git push at those sub folders based on the incoming arguments.