How to 'git commit' a single file/directory

376,168

Solution 1

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

Solution 2

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 

Solution 3

If you are in the folder which contains the file

git commit -m 'my notes' ./name_of_file.ext

Solution 4

Use the -o option.

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

-o, --only commit only specified files

Solution 5

Specify the path after the entered commit message, like:

git commit -m "commit message" path/to/file.extension
Share:
376,168

Related videos on Youtube

doublejosh
Author by

doublejosh

I develop, maintain, and refine web systems. Including: front-end code, content management backends and APIs, UX components, user data flow, logging metrics, enabling analytics, and directing projects. doublejosh.github.io/react-portfolio

Updated on July 17, 2022

Comments

  • doublejosh
    doublejosh almost 2 years

    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?

    Answer:

    Arguments were expected in this order...

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

    And it's not strict any more :)

    • CB Bailey
      CB Bailey over 12 years
      Are you sure you have your version correct? Updating to 1.5.2.1? 1.5.2.1 is over 4 years old now.
    • Adam Dymitruk
      Adam Dymitruk over 12 years
      Is anything prohibiting you from upgrading git?
    • doublejosh
      doublejosh over 9 years
      BTW: This was a VM spun up from an old recipe. Local Git was up to date, didn't realize this version was ancient. Strictness changed.
  • Sri Sankaran
    Sri Sankaran over 12 years
    You make a good observation. However, interestingly Git 1.7.5.1 on Windows blithely accepts git commit path_to_file -m 'message'
  • Lily Ballard
    Lily Ballard over 12 years
    @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.
  • doublejosh
    doublejosh over 12 years
    Thanks. The order of these messages changed... or at least the strictness of the order changed ;)
  • doublejosh
    doublejosh over 12 years
    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.
  • David Dimalanta
    David Dimalanta over 10 years
    I vote down for one reason: it won't work in GIT Bash when I'm trying to commit the whole Unity project.
  • Lily Ballard
    Lily Ballard over 10 years
    @DavidDimalanta: What do you mean?
  • David Dimalanta
    David Dimalanta over 10 years
    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
    David Dimalanta almost 9 years
    @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"
  • nu everest
    nu everest over 8 years
    On Windows with the Git CMD console I had to use "Double Quotes".
  • Chris22
    Chris22 almost 8 years
    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?
  • doublejosh
    doublejosh over 6 years
    That option isn't necessary. If you include a file page in your commit command it will only commit those files.
  • Tomas Varga
    Tomas Varga over 5 years
    Good to know if you want to make sure when messing with further params, e.g. --amend
  • Jonathan Cross
    Jonathan Cross about 5 years
    @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.
  • ihebiheb
    ihebiheb over 4 years
    what are the meaning/benefits of the -- in the 'more explicit solution' ?
  • Lily Ballard
    Lily Ballard over 4 years
    @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)
  • Jan Pansky
    Jan Pansky about 3 years
    For me, it doesn't work without double quotes ", use git commit -m "my notes" ./name_of_file.ext