267

I tried the following command:

git commit path/to/my/file.ext -m 'my notes'

And received an error in Git version 1.5.2.1:

error: pathspec '-m' did not match any file(s) known to git.
error: pathspec 'MY MESSAGE' did not match any file(s) known to git.

Is that incorrect syntax for a single file or directory commits?

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
doublejosh
  • 5,548
  • 4
  • 39
  • 45

8 Answers8

419

Your arguments are in the wrong order. Try git commit -m 'my notes' path/to/my/file.ext, or if you want to be more explicit, git commit -m 'my notes' -- path/to/my/file.ext.

Incidentally, Git v1.5.2.1 is 4.5 years old. You may want to update to a newer version (1.7.8.3 is the current release).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • 4
    You make a good observation. However, interestingly Git 1.7.5.1 on Windows blithely accepts `git commit path_to_file -m 'message'` – Sri Sankaran Jan 09 '12 at 21:32
  • 1
    @SriSankaran: Sounds like they taught it to recognize out-of-order arguments then. But according to the documentation, the correct order is to put the file list last, and it's probably a good idea to stick to the documented version of things. – Lily Ballard Jan 09 '12 at 21:40
  • Thanks. The order of these messages changed... or at least the strictness of the order changed ;) – doublejosh Jan 10 '12 at 23:22
  • BTW: This was a sandbox machine spun up from an old VM recipe. My local Git was up to date, but didn't realize this version was old (bad assumption and not knowing the version number history). Thanks all. – doublejosh Feb 27 '12 at 21:39
  • I vote down for one reason: it won't work in GIT Bash when I'm trying to commit the whole Unity project. – David Dimalanta Feb 18 '14 at 02:30
  • 1
    @DavidDimalanta: What do you mean? – Lily Ballard Feb 18 '14 at 22:56
  • I'm trying to commit a whole Unity project using your solution as provided. Before that, I have to git add the whole project folder except the filename "UnityLockfile" at Temp folder. I ignore it. Next, I decided to commit it although it will commit all the added new files only before ready to push it. It is worked on other project folders but except for Unity. – David Dimalanta Feb 19 '14 at 01:52
  • @KevinBallard I see my errors though. Active files may cannot be committed nor pushed until do so. And, I decided do this format for entering directory like this: git commit "C:/path/to/my/file" -m "Committed a directory" – David Dimalanta Aug 25 '15 at 06:22
  • 1
    On Windows with the Git CMD console I had to use `"Double Quotes"`. – nu everest Feb 22 '16 at 20:37
  • what are the meaning/benefits of the -- in the 'more explicit solution' ? – ihebiheb Dec 02 '19 at 15:16
  • 1
    @ihebiheb Looking right now I don't see any other non-flag parameters to `git commit` so I guess the answer is "nothing", but in many other git commands the `--` distinguishes paths from other freeform arguments (for example, with `git log` the `--` prevents a path from being interpreted as a revision range instead) – Lily Ballard Dec 04 '19 at 06:51
111

Try:

git commit -m 'my notes' path/to/my/file.ext 

of if you are in the current directory, add ./ to the front of the path;

git commit -m 'my notes' ./path/to/my/file.ext 
tommytucker7182
  • 213
  • 1
  • 5
  • 11
wadesworld
  • 13,535
  • 14
  • 60
  • 93
20

If you are in the folder which contains the file

git commit -m 'my notes' ./name_of_file.ext
Misa Lazovic
  • 2,805
  • 10
  • 32
  • 38
Piethon
  • 201
  • 2
  • 2
  • 1
    if I am already in the file, do I need a leading "./" (dot forward-slash)? could I use `git commit -m "my note" name_of_file.txt`? – Chris22 Jul 01 '16 at 19:23
  • @Chris22 I am not sure what you mean by "if I am already in the file" (maybe you meant "in the directory"?)... `./` is just normal path syntax, but yes, not necessary in this example. – Jonathan Cross Apr 27 '19 at 21:13
11

Use the -o option.

git commit -o path/to/myfile -m "the message"

-o, --only commit only specified files

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
W.Perrin
  • 4,217
  • 32
  • 31
8

Specify the path after the entered commit message, like:

git commit -m "commit message" path/to/file.extension
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul TP
  • 536
  • 5
  • 9
5

For Git 1.9.5 on Windows 7: "my Notes" (double quotes) corrected this issue. In my case, putting the file(s) before or after the -m 'message' made no difference; using single quotes was the problem.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
EddieBaby
  • 272
  • 3
  • 4
1

Suppose you are working on big project and have opened multiple files, and you made changes in single file, when you don't need to write git add ., this will add all the files to git, so you first need to check where you made changes by git status, here you will see all the paths next to the filenames, copy the path of the file where you made change and then write git add path, here path is whole line of path to the file (your modified file). Then you write your commit message by git -m "message" and then push.

This will push only the specified file which you have used with git add file

Danish Mehmood
  • 111
  • 1
  • 7
-5

You try if you are in the master branch:

git commit -m "Commit message" -- filename.ext
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131