In VS Code, is there a way to configure source control so that I MUST stage files/changes before they can be committed?

huangapple go评论66阅读模式
英文:

In VS Code, is there a way to configure source control so that I MUST stage files/changes before they can be committed?

问题

在我的当前环境中,很容易无意中一次性提交所有更改的文件,而无需进行暂存。我不希望发生这种情况。

(幸运的是,如果这种情况发生了,而且我还没有同步这些更改(也就是尚未推送到远程),我只需执行 git reset HEAD~1,刚刚提交的所有文件将重新回到我的暂存区)。

但是,如何在一开始就防止这一切发生呢?使用 Ctrl + Enter 很容易一不小心提交所有文件。

英文:

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.

答案1

得分: 1

这实际上不是VSCode的默认行为,您可能在本地设置中有"git.enableSmartCommit": true


您可以禁用此设置(编辑本地的settings.json或打开UI编辑器),然后让GUI指导您 - 当您按Ctrl+Enter而没有已暂存的文件时,应该会弹出一个对话框:

没有要提交的已暂存更改。
您是否要将所有更改暂存并直接提交?
                                 [从不] [始终] [取消] [是]

或者如果您的意图是 [从不],则直接设置"git.suggestSmartCommit": false

英文:

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].

答案2

得分: 1

VS Code有一个名为“智能提交”的功能,这可能是您在这里遇到的问题。

有两个/三个相关的设置:

  1. git.enableSmartCommit:"当没有已暂存的更改时,提交所有更改。"(默认值:false

  2. git.suggestSmartCommit:"建议启用智能提交(当没有已暂存的更改时提交所有更改)。"(默认值:true

可能您想要的是将git.enableSmartCommit保持在其默认值false(即不允许在没有手动暂存的情况下进行提交),并将git.suggestSmartCommit设置为false(即在尝试提交之前不要提示更改git.enableSmartCommittrue)。

一旦您这样做了,下次在VS Code中尝试执行提交操作而没有任何已暂存的更改时,您将收到通知,提示“没有要提交的更改。来源:Git(扩展)”,并提供一个按钮允许您“创建空提交”(如果您想要的话)。

请注意,这里还有一个git.smartCommitChanges设置,但它与您不想要的智能提交功能无关,只在您确实想要智能提交功能时才相关。但出于信息目的:

git.smartCommitChanges:"控制智能提交自动暂存的更改。" 值:

  1. "all":"自动暂存所有更改。"(默认值)
  2. "tracked":"仅自动暂存已跟踪的更改。"

VS Code的维护人员之一Matt Bierner已经制作了关于“智能提交”功能的解释视频 链接

如果您喜欢阅读有关工具历史的信息,可以查看 此链接

英文:

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.

答案3

得分: -1

你可以在你的项目中为 Git 添加一个 pre-commit 脚本。这样,即使你尝试提交,它也会阻止你,因为你的更改尚未暂存。

查看链接:
https://stackoverflow.com/a/76246507/11411002

英文:

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

huangapple
  • 本文由 发表于 2023年5月15日 11:26:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定