如何使用 Git 一次提交一个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2003491/
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 07:36:44  来源:igfitidea点击:

How to commit one file at a time using Git?

git

提问by Scud

Say there are several files modified and I just want one files committed each time. How to do that? Given an example with status looks like below. Thanks!!

假设有几个文件被修改,我只想每次提交一个文件。怎么做?给出一个状态示例,如下所示。谢谢!!

Ex:

前任:

> git status
#   modified: file01.txt
#   modified: file02.txt
#   modified: file03.txt
#   modified: file04.txt
#   modified: file05.txt

采纳答案by Alex Reisner

There are a few ways, but the simplest is probably:

有几种方法,但最简单的可能是:

git commit file01.txt
git commit file02.txt
...

Any paths you list after the commit command will be committed regardless of whether they're staged to be committed. Alternatively, you can stage each one, and then commit:

您在 commit 命令之后列出的任何路径都将被提交,而不管它们是否准备提交。或者,您可以暂存每个,然后提交:

git add file01.txt
git commit

git add file02.txt
git commit

...

回答by daf

Use git add -pfollowed by git commit. git add -pasks you for each change whether to stage it for committing, which is super convenient.

使用git add -p后跟git commit. git add -p每次更改都会询问您是否将其暂存以进行提交,这非常方便。

You can also use -pwith git resetand git checkout.

您也可以-pgit reset和 一起使用git checkout

回答by phils

git add -pdescribed by daf's answer is nice, but for a more direct approach to picking and choosing, I'm really liking:

git add -pdaf 的回答所描述的很好,但对于更直接的挑选和选择方法,我真的很喜欢:

git add -e

git add -e

It generates the appropriate patch, then loads it up in your preferred editor so that you can edit it as desired. When you save the file and exit the editor, and only the changes made by your edited version of the patch are added to the index.

它会生成适当的补丁,然后将其加载到您喜欢的编辑器中,以便您可以根据需要进行编辑。当您保存文件并退出编辑器时,只有您编辑过的补丁版本所做的更改才会添加到索引中。

If you accidentally close the editor without making changes, you'll probably want to use get reset HEADand then start over.

如果您不小心关闭了编辑器而没有进行更改,您可能想要使用get reset HEAD然后重新开始。

回答by Charles Ma

git add file01.txt
git stash
git commit -m'commit message'
git stash pop

repeat.

重复。

回答by phils

On the subject of staging partial changes within a file, it's worth noting that various text editors have support for doing this in a nicer way than any of the command-line options.

关于在文件中暂存部分更改的主题,值得注意的是,各种文本编辑器都支持以比任何命令行选项更好的方式执行此操作。

I use Magitfor Emacs, which uses single keystrokes for stepping forward or backwards through the hunks, increasing or decreasing the granularity, and staging or un-staging the current hunk. You can also mark a region manually and stage (or un-stage) just that region.

我使用Magitfor Emacs,它使用单次击键来向前或向后步进,增加或减少粒度,以及暂存或取消暂存当前的大块。您还可以手动标记一个区域,然后仅暂存(或取消暂存)该区域。

I believe there are similar plugins for vim, and other editors / IDEs as well.

我相信 vim 和其他编辑器/IDE 也有类似的插件。