如何仅提交修改过的(而不是新的或删除的)文件?
git status
显示一堆已修改的文件和一些已删除的文件。我想先提交修改的文件,然后提交删除的文件。我在 git add 中没有看到任何可以让我执行此操作的选项。我该怎么做呢?
编辑:正如所指出的, git add
无论如何都不会暂存已删除的文件,因此 git add .
就可以了。但它有一个副作用,即包含未跟踪的文件,我也想避免这种情况。我相应地更改了问题的标题。
git status
shows 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 add
that enables me to do this. How can I do it?
EDIT: As pointed out, git add
wouldn'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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
以下命令应该可以解决问题:
或者
来自 Pro Git 书籍:
The following command should do the trick:
or
From the Pro Git book:
git diff --name-only --diff-filter=M | 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)
您可以使用:
暂存自上次提交以来已修改的已跟踪文件。
从 git-add 手册页:
但不确定何时添加此功能。
You could use:
to stage already tracked files that have been modified since last commit.
From git-add man page:
Not sure when this feature has been added though.
我可能丢失了一些东西,但 git add 不包括已删除的文件,您必须使用 git rm 删除这些文件:
并且 git log 显示三个提交。
I may be missing something but git add doesn't include deleted files, you have to use git rm to remove those:
And git log shows three commits.
我会小心使用 git commit -a ——除非你确保你的 .gitignore 文件包含你不想添加的所有文件,
我已经在不是这种情况的区域多次使用了 git commit -a ,我最终不得不从 git 存储库中清理/删除临时文件等
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
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 状态
在分支主控上
您的分支已更新为“origin/master”。
更改未暂存以在此处提交:
(使用“git add ..
在此处输入
.”来更新将提交的内容)(使用“git Restore …”放弃工作目录中的更改)
修改: src/components/Header.js
没有添加任何更改到提交(使用“git add”和/或“git commit -a”)
如果我们尝试添加特定的修改文件,那么使用 git add "Modified file" 将抛出错误
$ git add Header.js
致命:路径规范“Header.js”与任何文件都不匹配
所以我使用了它并推送
git add -u // 更新修改后的文件。
git commit -m“Header.js”。
git push
这对我来说效果很好。
git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for enter here commit:
(use "git add ..
enter here
." to update what will be committed)(use "git restore …" to discard changes in working directory)
modified: src/components/Header.js
no changes added to commit (use "git add" and/or "git commit -a")
If we try to add particular modified file only then using git add "Modified file" will throw an error
$ git add Header.js
fatal: path spec 'Header.js' did not match any files
So I used this and pushed
Git add -u // To update the modified file.
git commit -m "Header.js".
git push
This worked fine for me.