【发布时间】:2021-05-04 21:31:12
【问题描述】:
如上所说
git - How do I commit only some files? - Stack Overflow
我们可以使用
git commit [--only] a b c -m "only part of files"
但是在下面的例子中:
$ mkdir t
$ cd t
$ git init
Initialized empty Git repository in /mnt/c/test/git-test/t/.git/
$ touch a b
$ git add .
$ git commit a -m a
[master (root-commit) c7939f9] a
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
$ git commit b -m b
[master cf4514a] b
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b
$ git status
On branch master
nothing to commit, working tree clean
$ ls
a b
我尝试仅将文件 b 提交到第二次提交中,但失败了。 (在工作树和工作树中使用 a、b 和 clean。这意味着两个文件都已提交。)
那么如何真正提交部分文件呢?
即使git add 单个文件也不起作用:
$ mkdir t
$ cd t
$ git init
Initialized empty Git repository in /mnt/c/test/git-test/t/.git/
$ touch a b
$ git add a
$ git commit --only a -m "a"
[master (root-commit) 04383c9] a
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
$ git rm --cached -r .
rm 'a'
$ git add b
$ git commit --only b -m "b"
[master d518916] b
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b
$ git checkout -f head~
Note: switching to 'head~'.
...
HEAD is now at 04383c9 a
$ ls
a
$ git checkout -f master
Previous HEAD position was 04383c9 a
Switched to branch 'master'
$ ls
a b
文件 a 仍在第二次提交中。
背景: 假设我有一个包含许多文件的文件夹,并且我想将文件集 A 提交到第一个提交(即第一个提交仅包含文件集 A),将 B 设置到第二个提交,... 我为什么这样做:只是出于好奇。
【问题讨论】:
-
使用
git commit -p ./fiepath命令。在这里查看更多git-scm.com/docs/git-commit#Documentation/git-commit.txt--p -
在提交前使用
git add -pi。运行命令后使用?阅读帮助。 -
@NomanGul 它不起作用。它只是提交了所有暂存文件。
-
@caramba 我认为
git add -pi是选择要暂存的更改块,而不是文件选择。例如,当您有一些空白文件时,它会显示“没有更改”。 -
我认为你误解了一些东西。在您的第二个示例中,您尚未提交删除
a,因此它在第二次提交中仍然存在。你想达到什么目的,目的是什么?我认为这会更容易解释这一点(或意识到你实际上是正确的:p)