撤销所有分支特定的更改,使分支反映主干

3

是否有可能回滚特定分支的所有更改而不丢失历史记录?

很久以前,我从master分支创建了一个分支来隐藏特定功能,但现在这个分支必须与master相同,但我需要保留分支历史记录。

master -> branched -> (revert stripping) -> branched = master

现在,branched应该反映出master,尽管我希望在branched中有一个提交,其中包含到达master所需的所有更改。

我能想到的最简单的方法是复制master的源代码,然后在branched中删除/粘贴文件,但我担心这样做会在以后从master合并时产生冲突?

2个回答

1

编辑:

Preuk的解决方案对我无效(Git 2.2.1),但以下命令有效:

$ git checkout branched
$ git revert -n master..branched
$ git commit -m "Revert to master"

初始帖子:

我建议在专用分支上使用git format-patch,然后使用git am,将提交压缩为一个提交使用git rebase -i,然后将结果应用于分支branched

  1. Create new branch starting from branched:

    $ git checkout branched
    $ git checkout -b tmpbranch
    
  2. Apply the reversed commits between master and branched:

    $ git format-patch -R master..branched --stdout | git am
    
  3. Squash the commits into a single one:

    $ git rebase -i branched
    $ # Use the squash command for all commits except the first one
    $ # and set the log message to "Revert to master" for instance.
    
  4. Then merge and delete the temporary branch tmpbranch onto branched:

    $ git checkout branched
    $ git merge tmpbranch
    $ git branch -d tmpbranch
    

这似乎是我正在寻找的,但它一直告诉我缺少“-m”标志。根据http://git-scm.com/docs/git-revert,如何获取父级编号?它不允许我使用“1”,我理解为第一个给定的分支。 - Daniel

0

你可以尝试使用压缩模式从主分支进行还原和变基。

首先,你需要从分支的开始处还原特定于该分支的提交。为此,可以使用类似 this 的工具:

git revert OLDER_COMMIT^..NEWER_COMMIT

然后,您将在卫星分支上获得1个提交,其中包含自分支拆分以来主分支的所有更改。

git checkout topicbranch
git rebase -i master

并选择每个提交的压缩。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接