git - 您如何在不暂存文件的情况下添加新文件?

标签 git staging git-index

为了有效地(按预期)使用 git,我进行了小的原子提交,同时我确实有更长的 session ,我不仅改变了一件事。因此,我大量使用 git add -p。不过,这不适用于全新的文件,因为我以后往往会忘记它们。

我想做的是,告诉 git 有一个新文件,我希望跟踪它,但不要暂存它 :

示例:运行 git status 产生:

# On branch my-current-branch
# Your branch is ahead of 'origin/my-current-branch' by 2 commits.
#
# Changes to be committed:
#
<<STAGED SECTION>> // A
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
<<UNSTAGED-YET-KNOWN SECTION>> // B
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
<<UNKNOWN SECTION>> // C

如果我在 C 部分中有一个文件 foo,并且我说 git add foo 它将转到 A 部分。如果我说 git add -N foo 它将同时转到 A 和 B。但是,这意味着它将包含在下一次提交中,至少因为有一个新文件。

我希望它专门放在 B 部分,这样我以后可以使用 git add -pgit add foo (或其他)将它添加到 A 部分。

编辑

关于 add -N 解决方案,这是行不通的,因为如果我在说完 add -N之后尝试提交正确添加后,git 会提示,因为它不知道如何处理空文件:

foo: not added yet
error: Error building trees

最佳答案

对于 Git 2.5,git add -N/--intent-to-add 实际上是正确的解决方案。
新文件不会成为下一次提交的一部分。

参见 commit d95d728作者 Nguyễn Thái Ngọc Duy ( pclouds )( merge 于 d0c692263 ):

diff-lib.c: adjust position of i-t-a entries in diff

问题:

Entries added by "git add -N" are reminder for the user so that they don't forget to add them before committing. These entries appear in the index even though they are not real. Their presence in the index leads to a confusing "git status" like this:

On branch master
Changes to be committed:
        new file:   foo

Changes not staged for commit:
        modified:   foo

If you do a "git commit", "foo" will not be included even though "status" reports it as "to be committed".

解决方案:

This patch changes the output to become

On branch master
Changes not staged for commit:
        new file:   foo

no changes added to commit

这意味着:

Treat such paths as "yet to be added to the index but Git already know about them"; "git diff HEAD" and "git diff --cached HEAD" should not talk about them, and "git diff" should show them as new. + files yet to be added to the index.

关于git - 您如何在不暂存文件的情况下添加新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11087143/

相关文章:

git - git索引中的 "stat information"是什么?

Git [远程拒绝] - 无法锁定 ref

ruby-on-rails - 使用 capistrano 进行分期和实时应用程序

git - github 分支与我推送的差异

jquery - 使用 jQuery 将子文件夹添加到所有链接之前

css - 如何在 rails 2.3 中预编译 css 样式表?

git - 如何从 git 索引中删除已修改的文件并一步放弃修改?

git - 为什么 "git index"有这么多名字?

git - 使用当前的 .gitignore 使 Git 重建历史

git - 防止在 Git 中推送本地更改