git - 从另一个分支上的选定提交创建一个 git 分支

标签 git merge rebase git-rebase

我在 master 之外创建了一个“功能”分支并工作了很长时间。然后,我获取了最新的主分支提交,并在其之上重新设置了我的“功能”分支提交。然后我将“功能” merge 到 master 中。但是,我忘记了 merge 并继续提交到“功能”分支。我现在想要在一个新分支中进行这些提交,所以我现在想创建另一个分支“feature_2”,它基于自上次 merge 到 master 以来“feature”分支上的提交。有什么建议吗?

最佳答案

考虑到:

  • feature 已经引用自上次 merge 到 master
  • 以来的提交
  • 分支只是指针
x--x--x (master)
       \
        y--y--y (feature)

,你可以简单地:

  • git checkout 功能
  • git checkout -b feature2
  • git branch -f feature master
    (前提是自 feature merge 后没有对 master 进行提交)
x--x--x (master, feature)
       \
        y--y--y (feature2)

feature 不再引用 master 的所有提交(重置为 master 所在的位置),但现在可以通过 feature2 访问>(在重置为 master 之前,feature 所在的位置)


OP Chip Castle添加:

I have 3 other team members who have had their branches merged into master since then, so I don't want to merge the same commits I have already merged.
I was hoping to give a SHA range only for the commits I need from feature to a new branch. Is there any way to do that or a better way?

所以情况是:

x--x--x--y'--y' (master, updated after a fetch from other repo)
       \
        y'--y'--y--y (feature, with y' being already merged, 
                      and y being the commits I need)

然后您可以在 master 之上简单地 rebase 功能:相同的提交将被忽略。

关于git - 从另一个分支上的选定提交创建一个 git 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3498710/

相关文章:

GIT下的Django媒体文件

php - 在PHP中合并两个json

git rebase 交互式 : squash merge commits together

git - 我怎样才能 rebase 和保留 merge 并进行压缩/修复

git - 如何在 Cygwin 终端中指定 git 路径?

git - Git “pairing broken” 和 “unknown” 状态是什么意思,它们何时出现?

Python Pandas Merge --- 哪些行没有合并?

merge - 如何接受 Remote 上的所有更改?

git - 如何在运行 "Loose Object"时跳过 'git gui' pop 窗口

GitHub - 查找与提交关联的 pull 请求