skip to Main Content

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


  1. 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 you Ctrl+Enter with no staged file:

    There are no staged changes to commit.
    Would you like to stage all your changes and commit them directly ?
                                     [Never] [Always] [Cancel] [Yes]
    

    or directly set "git.suggestSmartCommit": false if your intention is to say [Never].

    Login or Signup to reply.
  2. VS Code has a feature called "Smart Commits", which is probably what you’re running into here.

    There are two/three settings of relevance:

    1. git.enableSmartCommit: "Commit all changes when there are no staged changes." (default: false)

    2. 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 of false (I.e. don’t allow commits when nothing has been manually staged for commit) and set git.suggestSmartCommit to false (I.e. don’t ever prompt to change git.enableSmartCommit to true 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:

    1. "all": "Automatically stage all changes." (default)
    2. "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.

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search