2012-09-16 43 views
2

我用git add -p将我的代码更改分成多个提交。但是,做了git commit之后提交了所有的变化,包括未分离的变化。我看了几个关于SO的问题,但找不到任何明显的错误。你能帮我理解我做错了什么吗?以下是我尝试的命令及其输出。git commit提交unstaged的宏块

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 

回答

10

不要在git commit命令中指定文件名。

默认情况下,git commit没有其他参数提交索引的内容(无论您通过git add进行了何种操作)。但是,如果你指定了一个文件名(例如git commit <filename>),那么Git实际上完全绕过了索引并且提交指定文件的当前状态,因为它们在工作目录中。


基本上,要么使用git addgit commit指定的文件名,但不要两者都做。

+0

感谢。这有助于解决我的问题。我尝试过这种方式,因为我有多个文件上演,并不想在一次提交中提交它们。在这种情况下,最好的做法是一次添加/提交一个文件。 –

+1

是的,基本上,使用暂存区来构建提交的内容,然后提交这些已上演的内容。冲洗并重复。 – Amber

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 commitgit commit -m "this is my commit message"

+0

感谢您的回答。它有帮助。不幸的是,我不能接受这个答案,因为我必须选择一个。 –