如何在git中实现“浮动”的私有分支何时与上游合并? [英] How to achieve a private branch in git that "floats" when merging with upstream?

查看:171
本文介绍了如何在git中实现“浮动”的私有分支何时与上游合并?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有另一个组织仓库的分支。我在回购的最新标签,这不是头,我从该标签创建了一个永远不会推向上游的分支。这就是为什么我认为这个分支是私人的。



我已经提交到我的私人分支,并且正在我的生产环境中使用该代码。当向上游存储库添加一个新标签时,我希望能够修改它们。



但是,我希望始终保持提交的整洁堆叠在他们最后的标签之上。我不想合并,因为我的提交最终会在历史中居住,我希望看到它们在最前面,所以我可以在我的存储库上使用某些工具时轻松地使用它们。



实际上,我想要一个浮动分支,当我将上游更改带到我的存储库时,我可以将其移植到任意点。



<编辑]
我不相信我可以使用rebase,但是,因为这是一个历史重写操作。你看,我在两台机器上使用我的存储库,即我的开发和生产。我在开发机器上进行提交,推送到github,然后转到生产。所有这些与我最初分拣的上游存储库上的更改无关。



我不完全清楚移植,樱桃采摘或任何其他工具可能适合。无论它是哪种工具,我都认为它不应该重写历史。从我的阅读中,我发现重推回购历史在推动时是不可能的。所以我不确定我应该使用什么命令,这将移植一个分支而不会重写历史记录。



如果我使用的是mercurial,我可能会考虑类似于版本 - 受控mq。我不知道git的类似解决方案,如果有的话,或者是否有其他更适合git的工具。

[edit]



在评估了我在这里得到的答案之后,我终于确定选樱桃是正确的答案。在任何情况下,rebase都会删除历史记录,并且由于这是一个共享存储库,所以删除历史记录是不可接受的,至少根据我读过的每一个源文件。



樱桃采摘,但是,将提交复制到我的工作树上,而不从原始位置移除它。因此,我可以在最新的标签之上种植一份我的更改,并将它们放在一个漂亮的整洁的纸堆中。

为了记录,我也尝试过Mercurial,通过使用hg-git扩展,它允许您使用hg克隆git存储库。这有好有坏。最大的负面影响是,当我完成使用它时,它无法推动。 hg-git很乐意工作到那一刻,然后告诉你它不会用hg 1.9进行推送。至少可以说,虚假。另一方面,克隆和拉动大量变化非常缓慢。然而,mq和TortoiseHg的合并冲突解决工具相对于git cherry-pick和Smartgit的合并冲突解决方案是一个巨大的改进。我希望hg-git可以工作。



我想最后,浮动对于我的更改分支并不是很好的描述,因为结果是复制它而不是移动它。对不起,可能是我的描述很差,我仍然在搞清楚选项是什么。感谢您的帮助。

解决方案

  git fetch 
git rebase --onto latesttag previoustag yourbranch

冲突必须处理,完全取决于您对可能使用的文件所做的操作已经从一个标签变成了另一个。特别是,如果有人已经删除了一些您更改的代码,您将不得不解决这个问题,并确保您更改的代码现在可以应用到别处。这没有银弹。一旦你解决了你的冲突,一切正常,

  git add -A 
git rebase --continue


如果你在做这只是能够部署,浮动分支可能不是处理这个问题的最佳方法。考虑这些替代方案:


  1. 部署脚本来操纵文件以确保部署能够正常进行。

  2. 涂抹/填充工作目录时修改文件的清理脚本。 ( https://git-scm.com/book / en / v2 / Customizing-Git-Git-Attributes#_keyword_expansion

你的浮动分支似乎就在那里只是为了你自己的环境,因此不应该是项目的一部分。


I have a fork of another organization's repository. I'm at the the latest tag in the repo, which is not the head, and I've created a branch from that tag which will never be pushed upstream. That's why I consider the branch private.

I've made commits to my private branch and am using that code in my production environment. When a new tag is made to the upstream repository, I want to be able to pull their changes.

However, I'd like to always keep my commits in a neat stack on top of their last tag. I don't want to merge then, since my commits will end up living far back in the history and I want to see them right on top so I can work with them easily when I use certain tools on my repository.

So really, I want a "floating" branch, one that I can transplant to an arbitrary point when I bring the upstream changes to my repository.

[edits] I don't believe I can use rebase, however, since that's a history-rewriting operation. You see, I use my repository on two machines, my development and production. I make my commits on the development machine, push to github, then pull to production. All of this has nothing to do with the changes on the upstream repository that I originally forked from.

I'm not entirely clear on transplanting, cherry-picking or whatever other tool might be suited. Whichever tool it is though, I gather it shouldn't rewrite history. From my reading, I see that rewriting repo history is a no-no when pushing . So I'm not sure what commands I should be using that will transplant a branch without rewriting history.

If I were using mercurial, I might consider something like a version-controlled mq. I don't know the analogous solution for git, if there is one, or whether there is another, better-suited tool for git.

[edit]

After evaluating the responses I got here, I finally determined that cherry-picking was the right answer. In all cases, rebase removes history, and since this is a shared repository, removing history is unacceptable, at least according to every source I've read.

Cherry-picking, however, copies the commit to my working tree without removing it from its original location. So I can plant a copy of my changes on top of the latest tag and have them in a nice neat pile.

For the record, I also tried to do this with Mercurial, by using the hg-git extension which lets you use hg to clone a git repository. This had pluses and minuses. The biggest minus was that when I finished using it, it couldn't push. hg-git works happily until that point, then tells you that it doesn't push with hg 1.9. Bogus, to say the least. The other minus was that cloning and pulling a large set of changes is extremely slow. However, mq and TortoiseHg's merge conflict resolution tools are a vast improvement over git cherry-pick and Smartgit's merge conflict resolution. I wish hg-git could have worked.

I guess that in the end, "floating" wasn't such a good description for my change branch, since the result is to copy it rather than move it. Sorry for my perhaps poor description, I was still figuring out exactly what the options were. Thanks for the help.

解决方案

git fetch
git rebase --onto latesttag previoustag yourbranch

Conflicts have to be dealt with and entirely depend on what you are doing with files that may have changed from one tag to the next. In particular, if someone has eliminated some code that you change, you will have to address this and make sure that the code you changed can now be applied elsewhere. There is no silver bullet for this. Once you resolved your conflicts and everything works,

git add -A
git rebase --continue

Rinse repeat.

To add, if you are doing this to just be able to deploy, a "floating branch" may not be the best method for dealing with this. Consider these alternates:

  1. deploy scripts that manipulate the files to ensure the deploy works.
  2. smudge/clean scripts that modify files when populating the working directory. (https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#_keyword_expansion)

Your floating branch seems like it is there only for the sake of your own environment and hence should not be part of the project.

这篇关于如何在git中实现“浮动”的私有分支何时与上游合并?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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