[英]Fork a file within a Git repository
I'm working on an R project which currently has the following directory layout: 我正在研究一个R项目,该项目当前具有以下目录布局:
proj1 |-- file.r
file.r
is used to build a statistical model specific to Project 1 (hence proj1
). file.r
用于构建特定于项目1的统计模型(因此为proj1
)。
During the course of development, we will be building numerous models for numerous projects: 在开发过程中,我们将为众多项目构建众多模型:
Work |-- proj1 | └-- file.r |-- proj2 | └-- file.r : └-- projn └-- file.r
file.r
will be 90% similar between each of the projects, but there will be differences. 每个项目之间的
file.r
将达到90%,但是会有差异。 My question is, is there a way to create a master file.r
file and simply fork it for each project? 我的问题是, 有没有一种方法可以创建一个master
file.r
文件并为每个项目简单地创建它? That way, a bugfix/enhancement to the master can simply be rebased down to the forks, and the file-specific changes will be simply applied on top. 这样,可以简单地将基础的错误修正/增强重新归结到分叉,并且特定于文件的更改将仅应用在顶部。 My first thought was to use submodules, but I'm not certain how to apply that here.
我最初的想法是使用子模块,但是我不确定如何在这里应用它。 Thanks!
谢谢!
Use a "topic branch" for each project: 对每个项目使用“主题分支”:
git checkout master
git add file.r ;# this is your master template upon which others are based
git commit -m "Committed the master file"
Then for each project: 然后对于每个项目:
git checkout -B <project> master ;# create and checkout <project> branch
<hack away on file.r, commit when you want>
git push origin <project> ;# to share <project> with others
So in practice you end up with master
, upon which, say, project1
, project2
, project3
and so forth are based. 因此,在实践中,您最终获得
master
,例如project1
, project2
, project3
等。 Should do exactly what you want and keep it all quite sane. 应该完全按照自己的意愿做,并保持一切理智。
Advantages of this solution over others that encourage multiple repositories: 与鼓励多个存储库的其他解决方案相比,此解决方案的优点:
file.r
with git diff projectA projectB -- file.r
. git diff projectA projectB -- file.r
看到两个项目的file.r
之间的文件git diff projectA projectB -- file.r
。 You could do the same with multiple repositories, but it requires a repository specification like git diff projectA/master projectB/master -- file.r
. git diff projectA/master projectB/master -- file.r
。 Could get confusing if you have 20-30 project repositories or use submodules. git fetch origin
and watching the output. git fetch origin
更新就像发出git fetch origin
和查看输出一样简单。 git remote add <project>
repositories until you've got them all. git remote add <project>
存储库,直到拥有全部。 Disadvantages (an incomplete list): 缺点(列表不完整):
file.r
you're viewing at any given moment. file.r
可能都不那么明显。 That might be a deal breaker. R
projects, however, this might be less of a concern. R
项目,这可能不太重要。 IMO the best way to achieve this is: IMO达到此目的的最佳方法是:
git submodule
to integrate the shared code to each project git submodule
将共享代码集成到每个项目 There are ways described in the other answers. 其他答案中有描述的方法。
For example, 例如,
Finally, when no other way, I use comments in the file header, that it is a fork of another file. 最后,当没有其他方法时, 我在文件头中使用注释,即它是另一个文件的分支。
Date | Author | Description ------------- | ------------------- | -------------- 05/18/2018 | You | Forked from Other/file.r
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.