如何在索引中提交特定文件?

Tan*_*Woo 5 git

例子:

» git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    modified:   a
    modified:   b

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)

    modified:   a
Run Code Online (Sandbox Code Playgroud)

如果我修改了一个名为 a 的文件,并将它的一部分添加到index.

我只想提交索引中的文件 a,既不包含 b 也不包含工作目录中的 a。

我使用的方式是使用git commit -p和进入n不阶段。或者git commit -p file选择我想要的部分。

但是如果文件 a 中的部分已经添加到索引中,有没有更好的方法来直接提交文件 a 只在索引部分?


补充:

我认为这可能是对 git 的错误使用。

只有要在下一次提交中提交的更改才应添加到索引中。

显然文件 b 没有准备好提交,所以它应该在工作目录中。

Era*_*ran 5

git commit a只会承诺a并离开b舞台。

\n\n

这是手册页中的示例:

\n\n
$ edit hello.c hello.h\n$ git add hello.c hello.h\n$ edit Makefile\n$ git commit Makefile\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

这会进行一次提交,记录对 Makefile 的修改。为 hello.c 和 hello.h 暂存的更改不包含在生成的提交中。然而,它们的更改并没有丢失\xe2\x80\x89\xe2\x80\x94\xe2\x80\x89,它们仍然是上演的,只是被阻止了。

\n
\n

  • 仅提交“索引”中的特定文件,“git commit file”将提交“索引”和“工作目录”中的更改。 (4认同)

Von*_*onC 0

我只想提交索引中的文件 a,不包括工作目录中的 b 或 a。

b听起来您根本不想跟踪:

git rm --cached b
echo b >> .gitignore
git add .gitignore
git commit -m "Add a, ignore b"
Run Code Online (Sandbox Code Playgroud)

但如果你想跟踪b,只需确保你想添加任何修改,那么你可以使用git update-index

git reset -- b # will unstage (remove from index)
git update-index --skip-worktree -- b
...
# when ready to add b
git update-index --no-skip-worktree -- b
git add -- b
Run Code Online (Sandbox Code Playgroud)