21

我刚刚创建了一大段代码,我想在几个单独的提交中提交。
所以我可以暂存相关部分,提交、暂存、提交……等等,直到我提交所有更改。

缺少的部分是如何测试我是否正确拆分了提交。
即至少在暂存区的部分是否编译?

为此,我必须以某种方式使我的工作树与索引(暂存区)同步,而不会丢失稍后要提交的更改。

正确的方法是什么?最快
的方法是什么?

更新:
如何用 magit 做呢?

4

2 回答 2

16

你可以这样做:

$ git branch task1 # first set of commit to do

当您缓慢地将一些内容添加到索引中时,中间分支对于记录一些中间提交很有用。

然后尝试一个交互式会话来添加您想要的内容:

$ git add -i

随时添加您想检查已添加内容的时间:

$ git stash --keep-index

如果它编译,git commit你当前的工作,如果task1还没有完成,git stash pop恢复完整的工作树并重复。

task1完全烘焙后,您可以修剪所有那些 ' task1' 提交,并将所有工作合并到 master 中:

$ git checkout master
$ git merge task1
$ git branch -D task1 # no need for that intermediate branch

如果你想保存一些重要提交的历史,你可以先在master 之上task1rebase ,然后再合并(快进)task1mastertask1

最后,如果您的 stash 仍然包含一些正在进行的工作,请重复所有过程task2

于 2010-04-17T15:22:50.393 回答
0

这是我使用的一种 magit 方式:

  • 使用 magit "u" 或 "s" 为第一次提交创建索引以暂存或取消暂存块/文件/区域。(这也可以用 git gui 来完成)。一旦您的索引准备好提交:
  • 用“c”提交,写下你的提交信息,Ctrl-C Ctrl-C 提交。
  • stash 用“z”后跟 << Enter>>,这将隐藏你的所有更改。要取回您的更改,您可以在指针位于正确的存储条目上时使用“A”。

进行测试以检查您的提交是否良好。如果必须进行任何更改,请像以前一样重做,但在提交消息的屏幕上提交之前按 Ctrl-C Ctrl-A。这将修改(完成)您的最后一次提交,而不是创建一个新的提交。

Note that if you figure later that some code should come amend a commit that is before the last you've done, you should commit the code on it's own (with a temporary summary), and fold them into the right commit thanks to "L" to access log screen, then point your pointer before both commit you'd like to squish, and press "E" to launch a "git rebase -i" session. Re-order the commits so that the temporary summary is to "fix" the target commit. Quit the buffer, and TADA. All is done.

于 2011-10-07T13:18:02.893 回答