为以前的提交创建 git 分支:最好的方法是什么?

发布于 2024-12-09 15:25:55 字数 475 浏览 1 评论 0原文

想象一下以下情况:

  • 在“development”分支上完成了一堆相关的提交
  • 这些提交实际上应该是在“feature_x”分支上完成的
  • “feature_x”分支应该合并到“development”分支

图形示例:

当前情况:

development (HEAD) A--B--C--D--E

期望情况:

development         A--B
                        \
feature_x                C--D--E 

如何创建此功能分支,将这些先前的提交分组到该分支中,并创建“开发”分支看起来像没有对它进行任何个人提交?

Imagine the following:

  • A bunch of related commits are done on a 'development' branch
  • These commits should have actually been done to a 'feature_x' branch
  • The 'feature_x' branch should be merged into the 'development' branch

Graphed example:

Current situation:

development (HEAD) A--B--C--D--E

Desired situation:

development         A--B
                        \
feature_x                C--D--E 

How do I create this feature branch, group these previous commits into the branch, and make the 'development' branch look like no individual commits have been done to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

征棹 2024-12-16 15:25:55

你真的不需要太多:

git checkout devel
git checkout -b feature_x
# now devel and feature_x refer to commit E
git branch -f devel <hash of commit B>
# now you're still on feature_x, but devel is at commit B

You don't really need much to to that:

git checkout devel
git checkout -b feature_x
# now devel and feature_x refer to commit E
git branch -f devel <hash of commit B>
# now you're still on feature_x, but devel is at commit B
如日中天 2024-12-16 15:25:55
A--B--C--D--E(dev branch)
\
 \
   F--G(feature branch)

我假设你想变得像这样

A--(dev branch)
\
 \
   F--G--B--C--D--E(feature branch)

你可以使用 git rebase

在 dev 分支的 HEAD 上 执行此操作
创建一个临时分支用于变基目的

git branch temp
git rebase feature_branch

将您的开发分支移动到提交 A

git checkout dev_branch
git reset --hard <shasum of A>

将您的功能分支移动到提交 E 并删除临时分支

git checkout feature_branch
git reset --hard <shasum of E>
git branch -D temp
A--B--C--D--E(dev branch)
\
 \
   F--G(feature branch)

I assume you want to become like this

A--(dev branch)
\
 \
   F--G--B--C--D--E(feature branch)

You can use git rebase to do this

on the HEAD of dev branch
create a temp branch for rebasing purpose

git branch temp
git rebase feature_branch

move your devbranch to commit A

git checkout dev_branch
git reset --hard <shasum of A>

move your feature branch to commit E and delete temp branch

git checkout feature_branch
git reset --hard <shasum of E>
git branch -D temp
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文