如何与多个开发人员共享 git 功能(或主题)分支

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

How to share a git feature (or topic) branch with multiple developers

gitgit-branchgit-flowremote-branchfeature-branch

提问by duduklein

I'm following the the workflow described here, as I found many references pointing to this page as a good workflow. As mentioned in the article, "feature" branches are shared between developers, but do not go to the central repository.

我正在遵循此处描述的工作流程,因为我发现许多参考资料都指出此页面是一个很好的工作流程。正如文章中提到的,“功能”分支在开发人员之间共享,但不会转到中央存储库。

Let's say a developer "A" starts a new feature branch with git checkout -b newfeature develop. Now let's say that developer "B" needs also to work on this feature. This is my problem.

假设开发人员“A”使用git checkout -b newfeature develop. 现在假设开发人员“B”也需要处理此功能。这是我的问题。

What I did:

我做了什么:

  1. developer "B" adds developer A's machine as a remote
  2. developer "B" runs git branch remoteA/newfeature
  3. developer "B" works on this branch, commit his work and pushes the changes back to remoteA.
  1. 开发人员“B”将开发人员 A 的机器添加为远程
  2. 开发人员“B”运行 git branch remoteA/newfeature
  3. 开发人员“B”在这个分支上工作,提交他的工作并将更改推送回 remoteA。

Step 3 is not working, right now. I get a message:

现在,第 3 步不起作用。我收到一条消息:

remote: error: By default, updating the current branch in a non-bare repository is denied, because it will make the index and work tree inconsistent with what you pushed, and will require 'git reset --hard' to match the work tree to HEAD.

remote: error: You can set 'receive.denyCurrentBranch' configuration variable to 'ignore' or 'warn' in the remote repository to allow pushing into its current branch; however, this is not recommended unless you arranged to update its work tree to match what you pushed in some other way.

remote: error: To squelch this message and still keep the default behaviour, set receive.denyCurrentBranch' configuration variable to 'refuse'.

远程:错误:默认情况下,更新非裸存储库中的当前分支会被拒绝,因为它会使索引和工作树与您推送的内容不一致,并且需要“git reset --hard”来匹配工作树头。

远程:错误:您可以在远程存储库中将 'receive.denyCurrentBranch' 配置变量设置为 'ignore' 或 'warn' 以允许推送到其当前分支;但是,除非您安排更新其工作树以匹配您以其他方式推送的内容,否则不建议这样做。

远程:错误:要消除此消息并仍保持默认行为,请将 receive.denyCurrentBranch 配置变量设置为 'refuse'。

I have already set sharedRepository = true, but it did not help.

我已经设置了sharedRepository = true,但没有帮助。

I have 2 questions:

我有两个问题:

  1. what's the correct way to share feature branches between developers?
  2. how can I push back the changes in developer B's repository to developer A's original one?
  1. 在开发人员之间共享功能分支的正确方法是什么?
  2. 如何将开发人员 B 的存储库中的更改推回到开发人员 A 的原始存储库中?

采纳答案by Bill Door

You can push to a non-bare repo. What you cannot do is push to a non-bare repo that has the branch that you are pushing to checked out. The reason for this should make sense. Changing the files that someone else is possibly working on would be incorrect.

您可以推送到非裸存储库。你不能做的是推送到一个非裸仓库,该仓库有你要推送的分支。这样做的原因应该是有道理的。更改其他人可能正在处理的文件是不正确的。

Typically you will want to push to master or some other common shared branch. In order to avoid this conflict, the owner of the remote non-bare repo should work on a local branch or at least some other branch. Then you can push to the shared branch.

通常你会想要推送到 master 或其他一些常见的共享分支。为了避免这种冲突,远程非裸仓库的所有者应该在本地分支或至少某个其他分支上工作。然后你可以推送到共享分支。

To use your example:

使用您的示例:

  1. developer "B" adds developer A's machine as a remote
  2. developer "B" runs git branch remoteA/newfeature
    1. developer "A" does work on a local branch. git checkout -b work-newfeature
  3. developer "B" works on this branch, commit his work and pushes the changes back to remoteA.
    1. Developer "A" rebases to get the new work: git rebase newfeature
  1. 开发人员“B”将开发人员 A 的机器添加为远程
  2. 开发人员“B”运行 git branch remoteA/newfeature
    1. 开发人员“A”确实在本地分支上工作。 git checkout -b work-newfeature
  3. 开发人员“B”在这个分支上工作,提交他的工作并将更改推送回 remoteA。
    1. 开发人员“A”变基以获得新作品: git rebase newfeature

