git:将一些文件的历史拆分成单独的分支 [英] git: Split history of some files into a separate branch

查看:154
本文介绍了git:将一些文件的历史拆分成单独的分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我介绍了< feature.c>前一段时间,现在注意到它不应该是我的 main 分支的一部分,而是一个分支特性的一部分。是否可以使用例如 git-filter-branch 自动将< feature.c>的所有历史记录移出我的分支进入功能分支?

解决方案

听起来像是你在做相当疯狂的东西! :)



也就是说,我看到了一些选项,其中没有一个是特别自动的。


  1. 如果你有很多提交这个文件的提交,只要承认这个错误,从你的HEAD创建一个新的分支,并进一步提交该分支的提交,直到他们稳定。如果您的回购是共享的,这将成为唯一真正的选择。没有人想要有不同的历史,特别是如果这个功能在历史深处发生的话。

  2. 如果你只谈论10次左右的提交实际上触及了该文件,并且它们最近在没有其他许多提交交错的情况下提交,您可以在HEAD上检出一个新分支,并在添加该功能之前将不想使用此功能的分支恢复到之前的状态樱桃选择提交你需要的功能分支,直到你准备在日后提交所有。
  3. 如果你正在处理大量的交叉提交,而且你真的不想让这个功能出现,你可以写一个shell脚本来输出 git log 和樱桃选择它到一个新的分支。

      $ cd git-repo 
    $ git checkout -b特性-x
    $ the-perfect-shell-script`git log --pretty = format:%Hpath / to / feature.c`

    一旦你有了所有提交樱桃挑选出的功能分支,你可以使用 git filter-branch 来过滤掉所有引用该文件的提交。 手册页有一个简单的例子,就是这样。



    一旦你有了,你可以 git rebase feature-x --onto< filtered-branch> ,你应该当然,这应该是相当的不鼓励,特别是如果 发布。

  4. >


Say I introduced <feature.c> a while ago and now notice it shouldn't have been part of my main branch but rather a branch feature. Is it possible to use e.g. git-filter-branch to automatically move all of <feature.c>'s history out of my main branch into the feature branch?

解决方案

It sounds like you're doing something fairly insane! :)

That said, I see a few options, none of which are particularly automated.

  1. If you've got a ton of commits with that file present, just admit the mistake, make a new branch off of your HEAD, and put further commits with that feature into that branch until they're stable. If you're repo's shared, this becomes the only real option. No one wants to have divergent history, especially if that feature was committed deep in the history.

  2. If you're only talking about 10 or so commits that have actually touched that file, and they're fairly recently committed without many other commits interleaved, you could check out a new branch on HEAD, and revert the branch you don't want this feature in back to before you added the feature, and then cherry pick commits you need out of the feature branch until you're ready to commit them all in at a later date.

  3. If you're dealing with a ton of history, lots of interleaved commits, and you really don't want to have that feature present at all, you could write up a little shell script that takes the output of git log and cherry picks it into a new branch. Something along the lines of:

    $ cd git-repo
    $ git checkout -b feature-x
    $ the-perfect-shell-script `git log --pretty=format:"%H" path/to/feature.c`
    

    Once you have that feature branch with all of the commits cherry picked out, you can then use git filter-branch to filter out all of the commits that reference that file. The man page has a simple example that does exactly that.

    Once you've got that, you can then git rebase feature-x --onto <filtered-branch> and you should be good to go.

    Of course that should be quite discouraged, especially if any of that is published.

这篇关于git:将一些文件的历史拆分成单独的分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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