示例:
» 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
如果我修改了一个名为a的文件,并将其部分添加到index
中,那么索引中也有一个名称为b的文件。
我只想在索引中提交文件a,既不包括b也不包括工作目录中的a。
我使用的方式是使用git commit -p
并输入n
以不登台。或git commit -p file
,然后选择我想要的零件。
但是,如果文件中的部分已经是外接程序索引,是否有更好的方法直接提交文件中的仅在索引中的部分?
补充:
我认为这可能是对git的错误使用。
只有要在下一次提交中提交的更改才应添加到指数
很明显,文件b没有准备好提交,所以它可能在工作目录中。
git commit a
将只提交a
,并保留b
。
下面是手册页中的一个示例:
$ edit hello.c hello.h
$ git add hello.c hello.h
$ edit Makefile
$ git commit Makefile
这将进行一次提交,记录对Makefile的修改。为hello.c和hello.h进行的更改不包括在最终提交中。然而,他们的改变并没有丢失—它们仍然在上演,只是被搁置了。
我只想在索引中提交文件a,既不包括b也不包括工作目录中的a。
听起来你根本不想追踪b
:
git rm --cached b
echo b >> .gitignore
git add .gitignore
git commit -m "Add a, ignore b"
但如果你想跟踪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