git 决定不提交新添加的文件当前内容而是提交添加时的内容背后的理由是什么?

发布于 2024-10-31 05:58:36 字数 409 浏览 0 评论 0原文

所以,根据我在 Pro Git 中读到的内容,

原来 Git 暂存了一个文件 与运行时完全相同 添加命令。如果你承诺 现在,版本 someFile 原样 当您上次运行 add 时 命令是它将如何进入 提交,而不是文件的版本 它会在您的工作目录中查找 当您运行commit时。如果 运行后修改文件 添加,你必须运行 再次添加以暂存 文件的最新版本

似乎当您将文件添加到 git 存储库时,要么立即提交,要么在提交之前对同一文件进行任何修改,这些修改将不会保存在该提交中。

这背后的理由是什么?

So, from what I've read in Pro Git,

It turns out that Git stages a file
exactly as it is when you run the
add command. If you commit
now, the version of
someFile as it was
when you last ran the add
command is how it will go into the
commit, not the version of the file as
it looks in your working directory
when you run commit. If
you modify a file after you run
add, you have to run
add again to stage the
latest version of the file

it seems that when you add a file to your git repository, either you commit right away, or your if you make any modifications to that same file before the commit, those modifications won't be saved in that commit.

What is the rationale behind that?

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

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

发布评论

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

评论(3

稚然 2024-11-07 05:58:36

git 不跟踪文件,它跟踪更改。因此,当您“添加文件”时,您不是添加文件,而是向该文件添加更改,对于新文件来说是您添加它时的所有内容。因此,如果您在“添加文件”之后但在提交之前进行更改,则还必须添加这些新更改,否则它们将不会被提交。

git doesn't track files, it tracks changes. So when you "add a file", you're not adding the file, you're adding the changes to that file, which in the case of a new file is all its content at the point that you added it. So, if you make changes after "adding the file" but before you commit, you have to add those new changes as well or they won't get committed.

大海や 2024-11-07 05:58:36

几乎总是,在添加后立即提交是有意义的,这就是为什么 commit 有一个 -a 选项,以及为什么几乎所有其他 VCS 不允许您拆分添加和提交,即使它们在内部是单独的阶段。

在某些情况下,单独执行会非常方便。如果有些人不确定是否要保留更改,并且仍在进行试验,他们会使用暂存区作为一种临时提交。我更喜欢所有提交都是可编译的,因此当我准备好提交但遇到一堆编译错误时,有时我会执行 add 操作。这让我可以对我所做的更改进行比较,同时修复编译错误,以防万一搞砸了。如果您出于某种原因想要将更改拆分为多个提交,它也非常方便。

Almost always, it makes sense to commit immediately after an add, which is why commit has an -a option, and why practically every other VCS doesn't let you split the add and commit, even though they are separate stages internally.

There are some circumstances where it is very handy to do separately. Some people use the staging area as a sort of temporary commit if they're not sure they want to keep the changes or not, and are still experimenting. I prefer all my commits to be compilable, so I will sometimes do an add when I am ready to commit but have a bunch of compile errors. That lets me do a diff on what I have changed while fixing compile errors in case I mess anything up. It's also very handy if you want to split your changes into multiple commits for whatever reason.

腻橙味 2024-11-07 05:58:36

它实际上与 git add 处理非新文件的方式非常一致。

$ git checkout master # assume there is some file called code.c
# hack hack hack
$ git add code.c
# hack hack hack
$ git commit

这会添加文件与您执行 git add 时的情况相同。比如说,在 svn 中,您甚至不会执行添加操作,因为 svn add 仅适用于新文件。因此,这是一个额外的步骤(除非您像其他人所说的那样使用 git commit -a ),但它为您提供了更大的灵活性,可以在同一个文件中仅提交一些更改,而让其他更改保持未提交。一旦您熟悉了 git,您可能会因为这个原因而爱上 git add -p 命令。 :)

It's actually very consistent with the way git add works for non-new files.

$ git checkout master # assume there is some file called code.c
# hack hack hack
$ git add code.c
# hack hack hack
$ git commit

This adds the file as it looked at the time you did the git add. In, say, svn, you wouldn't even do the add because svn add is only for new files. So it's one extra step (unless you use git commit -a as someone else said) but it gives you a lot more flexibility, to commit only some changes and leave others uncommitted, within the same file. Once you get comfortable with git, you may fall in love with the git add -p command for exactly this reason. :)

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