Git - 将推送提交移动到不同的分支
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9086886/
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
Git - Moving Pushed Commits to a Different Branch
提问by Anthony Compton
My boss decided recently to try out an outsourcing group "for increased capacity" as we modify an existing application to add new features. Despite my concern that the group he chose didn't seem to communicate well and wasn't asking enough questions to truly understand and be effective, we went with them anyway.
我的老板最近决定在我们修改现有应用程序以添加新功能时尝试“增加容量”的外包小组。尽管我担心他选择的小组似乎没有很好的沟通,也没有提出足够的问题来真正理解和有效,但我们还是和他们一起去了。
We set them up to collaborate on our Git repository (hosted on GitHub). We created a branch just for them (TheOutsourcedBranch, we'll call it) and requested that they to do all of their work within this branch.
我们将它们设置为在我们的 Git 存储库(托管在 GitHub 上)上进行协作。我们专门为他们创建了一个分支(我们称之为 TheOutsourcedBranch)并要求他们在这个分支内完成所有的工作。
Additionally, we asked that they push their commits at the end of each day so that we can get a decent idea of how fast they work and (more importantly) how good their code is.
此外,我们要求他们在每天结束时推送他们的提交,以便我们可以很好地了解他们的工作速度以及(更重要的是)他们的代码有多好。
Today they pushed for the first time. Five commits, all of them in master. I'm trying to figure out how to move their commits from master into TheOutsourcedBranch, without losing the work they've done (although from the looks of it, we're going to have to throw it away anyway).
今天他们第一次推动。五次提交,都在 master 中。我试图弄清楚如何将他们的提交从 master 转移到 TheOutsourcedBranch,而不会丢失他们所做的工作(尽管从它的外观来看,无论如何我们都将不得不扔掉它)。
Orange dots are their commits, gray dots are mine.
橙色点是他们的提交,灰色点是我的。
I know there are a zillion questions about rebasing (and that might be what I need to do here) but I'm having a hard time figuring out which of them apply to my specific case. And in case it matters, the outsourcing group and myself are the only people that are really using the repo right now.
我知道有无数关于变基的问题(这可能是我在这里需要做的),但我很难弄清楚其中哪些适用于我的具体情况。万一重要的是,外包团队和我自己是目前真正在使用回购协议的唯一人员。
Thanks in advance!
提前致谢!
Edit:I may have found what I'm looking for: Move the most recent commit(s) to a new branch with Git
编辑:我可能已经找到了我要找的东西:使用 Git 将最近的提交移动到一个新分支
I'd like to check my understanding, though. Locally, I create a branch off of master (TheOutsourcedBranch), which would contain everything A-H. Then I reset master back to C. Master would now contain A-C.
不过,我想检查一下我的理解。在本地,我从 master (TheOutsourcedBranch) 创建了一个分支,其中包含所有 AH。然后我将 master 重置回 C。Master 现在将包含 AC。
Assuming that is correct, that would be fine for me (locally). But then what do I need to do to "force" the remote repository (GitHub) to accept my local view of how things played out and discard its version of the history?
假设这是正确的,那对我(本地)来说没问题。但是,我需要做什么来“强制”远程存储库 (GitHub) 接受我对事情如何进行的本地视图并丢弃其历史版本?
Once this has played out I think I can re-work this question to make it more generic/informative and cut out some fluff--otherwise it's probably a good candidate for deletion, I would guess.
一旦这件事发生了,我想我可以重新解决这个问题,使其更通用/信息丰富,并删除一些绒毛——否则我猜它可能是一个很好的删除候选者。
回答by Daniel Pittman
Getting the branch is easy:
获取分支很容易:
git branch their-branch master
git reset --hard master $SHA1_OF_C
git push --force $SHARED_REPO_REMOTE
This will rewrite history; it creates a new branch that is equivalent to the current master branch named their-branch
, then resets your local master to point to a random SHA1 (...or branch, or tag, or whatever), and finally forcibly updates the remote repository branch to match your local branch.
这将改写历史;它创建一个新的分支,相当于当前名为 的 master 分支their-branch
,然后将您的本地 master 重置为指向一个随机的 SHA1(...或分支,或标签,或其他),最后强行更新远程存储库分支以匹配您当地的分支机构。
That delivers exactlythe situation you want.
这正是您想要的情况。
Caveats: this will rewrite history, and can screw up anyone who has built off the old master. Watch out for merge problems following.
警告:这将改写历史,并可能搞砸任何建立在旧大师之上的人。注意以下合并问题。
No rebasing desired or required.
不需要或不需要变基。
(As an aside, I suggest you either give them a staging repository, or get them to use git send-email
, or use GitHub pull requests, or otherwise prevent them pushing to master until they get it right. Which, based on your summary, might be some time away.)
(顺便说一句,我建议你要么给他们一个临时存储库,要么让他们使用git send-email
,或使用 GitHub 拉取请求,或以其他方式阻止他们推送到掌握,直到他们做对了。根据你的总结,这可能是一些时间过去了。)
回答by Adam Dymitruk
Have them make a branch off of each bit of work from the same starting point for each ticket they work on. Have them merge the work to a RC branch. You can merge your work to the RC branch. This will allow you to test often. See the workflow we have here:
让他们从相同的起点为他们处理的每张工单创建一个分支。让他们将工作合并到 RC 分支。您可以将您的工作合并到 RC 分支。这将允许您经常测试。在此处查看我们的工作流程:
https://plus.google.com/109096274754593704906/posts/R4qkeyRadLR
https://plus.google.com/109096274754593704906/posts/R4qkeyRadLR
Let me know if you have any questions about it. It works well for us.
如果您对此有任何疑问,请告诉我。它对我们很有效。