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

偶尔善良 提交于 2019-12-03 09:37:35

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.

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

Now, to start adding your own work, you will branch upstream, and commit to that branch. 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:

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.
  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).
  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. You can think of those commits as your own upstream patches, living alongside those of Professor.

DON'T use "rebase" for that

I strongly advise against doing "rebase", because you will effectively destroy important history information (many comparison operations become difficult or impossible if you do rebaseing instead of mergeing).

Only use rebase if you have trivial (usually one-commit) changes that you need to merge, and want to avoid creating a merge commit.

Paul Stadig wrote a good piece about this topic (rebasing vs. merging): Thou Shalt Not Lie: git rebase, amend, squash, and other lies

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}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!