简体   繁体   English

我是否正确使用了cherry-pick

[英]Am i using cherry-pick correctly

I am not sure if i use Git correctly so i would like to know what you think about my way and how to make it better.我不确定我是否正确使用 Git,所以我想知道您对我的方式的看法以及如何改进它。
I`m programming a website were i want to include new features.我正在编写一个网站,我想包含新功能。 But for these features i don´t need the complete rest of the website.但是对于这些功能,我不需要网站的完整其余部分。 So what i do is this:所以我要做的是:

  1. on branch "master" i create a branch called "justBasics" were i deleted everything that is unnecessary (which is nearly everything).在分支“master”上,我创建了一个名为“justBasics”的分支,我删除了所有不必要的东西(几乎就是所有东西)。
  2. on branch "justBasics" i create my feature Branch "feature1".在分支“justBasics”上,我创建了我的功能分支“feature1”。 In "feature1" i can, for example, create a new file where i write my feature in.例如,在“feature1”中,我可以创建一个新文件,在其中写入我的功能。
  3. Now i want to include feature1 to the master.现在我想将 feature1 包含到 master 中。 The bad thing is that the branch "feature1" also includes stuff that i just need to try out my feature and doesn´t belong to the master so cant merge it to master (Or can i use merge? I don´t know how.).不好的是,分支“feature1”还包括我只需要尝试我的功能并且不属于 master 的东西,所以不能将它合并到 master (或者我可以使用合并吗?我不知道如何。 )。 For example some code for testing the feature or especially the stuff that i deleted to create "justBasics".例如一些用于测试该功能的代码,或者特别是我删除以创建“justBasics”的内容。
  4. To include the feature i go back to "master" and create the branch "includeFeature1".为了包含功能,我回到“master”并创建分支“includeFeature1”。 In "includeFeature1" i get only the important commits of the feature by cherry-pick.在“includeFeature1”中,我仅通过cherry-pick 获得了该功能的重要提交。
  5. now i can merge "includeFeature1" to master现在我可以将“includeFeature1”合并到master

This is how my repository would look at the end:这是我的存储库最后的样子:

* c9     (master) merging branch "includeFeature1"
|\
* | c8   some work on master to make this look better
| * c7   (includeFeature1)cherry picking commit c3 and c5 
|/
* c6     some work on master to make this look better
| * c5   (feature1) finished feature1
| * c4   some code that is necessary to test feature1. I don´t want this code at the end in my website.
| * c3   started feature1. add a file to work in
| * c2   (JustBasics) i deleted folders, features, preparing main.html to show nothing.
|/
* c1     website til this point. that includes other features

This way works for me but i get some problems with cherry-pick: Somehow when i want to checkout some other branch some old Hunks reappear.这种方式对我有用,但我在挑选樱桃时遇到了一些问题:不知何故,当我想结帐其他一些分支时,一些旧的帅哥又出现了。 Especially when i am on master and i try to checkout "JustBasics", the Hunks from c2 were i deleted all the folders and features reappear and i can´t switch branches.尤其是当我在 master 上并尝试结帐“JustBasics”时,c2 中的帅哥被我删除了所有文件夹和功能重新出现并且我无法切换分支。 So i have to delete them before i can continue.所以我必须先删除它们才能继续。

How do you work on complete new features were the rest of your code would disturb you?如果您的其余代码会打扰您,您如何处理完整的新功能?
What do you think about my way?你觉得我的方式怎么样?

Generally: git was not designed to have branches where you only work on a subset of the repo's content.通常:git 的设计初衷不是为了让你只处理 repo 内容的一个子集的分支。 As you've seen, you can work out a workflow that mostly does what you want, but there's always some rough edge.正如您所见,您可以制定出一个主要完成您想要的工作的工作流程,但总有一些不完善的地方。

If there's a subset of code that should be worked on in isolation, I'd recommend splitting that into its own repository;如果有一部分代码应该单独处理,我建议将其拆分到自己的存储库中; then it becomes a dependency management issue (ie how to recombine the right versions at build time) - which can be addressed with any number of tools, or possibly with submodules or subtrees if you want a git-only solution.那么它就变成了一个依赖管理问题(即如何在构建时重新组合正确的版本)——这可以用任意数量的工具来解决,如果你想要一个 git-only 解决方案,也可以用子模块或子树来解决。

Here are some maybe-even-bigger problems with the "branch that deletes a bunch of files" approach:以下是“删除一堆文件的分支”方法的一些可能甚至更大的问题:

If you ever do additional work on feature1 , then you'll have no good way to know what commits you've already cherry-picked into master , vs. what commits you never meant to cherry-pick over, vs. what new commits you actually want to cherry-pick over.如果您曾经在feature1feature1额外的工作,那么您将没有好的方法来了解您已经将哪些提交挑选到master ,与哪些提交您从未打算挑选过,以及哪些新提交给您其实是想挑挑拣拣过来的。 Ok, but you could rig up a solution to that using tags and maybe commit message conventions (and one more custom thing to remember when using your repo).好的,但是您可以使用标签来设置解决方案,并且可能提交消息约定(以及在使用您的存储库时要记住的另一件自定义事情)。

If there are ever changes on master that you want to share into justBasics you may find that difficult.如果您想将master上的更改分享到justBasics您可能会发现这很困难。 The merge will most likely conflict because "they edited, we deleted".合并很可能会发生冲突,因为“他们编辑了,我们删除了”。 Easy to resolve, but it will happen each time, and one more thing to remember.很容易解决,但每次都会发生,还有一件事要记住。

You kind of questioned whether you could merge feature1 back to master .您有点质疑是否可以将feature1合并回master If you ever do, the merge will try to delete all those other files from master - because deleting a file is a change.如果您这样做,合并将尝试从master文件中删除所有其他文件 - 因为删除文件是一种更改。 As with the previous case, the commit as a whole will likely conflict, but individual files may be silently deleted because they hadn't been modified on the master side.与前一种情况一样,整个提交可能会发生冲突,但个别文件可能会被静默删除,因为它们尚未在master端修改。

While cherry-pick can be handy in specific situations - eg foundational changes were made on a branch and you need them but aren't yet ready to merge the branch in - it should be used with caution (a cherry-pick that conflicts is likely to cause a headache later);虽然cherry-pick可以在特定情况下是方便-如基础进行了更改,在树枝上,你需要他们,但还没有准备在合并分支-它应该谨慎(樱桃采摘的冲突很可能使用以后会引起头痛); and I generally recommend against creating workflows that depend on regular cherry-picking.我通常建议不要创建依赖于常规挑选的工作流程。

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

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