Git登录配置的详细方法

Git登录绑定主要涉及配置用户信息和设置身份验证。以下是详细的配置方法:

1. 配置基本用户信息

bash 复制代码
# 全局配置用户名和邮箱
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱@example.com"

# 或者为单个仓库配置(在项目目录下)
git config user.name "你的用户名"
git config user.email "你的邮箱@example.com"

2. HTTPS方式登录绑定

GitHub/GitLab等平台:

bash 复制代码
# 设置凭据缓存(避免重复输入密码)
git config --global credential.helper store

# 或者设置缓存时间(15分钟)
git config --global credential.helper cache
git config --global credential.timeout 900

使用Personal Access Token:

  1. 在GitHub/GitLab等平台生成Personal Access Token
  2. 首次push时输入用户名和token(而非密码)

3. SSH方式登录绑定

生成SSH密钥:

bash 复制代码
# 生成SSH密钥对
ssh-keygen -t rsa -b 4096 -C "你的邮箱@example.com"

# 或使用ed25519算法(推荐)
ssh-keygen -t ed25519 -C "你的邮箱@example.com"

添加SSH密钥到ssh-agent:

bash 复制代码
# 启动ssh-agent
eval "$(ssh-agent -s)"

# 添加私钥到ssh-agent
ssh-add ~/.ssh/id_rsa
# 或
ssh-add ~/.ssh/id_ed25519

添加公钥到远程平台:

  1. 复制公钥内容:

    bash 复制代码
    cat ~/.ssh/id_rsa.pub
    # 或
    cat ~/.ssh/id_ed25519.pub
  2. 在GitHub/GitLab的设置中添加SSH密钥

测试SSH连接:

bash 复制代码
ssh -T git@github.com
ssh -T git@gitlab.com

4. 查看当前配置

bash 复制代码
# 查看所有配置
git config --list

# 查看用户信息
git config user.name
git config user.email

# 查看远程仓库地址
git remote -v

5. 切换远程仓库地址

bash 复制代码
# 从HTTPS切换到SSH
git remote set-url origin git@github.com:用户名/仓库名.git

# 从SSH切换到HTTPS
git remote set-url origin https://github.com/用户名/仓库名.git

建议

  • SSH方式更安全且便捷(一次配置,长期使用)
  • HTTPS方式简单直接,但需要token或频繁输入密码
  • 企业环境通常推荐SSH方式

找到SSH设置的正确路径:

方法1:通过用户菜单

  1. 点击右上角的头像(图片中显示"yz123luchy"的地方)
  2. 在下拉菜单中选择 "Settings"
  3. 在左侧边栏找到 "SSH and GPG keys"
  4. 点击 "New SSH key" 按钮

方法2:直接访问链接

直接在浏览器中输入:

复制代码
https://github.com/settings/keys

详细步骤图解:

复制代码
GitHub首页 → 点击右上角头像 → Settings → 左侧菜单"SSH and GPG keys" → "New SSH key"

添加SSH密钥时需要的信息:

  1. Title:给这个密钥起个名字(比如:"My PC"、"Home Computer"等)
  2. Key:粘贴你之前生成的公钥内容

获取公钥内容的命令:

回到Git Bash,运行:

bash 复制代码
cat ~/.ssh/id_rsa.pub
相关推荐
Geek-Chow3 小时前
Git 标签与发布流程:轻量标签、附注标签与签名标签
git·tags
wear工程师3 小时前
Git 冲突怎么解决:别只删标记,先看懂暂存区的 1、2、3 阶段
git
村雨遥3 小时前
覆盖提交不等于删除,这才是彻底清除 Git 历史的正确姿势
git·github
淮北4944 小时前
ubuntu22 默认输入法调整频率
运维·服务器·git·ubuntu·elasticsearch
dunge20266 小时前
2026 ChatGPT Plus / Pro + Codex 实战:从 0 搭一套 AI 编程项目模板,AGENTS.md + Git + 测试一次配好
人工智能·git·chatgpt
福如意如我心意1 天前
git清除仓库敏感文件并同步至远端的操作
git
Brilliantwxx1 天前
【Linux】 开发|Git 版本控制与 gdb/cgdb 调试
linux·笔记·git
.Hypocritical.2 天前
Git 全套实操|配置 /.gitignore/ 分支合并 / 冲突解决 /git stash 完整教程
git
来日方长。。。。long2 天前
Hermes Agent橙皮书共读|第三篇:保姆级实战部署|本地/VPS从零搭建Hermes
ide·git·hermes
其实防守也摸鱼2 天前
VS Code Git 工作树:解锁多分支并行开发新体验
数据库·git·安全·oracle·架构·自动化