You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows10下ShellJS执行Git提交命令返回退出码1,如何解决?

Windows10下ShellJS执行Git Commit命令失败(退出码1)的解决方法

1. 先验证Git命令本身的有效性

直接在Windows终端(CMD/PowerShell)里执行对应格式的Git提交命令,比如把代码里的变量替换为实际值:

git commit -m "🎉 feat : 新增用户模块"

如果手动执行也失败,先解决Git自身的问题:

  • 确认已有文件通过git add命令暂存
  • 检查Git全局配置的用户名和邮箱是否存在:
    git config --global user.name
    git config --global user.email
    
    若未配置,执行以下命令补充:
    git config --global user.name "你的用户名"
    git config --global user.email "你的邮箱"
    

2. 修复Windows下的引号解析问题

Windows终端对引号的解析逻辑和Unix系不同,原代码的双引号包裹方式可能导致命令解析错误,可尝试两种修改方案:

// 方案1:用单引号包裹提交信息
if (shell.exec(`git commit -m '${messageEmoji} ${messageType} : ${commitMsg}'`).code !== 0) {
    console.error('Error: Git commit failed');
    process.exit(1);
}

// 方案2:转义提交信息中的双引号(适合提交信息含单引号的场景)
const escapedCommitMsg = `${messageEmoji} ${messageType} : ${commitMsg}`.replace(/"/g, '\\"');
if (shell.exec(`git commit -m "${escapedCommitMsg}"`).code !== 0) {
    console.error('Error: Git commit failed');
    process.exit(1);
}

3. 开启错误输出排查具体原因

默认情况下ShellJS不会打印Git的错误详情,可通过{ silent: false }参数让错误信息显示,定位具体失败原因:

const result = shell.exec(`git commit -m "${messageEmoji} ${messageType} : ${commitMsg}"`, { silent: false });
if (result.code !== 0) {
    console.error('Error: Git commit failed');
    console.error('Git错误详情:', result.stderr);
    process.exit(1);
}

4. 确认脚本工作目录为Git仓库根目录

ShellJS默认继承Node进程的工作目录,若脚本不在Git仓库内执行会失败,可手动切换目录:

// 切换到你的Git仓库路径
shell.cd('D:/your-git-repo-folder');
// 再执行提交命令
const result = shell.exec(`git commit -m "${messageEmoji} ${messageType} : ${commitMsg}"`, { silent: false });
// ...后续错误判断逻辑

内容的提问来源于stack exchange,提问作者Soumya Sagar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 06:29:58