简体   繁体   English

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

[英]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

(根据Charles Bailey对相关问题的回答

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 --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. 换句话说,更新当前目录及其子目录中的所有跟踪文件。

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.

相关问题 Mercurial仅提交修改和删除的文件 - Mercurial commit only modified and deleted files Git:仅提交指定的文件(重命名、删除、修改、新文件等) - Git: Commit Only Specified Files (Renamed, Deleted, Modified, New File, etc) git 仅将修改的文件作为原子提交提交,然后仅将已删除的文件作为单独的原子提交提交 - git commit only modified files as atomic commit, then commit only deleted files as separate atomic commit 如何只提交一些新文件而忽略修改过的文件? - How to commit only some new files and ignore the modified ones? GIT 新/修改/删除文件列表 - GIT list of new/modified/deleted files 知道在提交中创建,修改或删除了哪些文件 - Know which files were created, modified or deleted in a commit 如何从任何地方添加* all * new / modified文件到git? (比如`git add -A`,或者`git commit -a`用于新文件) - How to add *all* new/modified files to git, from anywhere? (like `git add -A`, or `git commit -a` for new files) 如何提交暂存文件并排除修改的文件 - How to commit the staged files and excluding modified files Git如何只考虑新文件或修改过的文件 - Git how to take in consideration only new or modified files 如何只部署修改/新文件GIT + Jenkins + PHP? - How to deploy only modified/new files GIT+Jenkins+PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM