简体   繁体   English

如何使用Sourcetree拆分过去未推送的提交?

[英]How can I split a past un-pushed commit with Sourcetree?

The interactive rebase capabilities of Sourcetree are great, but I often find myself wanting to be able to "split" a commit into multiple smaller commits. Sourcetree的交互式rebase功能很棒,但我经常发现自己希望能够将提交“拆分”为多个较小的提交。 I know theres a way to do this from the command line, but I can't seem to find any place in the UI to handle this. 我知道有一种方法可以从命令行执行此操作,但我似乎无法找到UI中的任何位置来处理此问题。

Is there a way to do this with Sourcetree? 有没有办法用Sourcetree做到这一点? Or is this one of those places where I have to drop down to the command line to accomplish my goals? 或者这是我必须下载到命令行以实现目标的地方之一?

Splitting the commits from the command line using interactive rebase is explained in git help rebase (search for "SPLITTING COMMITS"). git help rebase (搜索“SPLITTING COMMITS”)中解释了使用交互式rebase从命令行拆分提交。 It is not a git command or option; 它不是git命令或选项; it is more a way of commits edit ing. 它更像是一种提交编辑方式。 You can do the steps described there using SourceTree for Mac. 您可以使用SourceTree for Mac执行此处描述的步骤。 From your question I understand you did interactive rebases using SourceTree before and are familiar with its UI. 根据您的问题,我了解您之前使用SourceTree进行了交互式rebase并熟悉其UI。


Update: 更新:

It seems that my original answer (see above) is too cryptic. 我的原始答案(见上文)似乎太神秘了。 There is no special git command for splitting commits, and consequently SourceTree does not provide a command or an option in the interface to do that. 没有用于拆分提交的特殊git命令,因此SourceTree不会在接口中提供命令或选项来执行此操作。 You have to read and follow the steps explained in the documentation using the GUI instead of the command line. 您必须使用GUI而不是命令行阅读并遵循文档中说明的步骤。

These are the steps (quoted from the documentation) and how to follow them using Atlassian SourceTree : 这些是步骤(引自文档)以及如何使用Atlassian SourceTree来遵循它们:

  1. Start an interactive rebase with git rebase -i <commit>^ , where <commit> is the commit you want to split. 使用git rebase -i <commit>^启动交互式rebase,其中<commit>是要分割的提交。 In fact, any commit range will do, as long as it contains that commit. 实际上,只要包含该提交,任何提交范围都可以。

Right-click on the parent commit of the commit you want to edit. 右键单击要编辑的提交的父提交。 Select "Rebase children of <hash> interactively ..." from the contextual menu that appears ( <hash> is the abbreviation of the clicked commit's hash). 从出现的上下文菜单中选择“以交互方式重新生成<hash> ...”<hash>是单击的提交的哈希的缩写)。

  1. Mark the commit you want to split with the action "edit". 使用“编辑”操作标记要拆分的提交。

SourceTree opens the "Interactive Rebase" dialog box. SourceTree打开“Interactive Rebase”对话框。 Identify in the list the commit you want to split (it's the last one) and check its checkbox in the column "Amend Commit?" 在列表中标识要拆分的提交(它是最后一个)并在“修改提交”列中选中它的复选框。 . Press the " OK" button to continue. 按“ 确定”按钮继续。

  1. When it comes to editing that commit, execute git reset HEAD^ . 在编辑提交时,执行git reset HEAD^ The effect is that the HEAD is rewound by one, and the index follows suit. 结果是HEAD被一个重绕,索引也随之而来。 However, the working tree stays the same. 但是,工作树保持不变。

SourceTree runs in the background the needed Git command(s) and stops when it comes to edit the commit you marked for amending. SourceTree在后台运行所需的Git命令,并在编辑您标记为要修改的提交时停止。 Right click the parent of the current HEAD and select "Reset (no branch, rebasing <branch> ) to this commit" from the contextual menu that appears ( <branch> is the name of the branch you had checked out when you started the process). 右键单击当前HEAD的父级,然后从出现的上下文菜单中选择“重置(没有分支,重新定位<branch> )到此提交”<branch>是您在启动过程时检出的分支的名称)。 It will ask what mode to use for reset; 它将询问用于重置的模式; select "Mixed - keep working copy but reset index" in the "Using mode" list and press "OK" . 在“使用模式”列表中选择“混合 - 保持工作副本但重置索引” ,然后按“确定”

  1. Now add the changes to the index that you want to have in the first commit. 现在将更改添加到您希望在第一次提交中拥有的索引。 You can use git add (possibly interactively) or git gui (or both) to do that. 您可以使用git add (可能是交互式)或git gui (或两者)来做到这一点。

It's split time! 这是分时间! Add to the index the files and the hunks you want to put into the first commit; 将要放入第一次提交的文件和数据添加到索引中; you can even do changes that were not present in the original commit if you need. 如果需要,您甚至可以执行原始提交中不存在的更改。 However, be aware that changes that were not in the commit you are editing may conflict with changes introduced by the later commits. 但是,请注意,您正在编辑的提交中没有的更改可能与后面提交引入的更改冲突。

  1. Commit the now-current index with whatever commit message is appropriate now. 使用现在适当的提交消息提交now-current索引。

Use SourceTree as usual and commit the staged changes. 像往常一样使用SourceTree并提交分阶段更改。

  1. Repeat the last two steps until your working tree is clean. 重复最后两步,直到工作树干净。

(no comments) (没意见)

  1. Continue the rebase with git rebase --continue . git rebase --continue继续rebase - 继续。

Pick the "Continue Rebase" from the "Actions" menu. “操作”菜单中选择“继续重新启动

Congratulations! 恭喜! You just split a commit using the hidden "split commit" command provided by SourceTree :p 您只需使用SourceTree提供的隐藏“split commit”命令拆分提交 :p

Remarks 备注

If something goes wrong or you make up your mind or for whatever reason you need to cancel the entire process then use the "Abort Rebase" from the "Actions" menu. 如果出现问题或您下定决心或因任何原因需要取消整个过程,请使用“操作”菜单中的“中止重新生成 In the background SourceTree will invoke git rebase --abort and everything well return to the state it was before you started. 在后台SourceTree将调用git rebase --abort ,一切都恢复到你开始之前的状态。

If you need to split/edit more than one commit on the same branch you can use the oldest commit on step 1 and mark for amending all the commits you need on step 2. Everything else is the same, SourceTree will repeat the steps 3-7 for each commit you marked for amending. 如果您需要在同一分支上拆分/编辑多个提交,您可以使用步骤1中最旧的提交并标记修改步骤2中所需的所有提交。其他一切都相同, SourceTree将重复步骤3每个提交您标记为修改的提交。

This is how you can split a commit using Atlassian SourceTree . 这是您使用Atlassian SourceTree分割提交的方法。 Any other Git client with GUI that provides a way to do interactive rebase can be used instead. 可以使用任何其他具有GUI的Git客户端来提供交互式rebase的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM