我怎样才能分裂一个埋在历史中的Git提交? [英] How can I split up a Git commit buried in history?

查看:98
本文介绍了我怎样才能分裂一个埋在历史中的Git提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浪费了我的历史,并想对它做一些改变。问题是,我有一个提交了两个不相关的更改,并且此提交被本地(非推送)历史记录中的其他更改所包围。



我想分割在我推出之前先完成这个提交,但是我看到的大多数指南都与拆分最近的提交或未提交的本地更改有关。

解决方案

div>

有一个指导可以在rebase手册页中分裂提交 。简要总结如下:


  • 执行包括目标提交(例如 git rebase -i < / p>


  • 当rebase达到该提交时,使用 git reset HEAD ^ 在提交之前重置,但保持工作树完好。

  • 逐步添加更改并提交它们,根据需要进行尽可能多的提交。 add -p 可以用于添加给定文件中的一些更改。如果您想重新使用该功能,请使用 commit -c ORIG_HEAD 原始提交信息。
    隐藏你没有提交的部分(或者甚至在提交之前隐藏 stash --keep-index ),测试,然后 git stash pop 将其余部分返回到工作树。继续进行提交,直到你完成所有修改,即有一个干净的工作树。 运行 git rebase --continue 继续在现在分割的提交之后应用提交。

I flubbed up my history and want to do some changes to it. Problem is, I have a commit with two unrelated changes, and this commit is surrounded by some other changes in my local (non-pushed) history.

I want to split up this commit before I push it out, but most of the guides I'm seeing have to do with splitting up your most recent commit, or uncommitted local changes. Is it feasible to do this to a commit that is buried in history a bit, without having to "re-do" my commits since then?

解决方案

There is a guide to splitting commits in the rebase manpage. The quick summary is:

  • Perform an interactive rebase including the target commit (e.g. git rebase -i <commit-to-split>^ branch) and mark it to be edited.

  • When the rebase reaches that commit, use git reset HEAD^ to reset to before the commit, but keep your work tree intact.

  • Incrementally add changes and commit them, making as many commits as desired. add -p can be useful to add only some of the changes in a given file. Use commit -c ORIG_HEAD if you want to re-use the original commit message for a certain commit.

  • If you want to test what you're committing (good idea!) use git stash to hide away the part you haven't committed (or stash --keep-index before you even commit it), test, then git stash pop to return the rest to the work tree. Keep making commits until you get all modifications committed, i.e. have a clean work tree.

  • Run git rebase --continue to proceed applying the commits after the now-split commit.

这篇关于我怎样才能分裂一个埋在历史中的Git提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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