问题描述
我搞砸了我的历史记录,想对其进行一些更改.问题是,我有一个包含两个不相关更改的提交,并且此提交被我本地(非推送)历史记录中的其他一些更改所包围.
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?
推荐答案
有一个拆分提交的指南 在 rebase 联机帮助页中.快速总结是:
There is a guide to splitting commits in the rebase manpage. The quick summary is:
执行包含目标提交的交互式 rebase(例如
git rebase -i ^ branch
)并将其标记为要编辑.
Perform an interactive rebase including the target commit (e.g.
git rebase -i <commit-to-split>^ branch
) and mark it to be edited.
当 rebase 到达那个提交时,使用 git reset HEAD^
重置到提交之前,但保持你的工作树完整.
When the rebase reaches that commit, use git reset HEAD^
to reset to before the commit, but keep your work tree intact.
逐步添加更改并提交,根据需要进行尽可能多的提交.add -p
可用于仅添加给定文件中的某些更改.使用 commit -c ORIG_HEAD
如果您想为某个特定项目重复使用原始提交消息提交.
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.
如果您想测试您正在提交的内容(好主意!),请使用 git stash
隐藏您尚未提交的部分(或 stash --keep-index
在你甚至提交之前),测试,然后 git stash pop
将其余的返回到工作树.继续提交,直到提交所有修改,即拥有一个干净的工作树.
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.
运行 git rebase --continue
以在 now-split 提交后继续应用提交.
Run git rebase --continue
to proceed applying the commits after the now-split commit.
这篇关于如何拆分埋藏在历史中的 Git 提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!