为GIT仓库项目设置独立配置参数

在Windows10上禁止将git凭证保存到控制面板的凭据管理器中

在 Windows 10 上彻底禁止 Git 将凭证(账号密码)保存到「控制面板 > 凭据管理器」中,核心是完全禁用 Git 的 credential.helper 凭据辅助配置(尤其是 wincred/manager-core 这类关联 Windows 凭据管理器的选项)。

步骤 1:检查当前凭据配置

git config --list --show-origin | findstr "credential.helper"

输出示例(不同层级的配置位置):

  • file:C:/Users/你的用户名/.gitconfig → 全局配置(最常见);
  • file:.git/config → 单个仓库的本地配置;
  • file:C:/Program Files/Git/etc/gitconfig → 系统级配置(需管理员修改)。

步骤 2:彻底移除 / 禁用所有层级的凭据配置

按「全局 → 本地 → 系统」顺序清理,确保无残留:

  1. 清理「全局配置」(对所有仓库生效,优先级最高)
# 移除全局的凭据辅助配置(推荐)
git config --global --unset-all credential.helper

# 强制置空(兜底,确保无残留)
git config --global credential.helper ""
  1. 清理「单个仓库的本地配置」(若有)

如果仅某个仓库配置了凭据保存,先进入该仓库目录,再执行:

# 替换为你的仓库路径(示例)
cd D:/Projects/your-git-repo
# 移除本地凭据配置
git config --local --unset-all credential.helper
git config --local credential.helper ""
  1. 清理「系统级配置」(需管理员终端)
# 移除系统级凭据配置
git config --system --unset-all credential.helper
# 强制置空
git config --system credential.helper ""

步骤 4:删除已保存的旧凭据(关键)

即使禁用了新凭证保存,已存入凭据管理器的旧凭证仍会生效,需手动删除:

  1. 打开「控制面板」→ 「用户账户」→ 「凭据管理器」;
  2. 切换到「Windows 凭据」标签;
  3. 找到所有以 git: 开头(或关联你的 Git 仓库地址,如 github.com、gitee.com)的凭据;
  4. 点击「删除」,确认移除。

步骤 5:验证禁用效果

运行以下命令,若无credential.helper相关输出(或输出为空),则配置生效:

git config --list | findstr "credential.helper"

设置凭证保存在项目本地

步骤 1:清理之前的配置

先移除之前写错的本地仓库配置,避免残留:

# 进入你的项目目录(替换为实际路径)
cd D:/Projects/your-git-repo

# 彻底删除本地仓库的 credential.helper 错误配置
git config --local --unset-all credential.helper

步骤 2:按终端类型重新正确配置

Windows 下需区分「PowerShell/CMD」和「Git Bash」,两者的引号 / 参数格式不同,分别给出正确命令:

场景 1:使用 PowerShell / CMD(Windows 原生终端)
必须用双引号包裹参数,正确命令:

# 核心正确配置:指定凭证存储到项目.git/credentials
git config --local credential.helper "store --file=.git/credentials"

场景 2:使用 Git Bash(Git 自带终端)
可使用单引号或双引号,正确命令:

# 方式1:单引号(推荐)
git config --local credential.helper 'store --file=.git/credentials'

# 方式2:双引号(也可)
git config --local credential.helper "store --file=.git/credentials"

步骤 3:验证配置是否正确
执行以下命令,检查输出是否和预期一致:

# 查看本地仓库的 credential.helper 配置
git config --local --get credential.helper

设置项目user.name和user.email

Git 的用户信息配置分 3 个层级(优先级:本地 > 全局 > 系统):

  • --local:仅当前项目生效(保存在项目 .git/config 文件中);
  • --global:对当前用户所有项目生效(保存在 C:/Users/你的用户名/.gitconfig);
  • --system:对系统所有用户生效(需管理员,极少用)。

为 Git 项目单独设置user.nameuser.email(仅对当前项目生效,不影响全局 / 其他项目),核心是使用git config --local命令(--local表示「本地仓库级别」,优先级高于全局配置)。

步骤 1:进入目标项目目录

打开 PowerShell/CMD/Git Bash,切换到需要单独配置的 Git 项目根目录(必须确保目录下有 .git 文件夹):

# 示例:替换为你的项目实际路径(PowerShell/CMD)
cd D:/Projects/your-git-repo

# Git Bash 路径写法(示例)
cd /d/Projects/your-git-repo

步骤 2:为当前项目设置专属的user.nameuser.email

执行以下命令,替换为你的自定义用户名和邮箱:

# 设置项目专属的用户名
git config --local user.name "你的项目专属用户名"

# 设置项目专属的邮箱
git config --local user.email "你的项目专属邮箱@xxx.com"

步骤 3:验证配置是否生效

查看当前项目的本地配置,确认user.nameuser.email已正确设置:

# 查看当前项目的所有本地配置(重点看 [user] 段)
git config --local --list

# 或单独查看用户名/邮箱
git config --local user.name
git config --local user.email
posted @ 2025-12-26 22:29  nuccch  阅读(3)  评论(0)    收藏  举报