Git 提交修改和 Git 提交新文件之间的区别

发布于 2024-10-25 17:51:34 字数 298 浏览 0 评论 0 原文

我是 这里很困惑...

如何对修改的文件进行git提交以及如何对修改后的文件进行git提交 新文件

另外,如何在 git 中单独提交文件?

I am confused here...

How do you do a git commit for a modified file and how do you do a git commit for new file?

Also, how do you individually commit files in git?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

山有枢 2024-11-01 17:51:34

git 提交过程分为两个部分。首先,“暂存”单个文件,然后执行“提交”命令,该命令实际上将“暂存”文件添加到存储库中。因此,如果您想“暂存”文件,您首先将使用“git add”命令:

git add myfile1

您可以对任意数量的文件执行此操作。当您调用“git status”时,您应该看到暂存文件的列表。您可以“添加”存储库中已存在但已更改的文件,也可以“添加”存储库中尚未存在的文件。新文件将在暂存区域中作为新文件出现,而其他文件将按修改后的状态出现,但是当您最终调用“git commit”时,您将提交暂存的所有内容。 git commit 的“-a”选项意味着自动暂存所有修改的文件,并提交它们(注意:这不包括存储库中尚未存在的文件)。

这是一个示例:

我有一个存储库,其中有两个文件:file1.txt 和 file2.txt
当我调用 git status 时,我得到以下信息:

# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   file2.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   file3.txt

因此,如果我想提交 file2.txt 更改和 file3.txt 我可以执行以下操作:

git add file2.txt
git add file3.txt
git commit

这将同时提交新文件和对 file2.txt 所做的更改

如果但是,我使用 git commit -a 仅提交 file2.txt 更改,而新文件 (file3.txt) 将被忽略。

因此,为了获得相同的效果,我也可以这样做:

git add file3.txt
git commit -a

在这种情况下,我不必添加更改的文件,因为提交的 -a 选项将处理该问题。

注意:如果您在没有“暂存”任何文件的情况下调用 git commit,则不会发生任何事情。

There are two parts to the git committing process. First you "stage" individual files and then you do the "commit" command which actually adds the "staged" files to the repository. So if you want to "stage" files you first will use the "git add" command:

git add myfile1

You can do that for as many files as you like. When you call "git status" you should see list of the staged files. You can "add" files that are already in your repository but have changes, and you can also "add" files that aren't yet in your repository. New files will come up as new in the staged area and other ones will just come up as modified, but when you finally call "git commit", you will commit everything that is staged. The "-a" option to git commit means automatically stage all modified files, and also commit them (Note: this does not include files that are not already in the repository ).

Here is an example:

I have a repository where I have two files: file1.txt and file2.txt
When I call git status I get the following:

# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   file2.txt
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#   file3.txt

So if I want to commit both file2.txt changes, and file3.txt I can do the following:

git add file2.txt
git add file3.txt
git commit

That will commit both the new file and the changes made to file2.txt

If however, I used git commit -a only file2.txt changes would have been commited and the new file (file3.txt) would have been ignored.

So to get the same effect, I could also do:

git add file3.txt
git commit -a

In this case I don't have to add the changed file because the -a option to commit will take care of that.

Note: If you call git commit without any files "staged", nothing will happen.

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