使git忽略其他成员没有的文件

时间:2018-07-20 15:06:02

标签: git gitignore


我对git用户有一个不寻常的问题:
我想成为git项目的成员,这意味着我希望能够将代码移入/移出存储库,同时保持我自己的项目文件的少数版本,不应将其推入或拉入存储库。
我的理想情况是,当我启动git时,我上传已更改的所有内容,但没有几个预定义的文件,当我拉动时,我下载所有已更改的内容,而不是刚才提到的文件。
我在Google搜索中发现“。git / info / exclude” 可能对我有帮助,但是我没有设法使它按预期工作。到目前为止,我发现似乎可以达到我预期目的的唯一方法是使用“ git update-index --assume-unchanged” ,但是为此,我必须分别发出命令当我将任何内容推入或拉出到存储库时。

有什么聪明的方法可以做到这一点吗?

1 个答案:

答案 0 :(得分:1)

In git, the basic unit that is pushed or pulled is the COMMIT, which is a snapshot of the entire content at a particular time. File versions may be pushed as part of a commit, but unlike some older systems (e.g. cvs) which were a collection of versioned files, git has a versioned collection of files.

And commits are immutable. Every detail of a commit's content is an integral part of the commit's identity. So when you say anything about "this commit, but with one file (or a few files) different", that means "a different commit that's mostly similar to this commit".

And once you're talking about different sets of commits, you're talking about diverging histories.

I often see people recommend using index flags (like assume-unchanged), but that's not what they're meant for. You've already observed that it doesn't work smoothly, and if you say "oh, well, there's nothing better" and do it anyway, it may lead to more hassles down the road.

If you need to have local differences, the best thing is to keep them outside your git work tree. (An alternative would be to keep them at ignored paths in the work tree, but since you don't fully control the upstream repo it may not be safe to assume a given path will always be unused.)

How to do that and still have everything work depends on your situation. Maybe you could use locally-modified build tooling (or other scripting) to insert your changes into local builds. You might be able to use .gitattributes to set up a custom filter that swaps between your local version and the "canonical" version when moving the files between the index and the work tree (though I expect that to be tricky and possibly brittle).