git 如何仅提交修改过的(而不是新的或删除的)文件?

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

How to commit only modified (and not new or deleted) files?

gitversion-control

提问by Pedro d'Aquino

git statusshows a bunch of files which were modified and some which were deleted. I want to first commit the modified files and then the deleted ones. I don't see any option in git addthat enables me to do this. How can I do it?

git status显示了一堆被修改的文件和一些被删除的文件。我想先提交修改过的文件,然后再提交删除的文件。我没有看到任何git add使我能够做到这一点的选项。我该怎么做?

EDIT: As pointed out, git addwouldn't have staged the deleted files anyway, so git add .would do. But it has the side-effect of including files which weren't tracked, which I would also like to avoid. I have changed the title of the question accordingly.

编辑:正如所指出的,git add无论如何都不会暂存已删除的文件,所以git add .会这样做。但它具有包含未跟踪的文件的副作用,我也想避免这种情况。我相应地更改了问题的标题。

回答by tokarev

The following command should do the trick:

以下命令应该可以解决问题:

git commit -a

or

或者

git commit -am "commit message"

From the Pro Gitbook:

Pro Git书中:

Providing the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit

为 git commit 命令提供 -a 选项可使 Git 在执行提交之前自动暂存已跟踪的每个文件

回答by Pedro d'Aquino

git diff --name-only --diff-filter=M | xargs git add

git diff --name-only --diff-filter=M | xargs git add

(based on Charles Bailey's answer on a related question)

(基于Charles Bailey 对相关问题的回答

回答by jln_wlflsbrg

You could use:

你可以使用:

git add -u

to stage already tracked files that have been modified since last commit.

暂存自上次提交以来已被修改的已跟踪文件。

From git-addman page:

git-add手册页:

-u
--update

Only match against already tracked files in the index rather than the working tree. That means that it will never stage new files, but that it will stage modified new contents of tracked files and that it will remove files from the index if the corresponding files in the working tree have been removed. If no is given, default to "."; in other words, update all tracked files in the current directory and its subdirectories.

-u
--update

只匹配索引中已经跟踪的文件而不是工作树。这意味着它永远不会暂存新文件,但它会暂存跟踪文件的修改后的新内容,并且如果工作树中的相应文件已被删除,它将从索引中删除文件。如果没有给出,默认为“.”;换句话说,更新当前目录及其子目录中的所有跟踪文件。

Not sure when this feature has been added though.

不确定何时添加此功能。

回答by Makis

I may be missing something but git add doesn't include deleted files, you have to use git rm to remove those:

我可能遗漏了一些东西,但 git add 不包含已删除的文件,您必须使用 git rm 来删除那些:

mkdir git-test
cd git-test
git init
touch a
touch b
touch c
git add .
git commit -m "Initial"
echo "a" > a
echo "b" > b
rm c
git status

# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       modified:   a
#       modified:   b
#       deleted:    c
#

git add .

# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   a
#       modified:   b
#
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    c
#

git commit -m "Changed"
git status

# On branch master
# Changed but not updated:
#   (use "git add/rm <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#       deleted:    c
#

git rm c
git commit -m "Deleted"

And git log shows three commits.

并且 git log 显示三个提交。

回答by serup

I would be careful with using git commit -a -- unless you have made sure that your .gitignore file has ALL files that you do NOT want added there

我会小心使用 git commit -a -- 除非您确保您的 .gitignore 文件包含您不想添加的所有文件

I have used git commit -a many times in areas where this was not the case, and I ended up having to clean up / delete temporary files etc. from git repository

我已经多次使用 git commit -a 在不是这种情况的区域,我最终不得不从 git 存储库中清理/删除临时文件等