git commit提交unstaged hunks

时间:2012-09-16 06:11:15

标签: git

我使用git add -p将我的代码更改拆分为多个提交。但是,在此之后执行git commit会提交所有更改,包括未暂存的更改。我看了几个关于SO的问题,但没有发现任何明显的错误。你能帮我理解我做错了什么吗?以下是我尝试的命令及其输出。

bash-3.2$ git diff test11.txt
diff --git a/test11.txt b/test11.txt
index f077274..e811cae 100644
--- a/test11.txt
+++ b/test11.txt
@@ -1,5 +1,5 @@
-Hello
-World
+hello
+world

-Blahblahblah
-blah
+blahblahblah
+Blah
bash-3.2$ git add -p test11.txt
diff --git a/test11.txt b/test11.txt
index f077274..e811cae 100644
--- a/test11.txt
+++ b/test11.txt
@@ -1,5 +1,5 @@
-Hello
-World
+hello
+world

-Blahblahblah
-blah
+blahblahblah
+Blah
Stage this hunk [y,n,q,a,d,/,s,e,?]? s
Split into 2 hunks.
@@ -1,3 +1,3 @@
-Hello
-World
+hello
+world

Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
@@ -3,3 +3,3 @@

-Blahblahblah
-blah
+blahblahblah
+Blah
Stage this hunk [y,n,q,a,d,/,K,g,e,?]? n

bash-3.2$ git status
# On branch test
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   test11.txt
#
# 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:   test11.txt
bash-3.2$ git commit test11.txt
[test 1b85189] Test12
 1 files changed, 4 insertions(+), 4 deletions(-)
bash-3.2$ git status
# On branch test
nothing added to commit

3 个答案:

答案 0 :(得分:10)

请勿在{{1​​}}命令中指定文件名。

默认情况下,没有其他参数的git commit会提交索引的内容(无论您通过git commit暂存了什么)。

但是,如果指定了文件名(例如git add),那么Git实际上会完全绕过索引并提交指定文件的当前状态,因为它们位于工作目录中。


基本上,使用git commit <filename> 指定文件名为git add,但不要同时执行这两项操作。

答案 1 :(得分:2)

$ man git-commit
NAME
       git-commit - Record changes to the repository

SYNOPSIS
       git commit [-a | --interactive] [-s] [-v] [-u<mode>] [--amend] [--dry-run]
                  [(-c | -C | --fixup | --squash) <commit>] [-F <file> | -m <msg>]
                  [--reset-author] [--allow-empty] [--allow-empty-message] [--no-verify]
                  [-e] [--author=<author>] [--date=<date>] [--cleanup=<mode>]
                  [--status | --no-status] [-i | -o] [--] [<file>...]
[...]

       <file>...
           When files are given on the command line, the command commits the contents of the named files, without recording the changes already
           staged. The contents of these files are also staged for the next commit on top of what have been staged before.

您只想撰写git commit <file>git commit

,而不是撰写git commit -m "this is my commit message"

答案 2 :(得分:0)

git commit --patch <pathnames...>做你所期待的。那就是:&#34;提交我已经在这些特定文件中播放的内容,但不要自动暂存和提交这些文件中任何未分级的黑客。&#34;不幸的是,它具有令人惊讶的效果,然后也提交了<pathnames...>中未列出的分阶段更改。 (git 2.13.6)