如何使用 Sourcetree 拆分过去未推送的提交? [英] How can I split a past un-pushed commit with Sourcetree?

查看:37
本文介绍了如何使用 Sourcetree 拆分过去未推送的提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sourcetree 的交互式 rebase 功能很棒,但我经常发现自己希望能够将一个提交拆分"为多个较小的提交.我知道有一种方法可以从命令行执行此操作,但我似乎无法在 UI 中找到任何位置来处理此问题.

有没有办法用 Sourcetree 做到这一点?或者这是我必须下拉到命令行才能实现目标的地方之一?

解决方案

git help rebase(搜索SPLITTING COMMITS").它不是 git 命令或选项;它更像是一种提交编辑的方式.您可以使用 SourceTree for Mac 执行那里描述的步骤.根据您的问题,我了解到您之前使用 SourceTree 进行了交互式 rebase,并且熟悉它的 UI.


更新:

看来我的原始答案(见上文)太神秘了.没有用于拆分提交的特殊 git 命令,因此 SourceTree 没有在界面中提供执行此操作的命令或选项.您必须阅读并按照 文档 中解释的步骤使用 GUI 而不是命令行.

这些是步骤(引自文档)以及如何使用 Atlassian SourceTree:

<块引用>

  1. 使用 git rebase -i ^ 启动交互式 rebase,其中 是您要拆分的提交.事实上,任何提交范围都可以,只要它包含该提交即可.

右键单击要编辑的提交的父提交.从出现的上下文菜单中选择以交互方式重新设置 的子项..."( 是单击提交的哈希值的缩写).

<块引用>

  1. 使用编辑"操作标记要拆分的提交.

SourceTree 打开Interactive Rebase"对话框.在列表中确定您要拆分的提交(它是最后一个)并选中列修改提交?" 中的复选框.按确定"按钮继续.

<块引用>

  1. 在编辑该提交时,执行 git reset HEAD^.效果是HEAD倒回1,索引也跟着.但是,工作树保持不变.

SourceTree 在后台运行所需的 Git 命令,并在编辑您标记为修改的提交时停止.右键单击当前 HEAD 的父级,然后从出现的上下文菜单( 是您在开始流程时签出的分支的名称).它会询问使用什么模式进行复位;在使用模式"中选择混合 - 保留工作副本但重置索引";列出并按确定".

<块引用>

  1. 现在将更改添加到您希望在第一次提交中拥有的索引.您可以使用 git add(可能以交互方式)或 git gui(或两者)来做到这一点.

现在是分割时间! 将要放入第一次提交的文件和大块文件添加到索引中;如果需要,您甚至可以进行原始提交中不存在的更改.但是,请注意,您正在编辑的提交中未包含的更改可能会与稍后提交中引入的更改发生冲突.

<块引用>

  1. 使用现在合适的任何提交消息提交当前索引.

像往常一样使用 SourceTree 并提交分阶段的更改.

<块引用>

  1. 重复最后两个步骤,直到您的工作树干净为止.

(没有评论)

<块引用>

  1. 使用 git rebase --continue 继续 rebase.

操作"菜单中选择Continue Rebase".

恭喜!您只需使用 SourceTree 提供的隐藏 split commit" 命令拆分提交:p

备注

如果出现问题或您下定决心或出于任何原因需要取消整个过程,请使用操作"中的Abort Rebase"强>菜单.在后台 SourceTree 将调用 git rebase --abort 并且一切都会回到你开始之前的状态.

如果您需要在同一分支上拆分/编辑多个提交,您可以使用第 1 步中最旧的提交,并标记修改第 2 步中需要的所有提交.其他一切都相同,SourceTree 将对您标记为修改的每个提交重复步骤 3-7.

这是使用 Atlassian SourceTree 拆分提交的方法.可以改用任何其他带有 GUI 的 Git 客户端,该客户端提供了一种交互式变基方法.

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. 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.

Is there a way to do this with 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"). It is not a git command or option; it is more a way of commits editing. You can do the steps described there using SourceTree for Mac. From your question I understand you did interactive rebases using SourceTree before and are familiar with its 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. You have to read and follow the steps explained in the documentation using the GUI instead of the command line.

These are the steps (quoted from the documentation) and how to follow them using Atlassian SourceTree:

  1. Start an interactive rebase with git rebase -i <commit>^, where <commit> is the commit you want to split. 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).

  1. Mark the commit you want to split with the action "edit".

SourceTree opens the "Interactive Rebase" dialog box. 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^. The effect is that the HEAD is rewound by one, and the index follows suit. 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. 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). 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.

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.

Use SourceTree as usual and commit the staged changes.

  1. Repeat the last two steps until your working tree is clean.

(no comments)

  1. Continue the rebase with git rebase --continue.

Pick the "Continue Rebase" from the "Actions" menu.

Congratulations! You just split a commit using the hidden "split commit" command provided by SourceTree :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.

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.

This is how you can split a commit using Atlassian SourceTree. Any other Git client with GUI that provides a way to do interactive rebase can be used instead.

这篇关于如何使用 Sourcetree 拆分过去未推送的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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