将先前的提交分解为多个提交 [英] Break a previous commit into multiple commits

查看:27
本文介绍了将先前的提交分解为多个提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不创建分支并在新分支上做一堆时髦的工作,是否有可能在将单个提交提交到本地存储库后将其分解为几个不同的提交?

Without creating a branch and doing a bunch of funky work on a new branch, is it possible to break a single commit into a few different commits after it's been committed to the local repository?

推荐答案

git rebase -i 就可以了.

首先,从一个干净的工作目录开始:git status 应该显示没有待处理的修改、删除或添加.

First, start with a clean working directory: git status should show no pending modifications, deletions, or additions.

现在,您必须决定要拆分哪些提交.

Now, you have to decide which commit(s) you want to split.

要拆分您最近的提交,首先:

To split apart your most recent commit, first:

$ git reset HEAD~

现在以通常的方式单独提交各个部分,根据需要生成尽可能多的提交.

Now commit the pieces individually in the usual way, producing as many commits as you need.

这需要rebase,即重写历史.要指定正确的提交,您有多种选择:

This requires rebasing, that is, rewriting history. To specify the correct commit, you have several choices:

  • 如果是三回提交,那么

  • If it is three commits back, then

  $ git rebase -i HEAD~3

其中 3 是返回的提交次数.

where 3 is how many commits back it is.

如果它在树中比你想数的更远,那么

If it is farther back in the tree than you want to count, then

  $ git rebase -i 123abcd~

其中 123abcd 是您要拆分的提交的 SHA1.

where 123abcd is the SHA1 of the commit you want to split up.

如果您在要合并到 master 的不同分支(例如,功能分支)上:

If you are on a different branch (e.g., a feature branch) that you want to merge into master:

  $ git rebase -i master

当您看到 rebase 编辑屏幕时,找到您想要拆分的提交.在该行的开头,将 pick 替换为 edit(简称 e).保存缓冲区并退出.Rebase 现在将在您要编辑的提交之后停止.然后:

When you get the rebase edit screen, find the commit you want to break apart. At the beginning of that line, replace pick with edit (e for short). Save the buffer and exit. Rebase will now stop just after the commit you want to edit. Then:

$ git reset HEAD~

以通常的方式单独提交各个部分,根据需要生成尽可能多的提交.

Commit the pieces individually in the usual way, producing as many commits as you need.

$ git rebase --continue

这篇关于将先前的提交分解为多个提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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