Masked Man
Masked Man

Reputation: 11055

git commit commits unstaged hunks

I used git add -p to split my code changes into multiple commits. However, doing git commit after that commits all changes, including the unstaged ones. I looked at a few questions on SO, but could not find any obvious mistake. Could you please help me understand what I am doing wrong? Below are the commands I tried, and their outputs.

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

Upvotes: 2

Views: 1140

Answers (3)

Joe Soroka
Joe Soroka

Reputation: 1

git commit --patch <pathnames...> does what you were expecting. That is: "commit what I've already staged in these specific files, but do not automagically stage-and-commit any unstaged hunks in those files." Unfortunately it has the surprising effect of then also committing staged changes that weren't listed in <pathnames...>. (git 2.13.6)

Upvotes: 0

einarmagnus
einarmagnus

Reputation: 3590

$ 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.

Instead of writing git commit <file>, you'll want to write just git commit or git commit -m "this is my commit message"

Upvotes: 2

Amber
Amber

Reputation: 527123

Don't specify the filename in the git commit command.

By default, git commit with no other parameters commits the contents of the index (whatever you have staged via git add).

If you specify a filename, however, (e.g. git commit <filename>), then Git actually bypasses the index entirely and commits the current state of the specified file(s) as they are in the working directory.


Basically, either use git add or specify filenames with git commit, but don't do both.

Upvotes: 11

Related Questions