将多个项目放入 git 存储库的最佳实践是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14679614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the best practice for putting multiple projects in a git repository?
提问by Stony
As some reason, I only have one repositoryto use.
But I have multiple projectsincluding java
projects, php scripts
and Android
apps projects.
由于某种原因,我只有一个存储库可以使用。
但是我有多个项目,包括java
项目php scripts
和Android
应用程序项目。
Now my problem is, I have to put them to different sub-foldersinside the repository
I use different IDEs, You know, each IDE can have a workspace of itself.
现在我的问题是,我必须将它们放在存储库内的不同子文件夹中,
我使用不同的 IDE,您知道,每个 IDE 都可以有自己的工作区。
Who can tell me a best practice to solve the problem?
谁能告诉我解决问题的最佳实践?
回答by Jacob Groundwater
While most people will tell you to just use multiple repositories, I feel it's worth mentioning there are other solutions.
虽然大多数人会告诉您只使用多个存储库,但我觉得值得一提的是还有其他解决方案。
Solution 1
解决方案1
A single repository can contain multiple independentbranches, called orphan branches. Orphan branches are completely separate from each other; they do not share histories.
单个存储库可以包含多个独立分支,称为孤立分支。孤立的分支彼此完全分离;他们不分享历史。
git checkout --orphan BRANCHNAME
This creates a new branch, unrelated to your current branch. Each project should be in its own orphaned branch.
这会创建一个新分支,与您当前的分支无关。每个项目都应该在它自己的孤立分支中。
Now for whatever reason, git needs a bit of cleanup after an orphan checkout.
现在无论出于何种原因,git 在孤儿结帐后都需要进行一些清理。
rm .git/index
rm -r *
Make sure everything is committed before deleting
在删除之前确保所有内容都已提交
Once the orphan branch is clean, you can use it normally.
孤儿分支清理干净后,就可以正常使用了。
Solution 2
解决方案2
Avoid all the hassle of orphan branches. Create two independent repositories, and push them to the same remote. Just use different branch names for each repo.
避免孤儿分支的所有麻烦。创建两个独立的存储库,并将它们推送到同一个远程。只需为每个 repo 使用不同的分支名称。
# repo 1
git push origin master:master-1
# repo 2
git push origin master:master-2
回答by Ivan
Solution 3
解决方案3
This is for using a single directory for multiple projects. I use this technique for some closely related projects where I often need to pull changes from one project into another. It's similar to the orphaned branches idea but the branches don't need to be orphaned. Simply start all the projects from the same empty directory state.
这是为多个项目使用单个目录。我将这种技术用于一些密切相关的项目,在这些项目中我经常需要将更改从一个项目拉到另一个项目中。它类似于孤立分支的想法,但分支不需要孤立。只需从相同的空目录状态启动所有项目。
Start all projects from one committed empty directory
从一个提交的空目录启动所有项目
Don't expect wonders from this solution. As I see it, you are always going to have annoyances with untracked files. Git doesn't really have a clue what to do with them and so if there are intermediate files generated by a compiler and ignored by your .gitignore file, it is likely that they will be left hanging some of the time if you try rapidly swapping between - for example - your software project and a PH.D thesis project.
不要指望此解决方案会带来奇迹。在我看来,您总是会对未跟踪的文件感到烦恼。Git 真的不知道如何处理它们,所以如果有编译器生成的中间文件并被 .gitignore 文件忽略,如果您尝试快速交换,它们很可能会在某些时候挂起例如,您的软件项目和 PH.D 论文项目之间。
However here is the plan. Start as you ought to start any git projects, by committing the empty repository, and then start all your projects from the same empty directory state. That way you are certain that the two lots of files are fairly independent. Also, give your branches a proper name and don't lazily just use "master". Your projects need to be separate so give them appropriate names.
然而,这是计划。通过提交空存储库来启动任何 git 项目,然后从相同的空目录状态启动所有项目。这样你就可以确定这两批文件是相当独立的。另外,给你的分支一个合适的名字,不要懒惰地只使用“master”。你的项目需要分开,所以给他们合适的名字。
Git commits (and hence tags and branches) basically store the state of a directory and its subdirectories and Git has no idea whether these are parts of the same or different projectsso really there is no problem for git storing different projects in the same repository. The problem is then for you clearing up the untracked files from one project when using another, or separating the projects later.
Git 提交(以及标签和分支)基本上存储目录及其子目录的状态,Git 不知道这些是相同项目还是不同项目的一部分,因此 git 将不同项目存储在同一存储库中确实没有问题。那么问题在于您在使用另一个项目时清除一个项目中未跟踪的文件,或者稍后分离项目。
Create an empty repository
创建一个空的存储库
cd some_empty_directory
git init
touch .gitignore
git add .gitignore
git commit -m empty
git tag EMPTY
Start your projects from empty.
从空开始你的项目。
Work on one project.
在一个项目上工作。
git branch software EMPTY
git checkout software
echo "array board[8,8] of piece" > chess.prog
git add chess.prog
git commit -m "chess program"
Start another project
开始另一个项目
whenever you like.
只要你喜欢。
git branch thesis EMPTY
git checkout thesis
echo "the meaning of meaning" > philosophy_doctorate.txt
git add philosophy_doctorate.txt
git commit -m "Ph.D"
Switch back and forth
来回切换
Go back and forwards between projects whenever you like. This example goes back to the chess software project.
随时在项目之间来回切换。这个例子可以追溯到国际象棋软件项目。
git checkout software
echo "while not end_of_game do make_move()" >> chess.prog
git add chess.prog
git commit -m "improved chess program"
Untracked files are annoying
未跟踪的文件很烦人
You will however be annoyed by untracked files when swapping between projects/branches.
但是,在项目/分支之间交换时,您会对未跟踪的文件感到恼火。
touch untracked_software_file.prog
git checkout thesis
ls
philosophy_doctorate.txt untracked_software_file.prog
It's not an insurmountable problem
这不是一个无法克服的问题
Sort of by definition, git doesn't really know what to do with untracked files and it's up to you to deal with them. You can stop untracked files from being carried around from one branch to another as follows.
顾名思义,git 并不真正知道如何处理未跟踪的文件,由您来处理它们。您可以阻止未跟踪的文件从一个分支转移到另一个分支,如下所示。
git checkout EMPTY
ls
untracked_software_file.prog
rm -r *
(directory is now really empty, apart from the repository stuff!)
git checkout thesis
ls
philosophy_doctorate.txt
By ensuring that the directory was empty before checking out our new project we made sure there were no hanging untracked files from another project.
通过在检查我们的新项目之前确保目录是空的,我们确保没有来自另一个项目的未跟踪的挂起文件。
A refinement
细化
$ GIT_AUTHOR_DATE='2001-01-01:T01:01:01' GIT_COMMITTER_DATE='2001-01-01T01:01:01' git commit -m empty
If the same dates are specified whenever committing an empty repository, then independently created empty repository commits can have the same SHA1 code. This allows two repositories to be created independently and then merged together into a single tree with a common root in one repository later.
如果每次提交空存储库时都指定了相同的日期,则独立创建的空存储库提交可以具有相同的 SHA1 代码。这允许独立创建两个存储库,然后将它们合并到一个具有公共根目录的单个树中。
Example
例子
# Create thesis repository.
# Merge existing chess repository branch into it
mkdir single_repo_for_thesis_and_chess
cd single_repo_for_thesis_and_chess
git init
touch .gitignore
git add .gitignore
GIT_AUTHOR_DATE='2001-01-01:T01:01:01' GIT_COMMITTER_DATE='2001-01-01:T01:01:01' git commit -m empty
git tag EMPTY
echo "the meaning of meaning" > thesis.txt
git add thesis.txt
git commit -m "Wrote my PH.D"
git branch -m master thesis
# It's as simple as this ...
git remote add chess ../chessrepository/.git
git fetch chess chess:chess
Result
结果
Use subdirectories per project?
每个项目使用子目录?
It may also help if you keep your projects in subdirectories where possible, e.g. instead of having files
如果您尽可能将项目保存在子目录中,这也可能会有所帮助,例如,而不是使用文件
chess.prog
philosophy_doctorate.txt
have
有
chess/chess.prog
thesis/philosophy_doctorate.txt
In this case your untracked software file will be chess/untracked_software_file.prog
. When working in the thesis
directory you should not be disturbed by untracked chess program files, and you may find occasions when you can work happily without deleting untracked files from other projects.
在这种情况下,您未跟踪的软件文件将是chess/untracked_software_file.prog
. 在thesis
目录中工作时,您不应该被未跟踪的国际象棋程序文件打扰,您可能会发现可以愉快地工作的场合,而无需从其他项目中删除未跟踪的文件。
Also, if you want to remove untracked files from other projects, it will be quicker (and less prone to error) to dump an unwanted directory than to remove unwanted files by selecting each of them.
此外,如果您想从其他项目中删除未跟踪的文件,转储不需要的目录比通过选择每个文件来删除不需要的文件更快(并且更不容易出错)。
Branch names can include '/' characters
分支名称可以包含“/”字符
So you might want to name your branches something like
所以你可能想给你的分支命名
project1/master
project1/featureABC
project2/master
project2/featureXYZ
回答by Hitmands
I would use git submodules
.
我会使用git submodules
.
have a look here Git repository in a Git repository
As per February2019, I would suggest Monorepos
根据 2019 年 2 月,我建议 Monorepos