7

In Visual Studio Code there is a warning if you attempt a commit with no staged files. It warns you that it will commit all unstaged files first and then commits them.

Is there a similar setting or a way to do the same thing in Visual Studio (2017 and onwards)?

The default behavior of Visual Studio 2017, when you hit Commit without staging files first is to silently commit all unstaged files, which causes issues quite frequently (accidental commits).

Under Settings => Source Controll I only see "Plugin Selection" with no additional options.

Holistic Developer
  • 2,458
  • 1
  • 18
  • 27
Pavel Donchev
  • 1,809
  • 1
  • 17
  • 29
  • 1
    VSCode 1.75 (Jan. 2023) should support this natively. See my [edited answer below](https://stackoverflow.com/a/56536900/6309). – VonC Jan 17 '23 at 23:42

2 Answers2

18

I accidentally clicked on "Always" when asked if I want to commit the unstaged files.

I started looking for a way to undo that choice of "Always", so I found this thread.

Anyways, here is the solution: look for "Git: Enable Smart Commit" in the VSCode settings and uncheck it (set to false) vs code git smart commit - always commit unstaged changes

Aleksandar
  • 3,558
  • 1
  • 39
  • 42
2

2019: There are no settings in Visual Studio, as discussed in this VSCode issue

In the case of Visual Studio, when there are no staged changes, but some changes, the content of the commit button comes "Commit All". So, user can know all changes will be committed even no changes staged.

The latest 2019 release notes don't show any evolution on that front, so for now, there seems to be no setting in place (like the one in VSCode)

That seems a bug though, considering the documentation:

Git does not automatically add changed files to the snapshot when you create a commit.
You must first stage your changes to let Git know which updates you want to add to the next commit. Staging lets you to selectively add files to a commit while excluding changes made in other files.


However, the same issue 15613 refers to VSCode 1.13 commit b31c1e1, which shows:

            if (pick === always) {
                config.update('enableSmartCommit', true, true);
            } else if (pick !== yes) {
                return false; // do not commit on cancel

So, as shown in Aleksandar's answer, enabling smart commit can help.

Only problem: issues 91472 and issues 51721: you need to avoid staging files when a commit is in progress.


Update 2023: issues 51721 is resolved

This has now been addressed as most commands contributed by the git extension are disabled while the commit operation is running.
And so is the commit input field.

For VSCode 1.75 (Jan. 2023), or VSCode insiders today.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • No, that seems not to be a bug but a very bad choice to please the users of their own VCS solution, TFVC... – Philippe Jun 11 '19 at 05:24
  • @Philippe I agree (I has forgotten about the TFVC side in this case) – VonC Jun 11 '19 at 05:47
  • @VonC thanks for the confirmation (both for 2017 and then - 2019). This will force me to search for a solution either on marketplace or try to do it on my own. I think pull requests could help avoiding such issues (someone pushes, the reviewer finds the issue), but still it will be better to prevent than to correct error. – Pavel Donchev Jun 11 '19 at 06:32
  • @VonC Say I modified three files, and want to commit only two, without adding the third to gitignore. Is there such a way in VSCode? Or no: and your answer refers to that? – Giorgi Moniava Mar 24 '20 at 14:08
  • @giorgim That would be `git update-index --assume-unchanged [path]` (https://stackoverflow.com/a/936290/6309), not sure if it is integrated to VSCode. – VonC Mar 24 '20 at 20:55