简体   繁体   English

如何使用不同的身份合并Git分支?

[英]How to merge a Git branch using a different identity?

We are using Git for a website project where the develop branch will be the source of the test server, and the master branch will serve as the source for the live, production site. 我们正在将Git用于一个网站项目,在该项目中, develop分支将成为测试服务器的源,而master分支将成为实时生产站点的源。 The reason being to keep the git-related steps (switching branches, pushing and pulling) to a minimum for the intended user population. 原因是将与git相关的步骤(切换分支,推动和拉动)保持在最低限度,以适应预期的用户群。 It should be possible for these (not extremely technical) users to run a script that will merge develop into master , after being alerted that this would be pushed to live. 应该可以为这些(不是非常技术)用户运行一个脚本,将合并develop成为master ,被警告,这将是推住了。 master cannot be modified by normal users, only one special user can do the merge. 普通用户无法修改master ,只有一个特殊用户可以进行合并。

This is where I'm not sure how to integrate this identity change into my code below: 这是我不确定如何将此身份更改集成到下面的代码中的地方:

https://gist.github.com/jfix/9fb7d9e2510d112e83ee49af0fb9e27f https://gist.github.com/jfix/9fb7d9e2510d112e83ee49af0fb9e27f

I'm using the simple-git npm library. 我正在使用simple-git npm库。 But more generally, I'm not sure whether what I want to do is actually possible as I can't seem to find information about this anywhere. 但更笼统地说,我不确定我想做的事实际上是否可行,因为我似乎无法在任何地方找到有关此事的信息。

My intention would be of course to use a Github personal token instead of a password. 我的意图当然是使用Github个人令牌代替密码。

Git itself doesn't do anything about user or permission management. Git本身对用户或权限管理不做任何事情。 So, the short answer is, don't try to do anything sneaky. 因此,简短的答案是,不要试图做任何偷偷摸摸的事情。 Rather, use Github's user accounts they way they were intended. 相反,请按预期使用Github的用户帐户。

What I suggest is to give this special user their own Github account, with their own copy of the repo. 我建议给这个特殊用户自己的Github帐户,以及他们自己的回购副本。 Let's say the main repo is at https://github.com/yourteam/repo , and the special repo is at https://github.com/special/repo . 假设主仓库位于https://github.com/yourteam/repo ,特殊仓库位于https://github.com/special/repo

The script will pull changes from the team repo's develop branch, and merge this into it's own master branch and push to https://github.com/special/repo . 该脚本将从团队仓库的开发分支中提取更改,并将其合并到它自己的分支中,并推送到https://github.com/special/repo

Then, it will push its changes to the team's master branch. 然后,它将把更改推送到团队的分支。 This step can optionally be a forced push, since no one else is supposed to mess with master, anyway. 这一步可以选择是强制推送,因为无论如何也不会有人干扰主机。 (In case someone does, using a forced push here means they have to fix their local repo to match the team repo later on, rather than having the script fail until someone fixes the team repo.) (如果有人这样做,则在此处使用强制推送意味着他们必须稍后修复本地存储库以匹配团队存储库,而不是在有人修复团队存储库之前使脚本失败。)

At the same time, your CI software will notice that master has changed at https://github.com/special/repo , and will publish as you normally would. 同时,您的CI软件会在https://github.com/special/repo发现master已更改,并且会像通​​常一样发布。 This is the linchpin: the CI doesn't pay attention to the team repo, so although your team has permission to change it, those changes don't make it into production. 这很关键:配置项不会关注团队仓库,因此,尽管您的团队有权进行更改,但这些更改不会使其投入生产。

This special user will need commit access to the team repo, in addition to its own GitHub repo. 除了自己的GitHub存储库之外,该特殊用户还需要对团队存储库的提交访问权限。 The easiest way is probably to use an SSH key, and run the git push command from the script, rather than trying to use the GitHub API. 最简单的方法可能是使用SSH密钥,然后从脚本运行git push命令,而不是尝试使用GitHub API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM