为什么在 Visual Studio Code 中 Git 无法工作?

10

我在我的项目中安装了husky。然后我运行了precommit命令。现在,当我运行git命令时,它不起作用,而是问我你想用什么打开这个文件。所以,我使用npm prune移除了husky,然后从项目目录的/.git中删除了hooks文件夹,但仍然没有帮助。我正在使用Windows 10。

8个回答

14

首先,在内置终端中键入:git --version

如果该命令无法正常工作,请下载并安装最新版本的Git。否则,在VS Code的扩展中,在搜索栏中键入:@builtin。这将列出VS Code中所有内置扩展,按不同部分进行分类。在“功能”部分中查找 Git 扩展程序。检查是否已禁用该扩展程序。启用它,你的版本控制应该就可以开始工作了。


7

在 VS Code 中取消勾选 Require Git User Config 设置。

Example


5
在阅读其他帖子并删除和重新安装Git和Github的答案后,我发现我之前进行实验时安装了Atom GitHub包。我删除了Atom并重新启动了VSCode,现在它可以使用Git存储库了。

1
关闭和打开VS Code对我有帮助。 - vinoth kumar

2

我遇到了一个类似的问题 - VS Code 与 Git 的配合出现了问题(例如:没有变更检测,从远程获取时出现问题),但我找不到导致这个问题的原因(我没有像 OP 那样安装 Husky)。

@user9795515's answer 的启发,我通过重新启动 VSC 中的 Git 功能解决了 Git 的问题:

  1. 进入 Extensions
  2. 在搜索栏中输入 @builtin
  3. 找到 Git 和 Git Base 功能并禁用它们
  4. 重启 VS Code
  5. 重新启用这两个功能

0

尝试从终端检查是否识别Git命令。 目前我正在使用Powershell终端。 git --version应该返回类似于git version 2.28.0.windows.1的内容。

如果这不起作用,请尝试以下步骤:

  1. 按照此链接VS Code文档中所述检查VS Code终端设置。
  2. 更新setting.json。我的设置应该是这样的。
"terminal.integrated.profiles.windows": {
        "PowerShell": {
            "source": "PowerShell",
            "args": ["-NoExit", "-ExecutionPolicy",  "Unrestricted", "-NoProfile", "-File", "C:\\Users\\LENOVO\\Documents\\WindowsPowerShell\\bootstrap-git.profile.ps1"],
            "icon": "terminal-powershell"
        },
        "Command Prompt": {
            "path": [
                "${env:windir}\\Sysnative\\cmd.exe",
                "${env:windir}\\System32\\cmd.exe"
            ],
            "args": [],
            "icon": "terminal-cmd"
        },
        "Git Bash": {
            "source": "Git Bash"
        }
    }
  1. 关闭终端,然后重新运行终端,检查是否能够识别 Git 命令,使用 git --version 命令。
  2. 在 Powershell 中添加额外的设置,将 Git 命令包含在 bootstrap-git.profile.ps1 文件中,该文件在第二步中使用。
# Start a transcript
#
if (!(Test-Path "$Env:USERPROFILE\Documents\WindowsPowerShell\Transcripts"))
{
    if (!(Test-Path "$Env:USERPROFILE\Documents\WindowsPowerShell"))
    {
        $rc = New-Item -Path "$Env:USERPROFILE\Documents\WindowsPowerShell" -ItemType directory
    }
    $rc = New-Item -Path "$Env:USERPROFILE\Documents\WindowsPowerShell\Transcripts" -ItemType directory
}
$curdate = $(get-date -Format "yyyyMMddhhmmss")
Start-Transcript -Path "$Env:USERPROFILE\Documents\WindowsPowerShell\Transcripts\PowerShell_transcript.$curdate.txt"

# Alias Git
#
New-Alias -Name git -Value "$Env:ProgramFiles\Git\bin\git.exe"

或者,如果您已经安装了Git,则尝试在VS Code中使用Git Bash终端。


0

我曾遇到同样的问题,但是我的 git --version 在命令提示符中可以使用。

我将 C:\Program Files\Git\binC:\Program Files\Git\cmd 设置到了 PATH 中(在 Windows 系统下 -> 环境变量 -> PATH),然后重新启动了命令提示符和 Visual Studio Code,问题得到了修复。


-1
重新安装git并将VS Code选择为git的默认编辑器。

-2

请在终端中运行git config --global safe.directory *


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接