Git:Stage into Commit,正确的工作流程是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2658753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:15:41  来源:igfitidea点击:

Git: Stage into Commit, what is the right workflow?

gitcommitsplitstaging

提问by ?ukasz Lew

I just created a big piece of code I want to commit in several separate commits.
So I can stage relevant parts, commit, stage, commit, ... and so on until I have all my changes commited.

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

The missing part is how can I test whether I split the commit correcty.
I.e. whether the part that is in staging area at least compiles?

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

To do that I must somehow bring my work tree to be in sync with index (staging area) without losing the changes to be committed later.

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

What is the rightway to do it?
What is the quickestway to do it?

什么是正确的做到这一点呢?
什么是最快的做到这一点呢?

Update:
How to do it with magit?

更新:
如何用 magit 做到这一点?

采纳答案by VonC

You could do it with:

你可以这样做:

$ git branch task1 # first set of commit to do

An intermediate branch can be useful to record some intermediate commits when you are slowly adding some content to the index.

当您缓慢地向索引添加一些内容时,中间分支可用于记录一些中间提交。

Then try an interactive session for addingjust what you want:

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

$ git add -i

Add any time you want to check what you have added:

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

$ git stash --keep-index

If it compiles, git commityour current work, and if task1is not yet complete, git stash popto restore the full working tree and repeat.

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

Once task1is fully baked, you can trim all those 'task1' commits, and merge the all work in master:

一旦task1完全出炉,可以修剪所有那些“ task1”提交,并合并在掌握所有的工作:

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

If you want to conserve the history of some significant task1commits, you can rebase first task1on top of master, before merging masterin task1(fast-forward)

如果你想节省一些显著的历史task1提交,可以先衍合task1上主的顶部,合并前mastertask1(快进)

Finally, if your stash still contains some work in progress, repeat the all process for task2.

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

回答by vaab

Here's a magit way I use:

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

  • make your index for the first commit with magit "u" or "s" to stage or unstage hunk/files/region. (This can be done with git gui also). Once your index is ready to commit:
  • commit with "c", write your commit message, Ctrl-C Ctrl-C to commit.
  • stash with "z" followed by << Enter>>, this will stash all your change. To get back your changes you can use "A" while your pointer is on the correct stash entry.
  • 使用 magit "u" 或 "s" 为第一次提交创建索引以暂存或取消暂存大块/文件/区域。(这也可以用 git gui 来完成)。一旦您的索引准备好提交:
  • 用“c”提交,写下你的提交信息,Ctrl-C Ctrl-C 提交。
  • 用“z”后跟 << Enter>> 存储,这将存储您的所有更改。要取回您的更改,您可以在指针位于正确的存储条目上时使用“A”。

make your tests to check if your commit is good. If any change must be done, redo as before, but press Ctrl-C Ctrl-A before commiting while in the screen of the commit message. This will amend (complete) your last commit instead of creating a new one.

进行测试以检查您的提交是否良好。如果必须进行任何更改,请像以前一样重做,但在提交消息屏幕中提交之前按 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.

请注意,如果您稍后认为某些代码应该修改在您最后一次完成之前的提交,您应该自己提交代码(带有临时摘要),并将它们折叠到正确的提交中,感谢“L " 访问日志屏幕,然后在您想要压扁的两个提交之前指向您的指针,然后按“E”启动“git rebase -i”会话。重新排序提交,以便临时摘要是“修复”目标提交。退出缓冲区和 TADA。一切都完成了。