如何将当前在一个分支上的一些最后的 Git 提交移动到另一个分支上?

发布于 2024-10-09 10:16:52 字数 848 浏览 7 评论 0原文

我有一个存储库,我一直在 master 分支上工作,最后添加了大约 10 个左右的提交,我现在希望这些提交位于另一个分支上,因为它们描述了我现在认为是实验性的工作(我仍在学习良好的 Git 实践)。

考虑到这一点,我现在希望将最后 10 次提交形成自己的分支,这样我就可以清理 master 并仅为“发布”/“稳定”提交保留。

为了说明这一点,我所拥有的是:

        b--b (feature B)
       /       
X--X--X--Z--Z--Z--Z--Z--Z (master)
    \
     a--a--a (feature A)

您可以看到标有 XZ 的提交都在 master 分支上,而我想要的是用 Z 标记的提交(现在被认为是实验性的“功能 Z”工作)位于自己的分支和以最右边的 X 结尾的 master 上。为了说明这一点,所需的图表:

        b--b (feature B)
       /       
X--X--X (master)
    \  \
     \  Z--Z--Z--Z--Z--Z (feature Z - the new branch I want)
      \
       a--a--a (feature A)

这样我将保留我的 master 用于发布和其他稳定提交,能够根据需要合并 A、B 和 Z 功能。

那么如何将“Z”提交移动到它们自己的分支上呢?

I have a repository where I had been working on master branch having last added some 10 or so commits which I now wish were on another branch, as they describe work that I now consider experimental (I am still learning good Git practices).

In light of this consideration, I would now like to have these last 10 commits form their own branch so to speak, so that I can have master clean and reserved for "release" / "stable" commits only.

To illustrate, what I have is:

        b--b (feature B)
       /       
X--X--X--Z--Z--Z--Z--Z--Z (master)
    \
     a--a--a (feature A)

You can see that commits marked with X and Z are all on the master branch, while what I want is commits marked with Z (the now considered experimental "feature Z" work) to lie on their own branch and master ending with the rightmost X. To illustrate, the desired graph:

        b--b (feature B)
       /       
X--X--X (master)
    \  \
     \  Z--Z--Z--Z--Z--Z (feature Z - the new branch I want)
      \
       a--a--a (feature A)

That way I will have my master reserved for releases and otherwise stable commits, being able to merge in A, B and Z features as needed.

So how do I move the "Z" commits onto their own branch?

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

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

发布评论

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

评论(4

美人骨 2024-10-16 10:16:52
git checkout master
git branch feature-Z
git reset <commit_id>

其中 commit_id 是 b 分支之前最后一次提交的标识符。

git checkout master
git branch feature-Z
git reset <commit_id>

where commit_id is an identifier of that last X commit before b branches off.

北笙凉宸 2024-10-16 10:16:52

为了完整起见,答案在这里 - http://git-scm.com/docs/git-reset - 搜索文本“撤消提交,使其成为主题分支” - 该示例显示将最后 3 个提交设为一个分支,并将 master 重置为这 3 个提交之前的提交:

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
nothing to commit (working directory clean)
$ git branch topic/wip
$ git reset --hard HEAD~3
$ git checkout topic/wip
Switched to branch topic/wip

For completeness, the answer is here - http://git-scm.com/docs/git-reset - search for the text "Undo a commit, making it a topic branch" - the example shows making the last 3 commits a branch and resetting master to the commit previous to those 3 commits:

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 3 commits.
#
nothing to commit (working directory clean)
$ git branch topic/wip
$ git reset --hard HEAD~3
$ git checkout topic/wip
Switched to branch topic/wip
七禾 2024-10-16 10:16:52

只需重命名 master 并在最后一个 X 处启动一个新的 master:

git checkout master; git 分支 -m 功能; git checkout -b master HEAD~6

Simply rename master and start a new master at the last X:

git checkout master; git branch -m feature; git checkout -b master HEAD~6

匿名。 2024-10-16 10:16:52

在所需的新分支中创建并签出
强制主分支 N 提交回来(在您的情况下为 10)

git checkout -b feature-z
git branch -f master HEAD~10

Create and checkout in desired new branch
Force master branch N commits back (in your case 10)

git checkout -b feature-z
git branch -f master HEAD~10
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文