Git - 如何有选择地将更改从一个分支应用到另一个分支? [英] Git - How to selectively apply changes from one branch to another?

查看:211
本文介绍了Git - 如何有选择地将更改从一个分支应用到另一个分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



更具体地说,我使用公共 dev c $ c>分支用于GitHub和一个私有 master 分支用于部署。当对一个分支进行更改时,他们需要应用到另一个分支,但是某些代码行需要保持不同。在我的情况下,它是一些CSS类和一个feed。



我是Git的新手,但是我已经完成了我的研究:
$可以使用b $ b


  • git merge --no-commit --no-ff ,然后使用 git mergetool 来选择我想要的以防止冲突。问题是,它只适用于冲突Git不能自动合并,所以我想保留不同的东西在我有机会使用我的mergetool之前被替换。


  • git difftool --cached 非常有用,因为它可以让我看到差异,但我需要复制我想要保留的内容并手动将其替换为文本编辑器,因为我不能简单地使用mergetool来选择和保存。

  • >似乎将指定的提交应用于另一个提交,但是我想保持不同的提交可能分散到不同的提交中,这些提交可能不仅包括我想保持不同的提交。我不能看到这个工作,除非我做了数以百万计的提交,这会让我疯狂。




另外要清楚的是, strong>我不希望一个分支成为另一个,合并的情况似乎是这样。 我希望有两个不同的分行,各自有不同的分歧,并应用从一个分行到另一个分行的变化。

是否有更好的工作流程可以让我通过应用更改并保留一些差异来保留开发和部署版本?我不介意使用单独的存储库或不同的工具,如果它导致解决方案。 我不认为存在解决方案一种从同一文件中选择部分提交的方法。我会说你需要简单地重新考虑你的代码,以便将这些部分移动到不同的文件中。



顺便说一句,如果你想从提交中获取一些文件,你可以按照此处的说明,使用其他命令组合的樱桃挑选。


Is it possible to selectively apply changes from one branch to another with Git?

More specifically, I use a public dev branch for GitHub and a private master branch for deployment. When changes are made to one branch they will need to be applied to the other, but some lines of code need to stay different. In my case it's a few css classes and a feed.

I'm new to Git but I've made my research :

  • git merge --no-commit --no-ff can be used followed by a git mergetool to chose what I want in case of a conflict. The problem is that it only works for conflicts Git can't automatically merge, so what I want to stay different gets replaced before I get the chance to use my mergetool.

  • git difftool --cached is useful as it allows me to see the differences, but I need to copy what I want to keep from there and manually replace it with a text editor, as I cannot simply choose and save like I can with mergetool.

  • git cherry-pick seems to apply a specified commit to another, but what I want to stay different may be scattered to different commits, and these commits may not only include what I want to stay different. I cannot see this working unless I make millions of commits that would drive me mad.

Also to be clear, I don't want one branch to become another, what seems to be the case with a merge. I want two separate branches with their respective differences and apply changes from one to the other.

Is there a better workflow that would allow me to keep a development and a deployment version by applying their changes and keeping a few differences? I don't mind using separate repositories or different tools if it leads to a solution.

解决方案

I don't think there is a way to pick parts of a commit from the same file. I would say you need to simply re-factor your code to move such parts in different files.

BTW, if you want to take some files from a commit, you can use cherry-pick with a combination of other commands as explained here.

这篇关于Git - 如何有选择地将更改从一个分支应用到另一个分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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