使用 git 将多个更改分解为单独的提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4948494/
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
Break up multiple changes into separate commits with git?
提问by markdorison
If I have made multiple sets of changes to a large file, is it possible to split those into separate commits using git?
如果我对一个大文件进行了多组更改,是否可以使用 git 将它们拆分为单独的提交?
回答by William Pursell
You want git add --patch
(documentation), which will allow you to select which changes to stage.
您想要git add --patch
(文档),这将允许您选择要进行哪些更改。
回答by Andrew Aylett
Yes, you can -- use git add -i
to select which hunks you want to stage for each commit. You can get documentation by running git help add
and scrolling to "Interactive Mode".
是的,您可以 -- 用于git add -i
选择要为每次提交暂存的帅哥。您可以通过运行git help add
并滚动到“交互模式”来获取文档。
回答by AvL
Williams answeris perfectly valid. But sometimes it is easier to do things by hand. For example if you accidentally updated some third-party library with a lot of files before committing the changes you previously made. With git add -p
(same as --patch
) you would need to walk through all of this files. So in this case it is much more convenient to just stage the file you want to commit and do a second commit with all of the other changes:
威廉姆斯的回答是完全有效的。但有时手工做事情更容易。例如,如果您在提交之前所做的更改之前不小心用大量文件更新了某些第三方库。使用git add -p
(same as --patch
) 您需要遍历所有这些文件。因此,在这种情况下,仅暂存要提交的文件并使用所有其他更改进行第二次提交会方便得多:
> git add /path/to/your/file.txt
> git commit -m "my commit message"
[master a0c5ea6] my commit message
1 file changed, 2 insertions(+), 1 deletion(-)
> git add --all
> git commit -m "updated library xyz"
回答by theVoogie
One more option which I constantly use is just because i forget about the patch
option :):
我经常使用的另一个选项只是因为我忘记了该patch
选项:):
Say you have updated files: aaa.txt
, bbb.txt
, ccc.txt
and you want to push
the aaa.txt
file in the first commit and then bbb.txt
and ccc.txt
in the second commit:
假设你有更新的文件:aaa.txt
,bbb.txt
,ccc.txt
和你想push
的aaa.txt
在第一次提交,然后文件bbb.txt
,并ccc.txt
在第二承诺:
step 1:
--------------------
git add aaa.txt
git commit -m "Added first file"
git stash
git push
step 2:
--------------------
git stash pop
git add -A
git commit -m "Added the rest of the modified data"
git push
Might be a little more verbose, but in my opinion I it gives a bit more control.
可能有点冗长,但在我看来,它提供了更多的控制权。
Cheers!
干杯!