[英]How to commit only modified (and not new or deleted) files?
git status
shows a bunch of files which were modified and some which were deleted. git status
显示了一堆被修改的文件和一些被删除的文件。 I want to first commit the modified files and then the deleted ones. 我想首先提交修改后的文件,然后提交已删除的文件。 I don't see any option in git add
that enables me to do this. 我没有在git add
中看到任何可以让我这样做的选项。 How can I do it? 我该怎么做?
EDIT : As pointed out, git add
wouldn't have staged the deleted files anyway, so git add .
编辑 :正如所指出的, git add
无论如何也不会上传已删除的文件,所以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. 我已相应更改了问题的标题。
The following command should do the trick: 以下命令应该可以解决问题:
git commit -a
or 要么
git commit -am "commit message"
From the Pro Git book: 从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在执行提交之前自动暂存已跟踪的每个文件
git diff --name-only --diff-filter=M | xargs git add
You could use: 你可以使用:
git add -u
to stage already tracked files that have been modified since last commit. 暂存自上次提交后已修改的已跟踪文件。
From git-add man page: 从git-add手册页:
-u -u
--update --updateOnly 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. 换句话说,更新当前目录及其子目录中的所有跟踪文件。
Not sure when this feature has been added though. 不确定何时添加了此功能。
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显示了三个提交。
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存储库中清理/删除临时文件等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.