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

发布于 2024-10-15 04:56:45 字数 258 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷情 2024-10-22 04:56:45

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

git commit -a

或者

git commit -am "commit message"

来自 Pro Git 书籍:

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

The following command should do the trick:

git commit -a

or

git commit -am "commit message"

From the Pro Git book:

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

残疾 2024-10-22 04:56:45

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)

冷血 2024-10-22 04:56:45

您可以使用:

git add -u

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

git-add 手册页:

-u
--更新

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

但不确定何时添加此功能。

You could use:

git add -u

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

From git-add man page:

-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.

Not sure when this feature has been added though.

污味仙女 2024-10-22 04:56:45

我可能丢失了一些东西,但 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"

并且 git log 显示三个提交。

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

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.

因为看清所以看轻 2024-10-22 04:56:45

我会小心使用 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

娇俏 2024-10-22 04:56:45

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文