299 字
1 分钟
Git如何在上传多个文件时排除少部分文件
2025-08-21

场景#

修改了多个文件,但是这些文件中有一个文件不想提交,单个添加太麻烦

解决#

先全部添加

Terminal window
git add .

然后查看暂存区,记下想排除的文件名

Terminal window
git status

从暂存区移除不想提交的文件

Terminal window
git reset HEAD <不想要的文件>

验证暂存区,看不想要的文件是否移除

Terminal window
git status

提交并推送

Terminal window
git commit -m "提交说明"
git push

实际操作#

Terminal window
git add .
git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: mx-space部署记录.md
new file: test1.md
new file: test2.md
new file: test3.md
git reset HEAD mx-space部署记录.md
Unstaged changes after reset:
M mx-space部署记录.md
git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: test1.md
new file: test2.md
new file: test3.md
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: mx-space部署记录.md
git push

如果移除错了可以通过git add <文件名>再将单独的文件添加进来

如果想全部移除,可以直接使用git reset指令

Git如何在上传多个文件时排除少部分文件
https://fuwari.cbba.top/posts/git如何在上传多个文件时排除少部分文件/
作者
Chen_Feng
发布于
2025-08-21
许可协议
CC BY-NC-SA 4.0