How do I solve this empty git commit warning?
Asked Answered
S

2

17

After pushing my code to the remote branch and creating a PR. I wanted to make some more changes to my code and then commit to the remote branch again

First, I started these steps:

git add .
git commit -m "Remove semicolons, change to multi line returns"`

But then this appeared right after the commit: enter image description here

I checked the status and unstage all the added files

git status
git restore --stage .

-> Then I add and commit again, the error is still there.

After that, I undo the commits & pull the branch to start again

git reset HEAD^
git pull

When I'm done making changes to the code, I ran git diff to see the changes I've made. Finally, I ran git add . & git commit -m "Recommit message", but somehow the error came back


Can anyone help me with what actually happened and how can I fix it?

If we can't fix it, is there any way to revert the code to normal?

Thank you!

Submariner answered 10/3, 2022 at 7:15 Comment(10)
Are you sure that git status shows anything in "Changes to be committed:" section?Wendall
@tymtam I did run git status once after the git pull. It shows: "On branch responsive_header. Your branch is ahead of 'origin/responsive_header' by 2 commits. (Use git push to publish your local commits). Nothing to commit, working tree clean"Submariner
"Nothing to commit, working tree clean" means just that. There is nothing to commit.Wendall
@tymtam After that, I git add . and commit again but it said "No staged files found". Then I make changes & add, commit again like I mentioned in the question above. It results to the same errorSubmariner
What does git status say after you make changes?Wendall
@tymtam It shows "Changes to be committed: (use "git restore --staged <file>.." to unstage). Modified: src/components/responsive_header.tsx" - Which is the file i made changesSubmariner
Can you show the sequence of commands with git status, git add, git commit where you have changes? It might be that there is a bug in your lint hook.Alexipharmic
Great. Now you just need to git commit -m "My awesome work". You should not git restore...Wendall
@tymtam I tried git commit again. It still shows the error :( Do you think it has something to do with the answer below about whitespace?Submariner
Can you show your husky configuration in package.json (husky: {...})? You probably have some kind of linter which fires just before your files are commited (via husky). When changes are small enough and linter fixes them for you, from git perspective nothing changes, hence this prevent empty commit error shows.Episcopalian
G
32

Look at the husky line on your screenshot, you have a pre-commit hook running here.

The reason it fires would have to be found in the hook itself, but it looks like the linting process deems that you changed only whitespace (check it with git diff --staged just before committing).

So if you do want to commit only your whitespace changes but the hook prevents it, check .git/hooks/pre-commit and consider using -n for your commit command (--no-verify) to commit without triggering the hook.

The alternative would be to use --allow-empty as hinted in yellow, but without knowing exactly what's in your lint hook, hard to say for sure. Who set up your repo/workflow? That hook has likely been put here for a reason, so be sure to discuss the matter with them.

Gertrude answered 10/3, 2022 at 7:49 Comment(2)
So you're saying if the changes are not only whitespace, it would work? I did change the whitespace, some parentheses and removed all the semicolons - I didn't think it would be a problem then. I will definitely discuss this with the owner. Thank you so much!Submariner
For me it was truly just whitespace. I was testing my husky setup and could not figure out why it would not run after the initial setup. If I change a boolean value it then runs husky properly through the commit process.Amblygonite
D
4

I found a good explanation of the problem in this article. At the end of the article it says the following:

lint-stage error out saying it was an empty git commit - a commit with no changes. No changes because all the changes I made were formatting changes, which were wrong according to prettier and it fixed it back and on doing so, there was no changes left to even be committed to git.

I think that the situation is similar with the processing of eslint files. Write code that does not contradict the rules of eslint and prettier and this error will not occur.

Duque answered 11/1, 2024 at 11:26 Comment(1)
For me, the issue is its not even linting, it just says lint-staged prevented and empty git commit and then my files remained unlinted :(Cobweb

© 2022 - 2025 — McMap. All rights reserved.