Git登录配置的详细方法

发布于:2025-08-18 ⋅ 阅读:(15) ⋅ 点赞:(0)

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

1. 配置基本用户信息

# 全局配置用户名和邮箱
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等平台:

# 设置凭据缓存(避免重复输入密码)
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密钥:

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

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

添加SSH密钥到ssh-agent:

# 启动ssh-agent
eval "$(ssh-agent -s)"

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

添加公钥到远程平台:

  1. 复制公钥内容:
    cat ~/.ssh/id_rsa.pub
    # 或
    cat ~/.ssh/id_ed25519.pub
    
  2. 在GitHub/GitLab的设置中添加SSH密钥

测试SSH连接:

ssh -T git@github.com
ssh -T git@gitlab.com

4. 查看当前配置

# 查看所有配置
git config --list

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

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

5. 切换远程仓库地址

# 从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,运行:

cat ~/.ssh/id_rsa.pub

网站公告

今日签到

点亮在社区的每一天
去签到