了解git cherry-pick [英] understanding git cherry-pick

查看:116
本文介绍了了解git cherry-pick的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自svn背景:由于(缺少)切换速度以及将分支合并回主干所用的小时或更多时间,我几乎从不分支。有时候,如果我需要在网站上修补一个问题,我会在后备箱中进行更改(这将与先前的更改或新功能一起),然后转到该文件,然后执行svn up path / to /文件名,它会更新只有该文件,解决问题,但保留其余的文件。



从概念上讲,这似乎不可能在git(或必要);它是否允许樱桃采摘的结构化分段和分组提交?因此,我可能会更改网站的特定区域,并将其作为一组进行提交,而不是像我使用svn一样进行操作,然后执行一天的工作并触碰整个批次的文件?

解决方案

在这种情况下,您可能想要做的是为您的修补程序创建一个新的分支,从所有需要分支的共同祖先分支出来修复。例如,假设您有一对维护版本以及您当前的开发版本:

   -  X  -  o  -  o  -  o  -  o -  o  -  o  -  o  -  o  -  o(主)
\\
o - o - o(release A)o - o(release B)

如果您需要制作一个适用于两个版本的修补程序,请从标记为X的提交开始创建一个分支。提交您的修补程序,然后合并分支到所有三个分支。



你可以选择樱桃,但是这里有一个关于何时挑选樱桃的好规则:不。唯一一个你想挑选樱桃的案例就是你严格管理你的分支机构。在这种情况下,这可能意味着您对主服务器进行了修复,而不是从较早的时间点将其正确分支,而且人们已经将更新提交给主服务器,因此您无法更改它。你必须选择在两个发布分支上进行选择。但是,当然,你应该首先正确地管理你的分支机构,而且你永远不需要挑选。 (是的,它有时会发生,这就是生活。)


Coming from an svn background: I hardly ever branched, due to the (lack of) speed of switching and the hour or more it took to merge branches back into the trunk. Sometimes, if I needed to hotfix a problem on a web site, I'd make the change in the trunk (which would live along with previous changes or new features) and then go to that file and just do "svn up path/to/filename" and it would update only that file, fixing the problem but sparing the rest of the files.

Conceptually, this doesn't seem possible in git (or necessary); is it structured staging and grouped commits that allow for cherry-picking? So, I might change a specific area of the site and commit it as a group instead of doing how I work with svn, and go about a day's work and touch files all over the commit the whole batch at once?

解决方案

What you probably want to do in this case is make a new branch for your hotfix, branching off of the common ancestor of all branches which will need the fix. For example, suppose you have a couple maintained releases along with your current development:

- X - o - o - o - o - o - o - o - o - o (master)
   \                       \
    o - o - o (release A)   o - o (release B)

If you need to make a hotfix which will apply to both releases, create a branch starting from the commit labeled X. Commit your fix, then merge that branch into all three branches.

You could cherry-pick, but here's a good rule of thumb about when to cherry-pick: don't. The only case in which you will want to cherry-pick is when you've managed your branches badly. In this case, that might mean that you made the fix on master instead of properly branching it off from an earlier point, and people have already pulled the updates to master, so you can't change it. You'd have to cherry-pick to get it on the two release branches. But of course, you should just manage your branches right in the first place, and you'll never need to cherry-pick. (Yes, it'll still happen sometimes; such is life.)

这篇关于了解git cherry-pick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