From 0e28a4b7604b3ea6b26e1db4576bb3cf4e14f552 Mon Sep 17 00:00:00 2001 From: Dmitriy Vasyura Date: Fri, 16 Jan 2026 21:36:23 +0100 Subject: [PATCH] Add `Git: Delete` action to run `git rm` command on the current document (#285411) --- extensions/git/package.json | 16 +++++++++++++++ extensions/git/package.nls.json | 2 ++ extensions/git/src/commands.ts | 35 +++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/extensions/git/package.json b/extensions/git/package.json index bb18ee9bf6b..47dbceb9092 100644 --- a/extensions/git/package.json +++ b/extensions/git/package.json @@ -321,6 +321,13 @@ "icon": "$(discard)", "enablement": "!operationInProgress" }, + { + "command": "git.delete", + "title": "%command.delete%", + "category": "Git", + "icon": "$(trash)", + "enablement": "!operationInProgress" + }, { "command": "git.commit", "title": "%command.commit%", @@ -1297,6 +1304,10 @@ "command": "git.rename", "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && resourceScheme == file && scmActiveResourceRepository" }, + { + "command": "git.delete", + "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0 && resourceScheme == file" + }, { "command": "git.commit", "when": "config.git.enabled && !git.missing && gitOpenRepositoryCount != 0" @@ -3303,6 +3314,11 @@ "description": "%config.confirmSync%", "default": true }, + "git.confirmCommittedDelete": { + "type": "boolean", + "description": "%config.confirmCommittedDelete%", + "default": true + }, "git.countBadge": { "type": "string", "enum": [ diff --git a/extensions/git/package.nls.json b/extensions/git/package.nls.json index ef41d0d6f44..94a1f61a516 100644 --- a/extensions/git/package.nls.json +++ b/extensions/git/package.nls.json @@ -36,6 +36,7 @@ "command.unstageChange": "Unstage Change", "command.unstageSelectedRanges": "Unstage Selected Ranges", "command.rename": "Rename", + "command.delete": "Delete", "command.clean": "Discard Changes", "command.cleanAll": "Discard All Changes", "command.cleanAllTracked": "Discard All Tracked Changes", @@ -167,6 +168,7 @@ "config.autofetch": "When set to true, commits will automatically be fetched from the default remote of the current Git repository. Setting to `all` will fetch from all remotes.", "config.autofetchPeriod": "Duration in seconds between each automatic git fetch, when `#git.autofetch#` is enabled.", "config.confirmSync": "Confirm before synchronizing Git repositories.", + "config.confirmCommittedDelete": "Confirm before deleting committed files with Git.", "config.countBadge": "Controls the Git count badge.", "config.countBadge.all": "Count all changes.", "config.countBadge.tracked": "Count only tracked changes.", diff --git a/extensions/git/src/commands.ts b/extensions/git/src/commands.ts index a0e362d56cf..d531ac92e49 100644 --- a/extensions/git/src/commands.ts +++ b/extensions/git/src/commands.ts @@ -1405,6 +1405,41 @@ export class CommandCenter { await commands.executeCommand('vscode.open', Uri.file(path.join(repository.root, to)), { viewColumn: ViewColumn.Active }); } + @command('git.delete') + async delete(uri: Uri | undefined): Promise { + const activeDocument = window.activeTextEditor?.document; + uri = uri ?? activeDocument?.uri; + if (!uri) { + return; + } + + const repository = this.model.getRepository(uri); + if (!repository) { + return; + } + + const allChangedResources = [ + ...repository.workingTreeGroup.resourceStates, + ...repository.indexGroup.resourceStates, + ...repository.mergeGroup.resourceStates, + ...repository.untrackedGroup.resourceStates + ]; + + // Check if file has uncommitted changes + const uriString = uri.toString(); + if (allChangedResources.some(o => pathEquals(o.resourceUri.toString(), uriString))) { + window.showInformationMessage(l10n.t('Git: Delete can only be performed on committed files without uncommitted changes.')); + return; + } + + await repository.rm([uri]); + + // Close the active editor if it's not dirty + if (activeDocument && !activeDocument.isDirty && pathEquals(activeDocument.uri.toString(), uriString)) { + await commands.executeCommand('workbench.action.closeActiveEditor'); + } + } + @command('git.stage') async stage(...resourceStates: SourceControlResourceState[]): Promise { this.logger.debug(`[CommandCenter][stage] git.stage ${resourceStates.length} `);