回答by Tigraine

The easiest way to share feature branches is to simply push them to the central repository so anyone can pull from them. That way you can simply use the infrastructure you already have for your main repository and you can easily share code.

共享功能分支的最简单方法是简单地将它们推送到中央存储库,以便任何人都可以从中提取。这样您就可以简单地使用您已经拥有的主存储库的基础架构,并且可以轻松共享代码。

Once a feature branch on the remote is no longer needed you can simply delete it by doing a

一旦不再需要远程上的功能分支,您只需执行以下操作即可将其删除

git push <server> :branch

I'd advise against sharing directly between developer machines as that is prone to problems like users being on different networks (not connected to each other).

我建议不要在开发人员机器之间直接共享,因为这很容易出现用户位于不同网络(彼此未连接)等问题。

If possible you can also use the GitHub model where there is one central repository on the server (the blessed main repo). And besides that main repository each developer has a "fork" of that repository where he has full commit access and can push branches to his liking.

如果可能,您还可以使用 GitHub 模型,其中服务器上有一个中央存储库(受祝福的主存储库)。除了那个主存储库之外,每个开发人员都有一个该存储库的“分支”,在那里他有完全的提交访问权限,并且可以根据自己的喜好推送分支。

In this case you can add your coworkers forks as remotes to your repository while maintaining easy access to the one centralized server (saving you the hassle of setting up SSH keys on every machine etc etc)..

在这种情况下,您可以将您的同事分叉作为远程添加到您的存储库,同时保持对一个中央服务器的轻松访问(省去您在每台机器上设置 SSH 密钥等的麻烦)。

A description on the GitHub model can be found here: http://www.eqqon.com/index.php/Collaborative_Github_Workflow

关于 GitHub 模型的描述可以在这里找到:http: //www.eqqon.com/index.php/Collaborative_Github_Workflow

Update: As a commentor pointed out this is a good link to get started with the centralized-feature-branch workflow: http://nvie.com/posts/a-successful-git-branching-model/

更新:正如评论者指出的,这是开始集中功能分支工作流程的一个很好的链接:http: //nvie.com/posts/a-successful-git-branching-model/

Update2: To expand on your second question:

Update2:扩展您的第二个问题:

What you are trying to do is push to a non-bare repository of another developer. Git introduced in some previous version (I think 1.6 or so) the concept of a bare repository - that's a repository that has nochecked out state but only contains the database that normally goes into .git.

您要做的是推送到另一个开发人员的非裸存储库。Git 在一些以前的版本(我认为 1.6 左右)中引入了裸存储库的概念 - 这是一个没有检出状态但只包含通常进入 .gitignore 的数据库的存储库.git

The reasoning behind this change was that whenever you push to your fellow co-workers repository (who is currently working on something) - you are manipulating the repository right under his nose. So he checks out version featureA-1 .. starts work .. you then push featureA-2 onto his repo and when he wants to commit he runs into trouble because the branch he was in has advanced by one commit he didn't see during development.

这一变化背后的原因是,每当你推送到你的同事存储库(他目前正在做某事)时 - 你就在他的眼皮底下操纵存储库。所以他检查了版本 featureA-1 .. 开始工作 .. 然后你将 featureA-2 推送到他的仓库,当他想要提交时,他遇到了麻烦,因为他所在的分支已经推进了一个他没有看到的提交发展。

Because this is quite disruptive - Most people have adopted the notion of local git repositories (the ones where you actively do work on) should be privatewhile you have a public git-repo (the fork) where you receive and share changes. That way you will never be interrupted by anyone else during your work (that's the whole idea behind the decentralized model anyway) and can only incorporate changes you want to have. (Nobody can push something onto your current work).

因为这非常具有破坏性 - 大多数人都采用了本地 git 存储库(您积极工作的那些)应该是私有的,而您拥有一个公共 git-repo(fork),您可以在其中接收和共享更改。这样你就永远不会在你的工作中被其他人打断(无论如何,这就是去中心化模型背后的整个想法),并且只能合并你想要的变化。(没有人可以将某些东西推到您当前的工作中)。