简体   繁体   English

如何管理Git“上游”分支和相关补丁?

[英]How to manage a Git “upstream” branch and related patches?

Recently I had an issue where I was given a patch for an assignment, as the professor had changed the code to add new functionality. 最近我遇到了一个问题,我给了一个分配补丁,因为教授已经改变了代码来添加新的功能。 Unfortunately, I had already put the original codebase in git and had made lots of changes and commits already. 不幸的是,我已经将原始代码库放在git中并且已经做了很多更改和提交。 The workflow I used to apply the patch is as follows: 我用来应用补丁的工作流程如下:

git checkout <hash_of_where_patch_should_go>
git checkout -b patch_branch
git apply patch
git add ./* && git commit -m "applying patch"
git rebase master patch_branch
    //Fix merge conflicts
git rebase patch_branch master

This worked wonderfully, but my question is this: is this the 'correct' way to perform such a task? 这非常有效,但我的问题是:这是执行此类任务的“正确”方法吗? It seems to me like there must be a more straightforward way. 在我看来,必须有一种更直接的方式。

Assuming it's not simpler to just apply the patch and deal with any rejects directly, I'd 假设仅仅应用补丁并直接处理任何拒绝并不简单,我会

git checkout $patch_base
git apply --index patch
git commit -m'patch from prof'
git checkout @{-1}

git cherry-pick @{-1}
# or git merge @{-1}

Keep a separate branch for the unmodified upstream code 为未修改的上游代码保留单独的分支

Create a branch where you put the code as it is released by your professor. 创建一个分支,您可以在其中放置教授发布的代码。 In your case, that will probably mean: 在您的情况下,这可能意味着:

git checkout -b upstream <commit_where_you_imported_professors_stuff>

Apply patches from upstream only to the upstream branch 仅将上游补丁应用于上游分支

When your professor gives you a patch to his (her?) code, you will then checkout your upstream branch, and apply the patch there – this should always be possible without conflicts. 当你的教授为你的(她的?)代码提供补丁时,你将检查你的upstream分支,然后在那里应用补丁 - 这应该总是可以没有冲突。

git checkout upstream
git apply <whatever>
git add -A
git commit -m 'Patch from upstream'

Branch your own work off upstream , and merge when needed upstream分支您自己的工作,并在需要时合并

Now, to start adding your own work, you will branch upstream , and commit to that branch. 现在,要开始添加自己的工作,您将分支upstream ,并提交到该分支。 In your case, this has already happened, because you're already working on the project. 在你的情况下,这已经发生了,因为你已经在做项目了。

git checkout -b my_work upstream
... work ... work ... work (no play)
git add <stuff>
git commit -m 'Work done'

When you have to modify upstream (the "framework" given to you by professor), you do it as described above (checkout, patch, commit), and then merge it into your work branch: 当你必须修改upstream (教授给你的“框架”)时,你可以按照上面的描述(checkout,patch,commit)进行修改,然后将它合并到你的工作分支中:

git checkout upstream
... patch upstream ...
git checkout my_work
git merge upstream

This way, you will get the following benefits: 这样,您将获得以下好处:

  1. It's clearly visible to you on the upstream branch what the current state of the framework is, and which patches have been given to you, and when. upstream分支上,您可以清楚地看到框架的当前状态是什么,以及哪些补丁已经提供给您,以及何时。
  2. When you have to patch upstream and merge it with your own work, it's easy to back out of this step if things go wrong (just do git merge --abort or remove the merge commit). 当你必须修补upstream并将其与你自己的工作合并时,如果出现问题,很容易退出这一步(只需执行git merge --abort或删除合并提交)。
  3. It's easy to see for you when during the course of the project you had to merge from Professor and which changes (and possibly merge conflicts on your work branch) that introduced. 在项目进行过程中,您必须从教授合并以及所引入的更改(以及可能合并您的工作分支的冲突)时很容易看到。
  4. (Optional, not always useful) If you make your own patches to the framework, you can make them on the upstream branch (instead of your "work branch"), which will make it easier for you to display ( diff ) them later on. (可选,并不总是有用)如果您为框架制作自己的补丁,可以在upstream分支(而不是“工作分支”)上创建它们,这将使您以后更容易显示( diff )它们。 You can think of those commits as your own upstream patches, living alongside those of Professor. 您可以将这些提交视为您自己的上游补丁,与教授一起生活。

DON'T use "rebase" for that 不要使用“rebase”

I strongly advise against doing "rebase", because you will effectively destroy important history information (many comparison operations become difficult or impossible if you do rebase ing instead of merge ing). 我强烈建议不要做“rebase”,因为你将有效地破坏重要的历史信息(如果你做的是rebase而不是merge许多比较操作变得困难或不可能)。

Only use rebase if you have trivial (usually one-commit) changes that you need to merge, and want to avoid creating a merge commit. 如果您需要合并的简单(通常是一次提交)更改,并且希望避免创建合并提交,则仅使用rebase。

Paul Stadig wrote a good piece about this topic (rebasing vs. merging): Thou Shalt Not Lie: git rebase, amend, squash, and other lies 保罗·斯塔迪格写了一篇关于这个话题的好文章(重新定位与合并): 你不应该说谎:git rebase,修正,壁球和其他谎言

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

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