25

I'm looking for the equivalent of git commit -am "blah blah" but for just a single file. If I try:

git commit my.file -am "blah blah"

I get:

fatal: Paths with -a does not make sense.

I looked around but I could only find solutions that suggest using aliases (e.g. this one), but even those don't seem like they could be modified because I need to pass an argument. Do I have to resort to calling git through a shell?

It seems like there should be a simpler option for something that I imagine would be extremely common. Right now I'm stuck with:

git add my.file
git commit -m "blah blah"
Community
  • 1
  • 1
BrodieG
  • 51,669
  • 9
  • 93
  • 146

1 Answers1

43

Just omit the -a which means --all which is not what you want. Do this:

git commit my.file -m "blah blah"

That will commit only my.file, even if other files are staged (by git add).

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 4
    Jeez, can't believe it was that simple. I didn't realize I could commit without staging (the "Changes not staged for commit:" message threw me off). No wonder no one else asked this question... I also missinterpreted the -a to mean `add`. – BrodieG Oct 16 '14 at 14:38
  • 2
    This *will* commit the file even though it's not yet staged by `git add`. (I didn't believe that at first -- though it's mentioned both in question and in the above comment(!), so explicitly mentioning it here.) – Nickolay Jun 29 '17 at 10:35
  • 5
    However, if a file is yet untracked, this won't commit it. – isarandi May 07 '18 at 12:33