In my current environment, it is possible to accidentally commit all changed files at once, without staging. I don’t want that to happen.
(Thankfully, that it does happen and I have not yet synced the changes (AKA pushed to remote), I can just do git reset HEAD~1
, and all of the files which were just committed will be back in my staging area).
But how would one prevent all this from happening in the first place? It is super easy to Ctrl + Enter
and accidentally commit all the files.
3
Answers
This is actually not the default behavior of VSCode, you probably have
"git.enableSmartCommit": true
in your local settings.You can disable this setting (edit your local
settings.json
or open the UI editor) and either let the GUI guide you — you should have a dialog popping when youCtrl+Enter
with no staged file:or directly set
"git.suggestSmartCommit": false
if your intention is to say[Never]
.VS Code has a feature called "Smart Commits", which is probably what you’re running into here.
There are two/three settings of relevance:
git.enableSmartCommit
: "Commit all changes when there are no staged changes." (default:false
)git.suggestSmartCommit
: "Suggests to enable smart commit (commit all changes when there are no staged changes)." (default:true
)Probably what you want is to do is to keep
git.enableSmartCommit
at its default value offalse
(I.e. don’t allow commits when nothing has been manually staged for commit) and setgit.suggestSmartCommit
tofalse
(I.e. don’t ever prompt to changegit.enableSmartCommit
totrue
when attempting to commit before anything has been manually staged).Once you do that, the next time you try to perform a commit action in VS Code without any staged changes, you’ll get a notification saying "There are no changes to commit. Source: Git (Extension)", with a button allowing you to "Create Empty Commit" (if you want to).
Note that there’s also a
git.smartCommitChanges
setting, but it isn’t relevant here, since you don’t want the smart commit feature, and it’s really only relevant when you do want the smart commit feature. But for informational purposes:git.smartCommitChanges
: "Control which changes are automatically staged by Smart Commit." values:"all"
: "Automatically stage all changes." (default)"tracked"
: "Automatically stage tracked changes only."Matt Bierner (one of the VS Code maintainers) has made an explanatory video about the "Smart Commit" feature here.
If you like reading about tool history, see https://code.visualstudio.com/updates/v1_70#_action-button-improvements.
You can add a
pre-commit
script for git in your project. That way even if you try to commit it will prevent you, as your changes are not staged yet.Check here:
https://stackoverflow.com/a/76246507/11411002