Git 子分支或类似的概念

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30051111/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:58:31  来源:igfitidea点击:

Git sub branch or similar concept

git

提问by ArchNemSyS

Does git support a concept of a sub branch; ie a branch of a branch

git 是否支持子分支的概念;即一个分支的一个分支

For example a group of teachers have a bare shared repo on a network drive (no server ssh authentication etc) that they pull and push to on a shared drive merging into master when appropriate.

例如,一组教师在网络驱动器上有一个裸共享的存储库(没有服务器 ssh 身份验证等),他们在适当的时候在共享驱动器上拉取并推送到主驱动器。

This repo would include teacher files such as worked solutions as well as student files.

该存储库将包括教师文件,例如有效的解决方案以及学生文件。

Students shouldn't clone this repo nor checkout their respective teachers branch directly as it contains the answers.

学生不应该克隆这个 repo,也不应该直接检查他们各自的教师分支,因为它包含答案。

But the repo could contain a branch for each class/topic with only student files that tracks upstream changes. Students then branch this branch to do their work. Teacher tracks the student branch as a remote and pulls in the work.

但是 repo 可以包含每个类/主题的分支,只有跟踪上游更改的学生文件。学生然后分支这个分支来完成他们的工作。教师将学生分支作为远程跟踪并拉入工作。

The closest I have come to the above is cloning a single branch into a separate repo and having students branch in that repo. The problems with the above include obvious data duplication (windows support of system-links within git is poor) and extra class/topic repos that have to be kept updated with upstream changes.

我最接近上述的是将单个分支克隆到一个单独的回购中,并在该回购中让学生分支。上述问题包括明显的数据重复(Windows 对 git 中系统链接的支持很差)和额外的类/主题存储库,这些存储库必须随上游更改保持更新。

Any ideas how to better support this workflow in Git/LibGit2 ?

任何想法如何在 Git/LibGit2 中更好地支持此工作流程?

采纳答案by Matthieu Moy

I use a similar flow, but with two distinct histories (i.e. two different repositories that do not have any commit in common): one for teachers, and one for students. Having two separate histories has several advantages:

我使用了一个类似的流程,但有两个不同的历史(即两个不同的存储库,没有任何共同的提交):一个用于教师,另一个用于学生。拥有两个独立的历史记录有几个优点:

  • I can include generated files (e.g. *.pdf) in the student's repository, and track source files (e.g. *.tex) in the teacher's one.

  • I do not need to show individual commits made by teachers on the student's repository. They just see ~1 commit per class saying "new class online".

  • I chose which file I want to publish.

  • 我可以在学生的存储库中包含生成的文件(例如 *.pdf),并在老师的存储库中跟踪源文件(例如 *.tex)。

  • 我不需要在学生的存储库中显示教师所做的个人提交。他们只看到每班约 1 次提交说“新班级在线”。

  • 我选择了要发布的文件。

In my teacher's repository, I have a makefile with an make installtarget that copies the generated files to the student's repository. For convenience, I have targets make git-commit, make git-pushthat run git commitand git pushin the student's checkout.

在我老师的存储库中,我有一个带有make install目标的 makefile,它将生成的文件复制到学生的存储库。为方便起见,我有目标make git-commitmake git-push运行git commitgit push在学生的结帐中。

On the students side, they run git cloneonce, git pullwhenever I add new stuff. The can use my repository as a skeleton for their lab works, and work collaboratively (I advise them to use one remote for their coworker and one remote for me).

在学生方面,每当我添加新内容时,他们都会运行git clone一次git pull。他们可以使用我的存储库作为他们实验室工作的骨架,并协同工作(我建议他们为同事使用一个遥控器,为我使用一个遥控器)。

回答by Madara's Ghost

There's no notion of a main branch in git (masteris a completely normal branch that git provides by default). You can branch off of master, you can branch off of any other branch, you can branch off of a branch of a branch of a branch of a branch.

git 中没有主分支的概念(master是 git 默认提供的完全正常的分支)。你可以分支master,你可以分支任何其他分支,你可以分支一个分支的一个分支的一个分支。

To your specific questions, git doesn't have a concept of branch visibility. If I clone your repo, I get all of the branches just fine, and it doesn't matter how yourlocal repo tracks the remote branches.

对于您的具体问题,git 没有分支可见性的概念。如果我克隆你的 repo,我得到所有的分支就好了,你的本地 repo如何跟踪远程分支并不重要。

In short, if you don't want people having information that's available to the repo, don't allow them to clone it. Provide a different remote repo with just the information you need, and push only the relevant information there.

简而言之,如果您不希望人们拥有可用于 repo 的信息,请不要让他们克隆它。使用您需要的信息提供不同的远程存储库,并仅将相关信息推送到那里。

So for example, my repo has branches public, private-1and private-2, I can push all of my branches to origin(git push private-remote private-1etc), and have another remote setup where you strictly only push publicto it.

因此,例如,我的 repo 有分支publicprivate-1并且private-2,我可以将所有分支推送到origingit push private-remote private-1等),并进行另一个远程设置,您只能将public其推送到它。

Your students then clone the public remote, branch off of public, push their changes and their own branches, then you can pull those branches from public-remoteand push into your private-remote. Git supports multiple remotes just fine.

您的学生,然后克隆公众远程,岔开的public,把他们的变化和自己的分支机构,那么你可以把这些分支从public-remote和推到你private-remote。Git 支持多个遥控器就好了。

回答by VonC

Does git support a concept of a sub branch; ie a branch of a branch

git 是否支持子分支的概念;即一个分支的一个分支

No, a branch can be renamed, deleted or rebased (ie moved around) at any time.

不,分支可以随时重命名、删除或重新定位(即移动)。

It does support the notion of "namespace" (or hierarchy, or group), with the name of the branch including one or several "/": class/topiccan be the name of a branch.

它确实支持“命名空间”(或层次结构,或)的概念,分支的名称包括一个或多个“ /”:class/topic可以是分支的名称。

You find a similar use of namespace with gitolite(an ACL to control access to Git repos based on authentication).
See:

您会发现名称空间与gitolite(一种基于身份验证控制对 Git 存储库的访问的 ACL)的使用类似。
看: