Home > Enterprise >  git commit doesn't appear to work when --porcelain option is set
git commit doesn't appear to work when --porcelain option is set

Time:11-08

It appears that git commit doesn't work when the --porcelain option is selected. Is this a bug? Or am I misunderstanding something about how git commit works?

I have a repo containing one file that has been modified. When I run git status, I see this (as expected):

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   README.md

When I run git commit -m "Update to readme" --porcelain I get this output:

$ git commit -m "Update to readme" --porcelain
M  README.md

But, when I run git status again, I get the original output:

On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   README.md

Now, if I commit without --porcelain I get the expected behaivor:

[main 6f17cac] Update to readme
 1 file changed, 6 insertions( ), 3 deletions(-)

And git status also gives the expected result:

On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

Is this a bug? Or am I misunderstanding something about how git commit works?

I am using git 2.37.1 on MacOS 12.6:

$ git --version
git version 2.37.1 (Apple Git-137.1)

CodePudding user response:

--porcelain implies --dry-run which does not actually do the commit.

--dry-run

Do not create a commit, but show a list of paths that are to be committed, paths with local changes that will be left uncommitted and paths that are untracked.

--porcelain

When doing a dry-run, give the output in a porcelain-ready format. See git-status(1) for details. Implies --dry-run.

  • Related