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
相关推荐
realhuizhu1 天前
你的Git提交记录是“代码史诗”,还是“只有上帝能看懂的天书”?
git·版本控制·团队协作·ai工具·提示词工程
Mr YiRan1 天前
Git “cherry-pick“ 命令详解和应用场景
git
星月心城1 天前
git提交代码时所遇问题
大数据·git·elasticsearch
Dolphin_海豚1 天前
到底是选 merge 还是选 rebase
git·面试·程序员
云和数据.ChenGuang1 天前
采集Git相关日志(结合Filebeat)
大数据·git·elasticsearch
苹果电脑的鑫鑫1 天前
git如何撤销上次上传的内容
大数据·git·elasticsearch
Sapphire~1 天前
Git --- Local Changes Prevent from Pull
git
UX20171 天前
Git LFS 管理 Unity 大文件
git·unity
bad-Lz1 天前
git代码库管理
大数据·git·elasticsearch
YMGogre1 天前
GitHub 仓库管理员
git·github