Unexpected "Nothing specified, nothing added" message in Git

Recently, Git (2.46.0, homebrew, macos 14.6.1) has started behaving like this:

$ git add README.md

...

$ git commit -m "Update README file"

Nothing specified, nothing added.
hint: Maybe you wanted to say 'git add .'?
hint: Disable this message with "git config advice.addEmptyPathspec false"
[master 3d0e36e] Update README file
 1 file changed, 27 insertions(+), 22 deletions(-)

$ git log
commit 3d0e36e8c6bb589201dbd9670b5282bb40115b40 (HEAD -> master)
Author: redacted <redacted@example.com>
Date:   Sat Aug 31 18:24:52 2024 +0100

    Update README file

i.e.:

  • I add a file
  • I write a commit message and it tells me there’s nothing to commit
  • It commits the change anyway

The docs imply you would only see that message when running git add:

addEmptyPathspec Shown when the user runs git add without providing the pathspec parameter.

Understanding the Problem:

  • You’re adding a modified file (README.md ) to the staging area using git add README.md .
  • Git warns you with the message "Nothing specified, nothing added. hint: Maybe you wanted to say 'git add .'?..."
  • Despite the warning, the file gets committed with git commit -m "Update README file" .

Explanation:

This behavior occurs because Git’s default configuration includes the advice.addEmptyPathspec setting. When you run git add with a specific file path like README.md , Git checks if the file already exists in the working directory. If it doesn’t, it displays the warning message. However, even with the warning, Git allows you to proceed with the commit, potentially leading to unexpected results.

Solution:

There are two main approaches to address this:

  1. Temporarily Disable the Warning:
  • Run the following command in your terminal:
    Bash
git config --global advice.addEmptyPathspec false
  • This disables the warning message globally for all future Git operations.
  • Explicitly Add All Changes:
  • Instead of using git add <file> , use git add . .
    Bash
git add
  1. This command adds all modified and tracked files in the current working directory to the staging area, ensuring your intended changes are committed.

Recommendation:

Consider using the second approach (adding all changes explicitly) to maintain consistency and avoid potentially forgetting to add files in the future. However, if you prefer not to see the warning message, the first approach is available for temporary disabling.

Additional Notes:

  • To re-enable the warning message after disabling it, run the following command:
    Bash
git config --global advice.addEmptyPathspec true

These approaches are applicable to your specific Git version (2.46.0) and macOS version (14.6.1).