GitHub使用
GitHub使用
前言
- 本文为个人在使用GitHub上传项目时,遇到并解决的一些问题,作为笔记记录
问题背景
由于国内使用github,网络背景较为复杂,简要介绍下问题的背景如下:
在学会自建消防云梯前,曾在大局域网下,使用过账号和密码校验使用github,由于小鬼子的封锁日益严密,严重阻碍我的学习,迫使我购买消防车&云梯
自建消防云梯,web+ws+tls+cdn,并开启云梯
之前曾使用过gitee,也曾在github上使用过另一个账号
切换为当前github账号使用,当前已通过git config --global --edit设置为自己的账号名字和邮箱
- 通过
git config --global user.name "用户名"
命令设置的用户名,三个字更改为你在GitHub上用户名,即头像隔壁的名字- 设置后,确保通过
git config user.name
显示出来的用户名,与你GitHub上用户名一致
- 设置后,确保通过
- GitHub上中,启用email privacy后:
- 请到 https://github.com/settings/emails 页面中找到你的邮箱地址
- 启用email privacy后(即勾选了Keep my email addresses private),那么你的邮箱地址格式为
那串完整的加粗的字体,即:“数字+GitHub用户名@users.noreply.github.com”
- 设置后,确保通过
git config user.email
显示出来的邮箱,与上述加粗字体一致
- 通过
开源项目xxxx从github下载,打算自己先作修改,再上传到自己建的仓库中,后续个人定制化修改
问题情况及解决步骤
背景 要掌握的操作命令
查看 当前电脑上的GitHub配置用户名 git config user.name
查看 当前电脑上的GitHub配置用户邮箱 git config user.email
查看 当前电脑上的GitHub配置 git config --list
以vim修改 当前电脑上的GitHub配置 git config --global --edit
在电脑上切换GitHub账号;修改用户名、邮箱、密码的命令 git config --global user.name "用户名" git config --global user.email "邮箱" git config --global user.password= "密码"
问题1 设置远端仓库
prompt:我有一个本地的项目xxxx,已在本地提交产生版本记录,本地的版本名为20250115,现在我该如何将项目上传github呢
GPT回答:
要将已有的本地项目上传到 GitHub,并且保留已有的提交历史和版本记录,可以按照以下步骤操作:
步骤一:在 GitHub 创建一个新的远程仓库
登录 GitHub,点击右上角的 + 图标,选择 New repository 填写项目名称和其他信息,点击 Create repository 记住生成的远程仓库地址,例如 https://github.com/yourusername/yourrepository.git
步骤二:将本地项目连接到 GitHub
在本地项目目录中初始化 Git(如果尚未初始化) git init 如果你的项目已经是一个 Git 仓库,可以跳过这一步。
设置远程仓库地址 git remote add origin https://github.com/yourusername/yourrepository.git 使用你在 GitHub 上生成的远程仓库地址替换 https://github.com/yourusername/yourrepository.git。
步骤三:将本地版本推送到远程仓库
检查当前分支,通常,默认分支为 master 或 main。查看当前分支: git branch 如果需要切换到 main 分支: git checkout main 或者,如果使用的是 master 分支,执行: git checkout master
将本地版本推送到 GitHub: git push -u origin main 或者,如果是 master 分支: git push -u origin master
如果需要标记版本号(例如 20250115)可以用 Git 标签功能标记版本: git tag 20250115 git push origin 20250115
步骤四:验证上传结果 访问 GitHub 上的项目页面,检查代码和提交记录是否已成功上传。 检查标签是否存在,可以在 GitHub 的 Releases 中查看。
常见问题 远程仓库冲突:如果远程仓库已有初始化文件(如 README.md),可能会出现冲突提示。可以先拉取远程更改: git pull origin main --allow-unrelated-histories git push origin main 认证错误:确保你使用了正确的 GitHub 账户和访问令牌(而不是密码)来认证。
问题2 协议报错
prompt:在执行git push -u origin main后,出现以下报错,是哪里的问题呢 fatal: unable to access 'https://github.com/xxxxxxuser/xxxxgit上的项目名字.git/': OpenSSL/3.2.1: error:0A00010B:SSL routines::wrong version number
GPT回答:
该错误信息通常与 OpenSSL 版本问题 或 网络代理设置问题 有关。下面是解决方法:
- 检查 Git 和 OpenSSL 版本 1.1 确认 Git 版本是否兼容 使用以下命令查看当前 Git 版本:
git --version 如果版本过低,建议更新到最新稳定版本。下载最新版本:Git 官网。
1.2 检查 OpenSSL 版本 openssl version 确保 OpenSSL 是兼容的版本。如果版本异常,可以尝试更新 OpenSSL。
- 解决方法
2.1 强制使用 HTTPS/HTTP2 兼容版本 编辑 Git 配置,强制使用兼容的 SSL/TLS 版本: git config --global http.version HTTP/1.1
如果此方法无效,可以尝试禁用 HTTP2: git config --global http.version HTTP/1.0
2.2 检查代理配置 有时候代理设置错误会导致 SSL 问题。
查看代理设置: git config --global --get http.proxy 如果显示了代理,清除它: git config --global --unset http.proxy git config --global --unset https.proxy
2.3 确保 Git 使用了系统的 CA 证书 设置 Git 以使用正确的证书: git config --global http.sslbackend openssl git config --global http.sslverify true
- 临时回避方案 如果以上方法未解决,可以尝试切换到 SSH 方式推送:
确保生成了 SSH 密钥对并添加到 GitHub。
使用以下命令将远程地址切换为 SSH: git remote set-url origin git@github.com:xxxxxxuser/xxxxgit上的项目名字.git 再次推送: git push -u origin main
- 确保网络环境正常 网络代理、VPN 或网络运营商问题也可能导致此错误。尝试更换网络或关闭代理服务。
- 按gpt建议,执行git config --global http.version HTTP/1.1或者git config --global http.version HTTP/1.0,的确没有这个报错了,但是又会有新的报错
remote: Repository not found. fatal: repository 'https://github.com/xxxxxxuser/xxxxgit上的项目名字.git' not found
问题3 用户权限和身份验证报错
新的报错 remote: Repository not found. 表明无法找到你指定的远程仓库,这通常是以下原因之一导致的:
检查与解决方法
确认远程仓库地址正确 确保使用了正确的仓库地址。请访问 你的 GitHub 仓库 页面,复制项目的 HTTPS 地址,然后执行以下命令重新设置远程地址: git remote set-url origin https://github.com/你的用户名/你的仓库名.git 例如: git remote set-url origin https://github.com/xxxxxxuser/xxxxgit上的项目名字.git
检查用户权限和身份验证 如果你没有权限访问指定仓库或使用了错误的用户身份信息,也会导致此问题。
确保登录了正确的 GitHub 账户,并具有推送权限。 使用 git config --global user.name 和 git config --global user.email 检查配置的用户名和邮箱是否正确。 使用 GitHub 的个人访问令牌代替密码来验证,令牌生成方法:GitHub 令牌生成。 3. 验证远程仓库的名称拼写 确保仓库名称是 xxxxgit上的项目名字,没有大小写或拼写错误。
- 检查仓库的私有或公开状态 如果仓库是私有的,你必须确保使用了拥有权限的账户访问。
测试连接与推送 确认远程仓库地址设置正确后,使用以下命令查看远程信息: git remote -v 再执行推送: git push -u origin main
- 按gpt建议,执行后仍报错账号权限相关错误
remote: Permission to xxxxxxuser/xxxxgit上的项目名字.git denied to user账号B. fatal: unable to access 'https://github.com/xxxxxxuser/xxxxgit上的项目名字.git': The requested URL returned error: 403
问题来了,这个报错是报错的我的GitHub账户B的报错,明明已经设置了全局账号A的信息xxxxxxuser,但是还是显示账号B权限报错
为了先让项目上传,只能到GitHub的账户A下,将项目设为公开
问题4 切换账号
prompt:我曾在电脑上使用过另一个github账号 user账号B,如何切换以使用现在的这个xxxxxxuser账号呢
GPT回答:
要解决 GitHub 使用错误账号的问题,你需要切换到正确的 xxxxxxuser 账号。以下是详细步骤:
- 清除旧的 GitHub 凭据 Git 会缓存之前使用的凭据,首先清除旧的 user账号B 凭据。
Windows 打开 控制面板 > 凭据管理器。 找到 Windows 凭据 或 通用凭据 中与 github.com 相关的条目。 删除相关条目。
macOS 打开 钥匙串访问。 搜索 github.com。 删除与 GitHub 相关的条目。
Linux 如果使用 git-credential-cache,清除缓存: git credential-cache exit
- 配置正确的 GitHub 凭据
下次推送时,Git 会提示输入用户名和密码(或令牌)。
生成并使用 GitHub 个人访问令牌(PAT) 由于 GitHub 已不再支持使用密码,需生成访问令牌:
进入 GitHub 个人访问令牌页面。 生成新令牌,选择适当权限(如 repo)。 使用令牌代替密码。
确保使用正确的用户配置 重新配置 Git 使用正确的用户信息: git config --global user.name "xxxxxxuser" git config --global user.email "xxxxxxuser@users.noreply.github.com"
测试连接 使用以下命令测试连接: git push -u origin main 当 Git 提示时,输入 xxxxxxuser 的用户名,并使用生成的访问令牌代替密码。
确保远程地址正确 如果远程地址绑定错误的账号,重新设置: git remote set-url origin https://github.com/xxxxxxuser/xxxxgit上的项目名字.git
- 但实际上,按照gpt做法,并没有成功切换了我的本地电脑上的GitHub账号,在网页上,可以很轻易切换GitHub的登录账号,但是在项目提交上,很难切换
通过执行 git config --global http.sslbackend openssl 以及执行 git config --global http.sslverify true
此时git push -u origin main的报错变为 ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repository.
连上了,但是使用的却是我的账号B,只能先搁置切换
问题5 端口切换
因为使用了消防云梯,只能走443了
prompt:如何解决这个报错呢 ssh: connect to host github.com port 22: Connection timed out fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
- GPT回答:
ssh: connect to host github.com port 22: Connection timed out 是 SSH 连接问题,通常由于网络限制或配置错误导致。以下是解决方法:
- 检查 SSH 连接 首先,使用以下命令测试是否能连接到 GitHub: ssh -T git@github.com
如果提示 Permission denied,说明 SSH 密钥配置有问题。 如果提示 timeout,说明端口 22 被阻止。
- 切换到端口 443 有些网络环境禁止了端口 22,但允许 HTTPS 端口 443,可以让 SSH 使用 443。
修改 SSH 配置 打开或创建 ~/.ssh/config 文件: nano ~/.ssh/config # 或者用其他文本编辑器
添加以下内容: Host github.com Hostname ssh.github.com Port 443 保存并退出。
- 检查和更新 SSH 密钥 检查已有的 SSH 密钥 ls ~/.ssh/id_*
如果存在 .pub 文件,表示已有公钥。
生成新的 SSH 密钥(如果没有) ssh-keygen -t ed25519 -C "your_email@example.com"
按提示操作,生成 id_ed25519 和 id_ed25519.pub。
添加密钥到 GitHub 复制公钥内容: cat ~/.ssh/id_ed25519.pub
打开 GitHub SSH 密钥设置。
添加新密钥,粘贴公钥。
- 确保使用 SSH 远程地址 切换远程地址为 SSH: git remote set-url origin git@github.com:yourusername/yourrepository.git
用正确的用户名和仓库名替换 yourusername/yourrepository.git。
检查防火墙和网络限制 如果你在公司或学校网络,端口可能被阻止。尝试切换网络或使用 VPN。 确保防火墙允许 ssh.github.com 端口 443。
重试连接 ssh -T git@github.com git push -u origin main 这应该能解决超时问题并成功推送。
- 按照git的说法,在本地电脑作密钥,然后复制到GitHub上验证,但实际上,执行了以后是不成功的
问题6 ssh密钥校验
prompt:ssh -T git@github.com执行后报错 The authenticity of host '[ssh.github.com]:443 ([github服务器的ip]:443)' can't be established. ED25519 key fingerprint is SHA256:+DiY3wv这个密钥与粘贴本地的ssh密钥到与GitHub后返回的根本不是同一个UvCOqU. This host key is known by the following other names/addresses: ~/.ssh/known_hosts:1: github.com Are you sure you want to continue connecting (yes/no/[fingerprint])? Host key verification failed.应该如何解决呢
GPT回答: Host key verification failed. 表示 SSH 连接时,远程主机密钥不匹配或未被信任。这是一个安全警告,通常可以通过以下方法解决:
- 解决方法 1.1 删除旧的 GitHub SSH 主机密钥 删除 ~/.ssh/known_hosts 文件中与 GitHub 相关的旧条目。
编辑 known_hosts 文件: nano ~/.ssh/known_hosts 找到包含 github.com 的行,删除该行。 保存并退出。
或使用以下命令直接删除第一行(根据错误提示中的 known_hosts:1): sed -i '' '1d' ~/.ssh/known_hosts
1.2 使用 ssh-keyscan 更新 GitHub 主机密钥 自动获取并添加最新的 GitHub 主机密钥: ssh-keyscan -p 443 ssh.github.com >> ~/.ssh/known_hosts
重新尝试连接 ssh -T git@github.com
手动确认(如果再次提示) 运行连接命令时,输入 yes 以信任新的主机密钥: Are you sure you want to continue connecting (yes/no)? yes
验证解决 完成上述操作后,你应该可以成功建立 SSH 连接并执行 git push。
- 按照gpt说法,我又删掉了本地电脑上的密钥,改为从GitHub上请求密钥(即使用 ssh-keyscan 更新 GitHub 主机密钥),此时能连接上了,也能提交了,但是电脑上是使用的账号B去给账号A下的项目作的提交
问题7 回到账号切换
- prompt:
按照上述操作,虽然能成功连接到GitHub并上传项目,但是我发现了问题:
在https://github.com/userA 这个地址下,我能看到我的GitHub账号A下的这个项目xxxxgit上的项目名字
但是,按照上述 ssh-keyscan -p 443 ssh.github.com >> ~/.ssh/known_hosts操作后,再执行 ssh -T git@github.com,再执行git push -u origin main
得到的效果却是:账号A下的项目,由我的账号B所commit提交,但是我在电脑上,通过git config --list查看到的信息都是账号A的信息
如何将我本地的电脑,当前所使用的GitHub账号B,切换为GitHub账号A呢
- GPT回答:
要将本地的 Git 配置从使用账号 B 切换回账号 A 并确保提交记录显示正确的用户,可以从以下几个步骤修正:
- 检查和更新本地 Git 用户信息 Git 使用 user.name 和 user.email 来标识提交者,你需要确保这些信息与账号 A 对应。
查看当前配置 git config --global --list 设置为账号 A 的信息
git config --global user.name "AccountAUsername" git config --global user.email "AccountAEmail@example.com"
如果想只对某个项目生效,进入项目目录并使用: git config user.name "AccountAUsername" git config user.email "AccountAEmail@example.com"
- 检查 SSH 密钥配置 不同的 GitHub 账号使用不同的 SSH 密钥。为避免使用账号 B 的 SSH 密钥,确保当前 SSH 配置使用账号 A 的密钥。
创建或选择账号 A 的密钥 生成或确认现有的 SSH 密钥: ssh-keygen -t ed25519 -C "AccountAEmail@example.com" 备注:AccountAEmail@example.com替换为加粗的一串数字+GitHub用户名@users.noreply.github.com
将生成的公钥添加到 GitHub 账号 A 的 SSH 设置中。
创建 SSH 配置文件 编辑或创建 ~/.ssh/config: nano ~/.ssh/config 添加以下内容,确保使用账号 A 的密钥: Host github.com-A HostName github.com User git IdentityFile ~/.ssh/id_account_a 将远程地址设置为 SSH: git remote set-url origin git@github.com-A:AccountAUsername/Gemini2025ProChat.git
清除或更新缓存的凭据 清除旧凭据缓存,以便下次推送时使用正确的 SSH 密钥: git credential-cache exit
重新验证 执行以下命令以确保配置生效: ssh -T git@github.com-A 正确显示账号 A 的确认信息。
更新提交历史(如果必要) 如果之前的提交已记录了错误的用户名或邮箱,可以使用 git rebase 或 git filter-branch 修改历史提交: git commit --amend --author="Account A AccountAEmail@example.com" git push --force 这样,你应该能够成功切换到账号 A,并确保提交记录和身份验证都使用正确的 GitHub 账号。
- 按照GPT提示,我又在本地电脑上,以覆盖性的方式,生成了一次新的密钥,但是报错如下:
执行命令: ssh -T git@github.com-用户A的名字 报错: ssh: connect to host github.com port 22: Connection timed out
问题8 自定义 SSH 配置文件
- prompt:
当执行到上述第四步的时候,报错ssh: connect to host github.com port 22: Connection timed out该如何解决呢
- GPT回答:
由于你使用的是自定义 SSH 配置文件,并且之前遇到了端口 22 连接超时的问题,可以将 Host github.com-A 配置为使用端口 443 来解决这个问题。
修改 ~/.ssh/config 配置 将配置更新为以下内容以使用端口 443:
Host github.com-A HostName ssh.github.com Port 443 User git IdentityFile ~/.ssh/id_account_a
其中 IdentityFile 指向你的账号 A 的 SSH 密钥文件(例如 ~/.ssh/id_ed25519_account_a)。
确保配置已正确保存 使用以下命令来验证 SSH 配置文件语法: ssh -T git@github.com-A
如果配置正确,你应该看到类似如下提示: Hi AccountAUsername! You've successfully authenticated, but GitHub does not provide shell access.
更新远程地址以使用新配置 确保你的远程仓库地址也使用了 Host github.com-A,运行以下命令: git remote set-url origin git@github.com-A:AccountAUsername/Gemini2025ProChat.git
这样,你的 SSH 连接应该能使用端口 443,并成功切换到账号 A。
- 按照GPT提示:
- 先执行
cat ~/.ssh/id_ed25519.pub
拿到这个最新的密钥
- 先执行
- 到 https://github.com/settings/keys GitHub页上,将你的ssh密钥加进去,点
New SSH key
,粘贴进去cat ~/.ssh/id_ed25519.pub
查到的结果,名字上写好这个是账号A的ssh密钥
- 到 https://github.com/settings/keys GitHub页上,将你的ssh密钥加进去,点
- 执行
vim ~/.ssh/config
(gpt是建议用的nano,但我熟悉使用vim),到自定义SSH 配置文件~/.ssh/config
中,作如下修改:
- Host github.com-后面跟你的GitHub账号A名字
- HostName ssh.github.com
- Port 443
- User git
- IdentityFile ~/.ssh/你账号A的密钥的实际路径和实际密钥的名字
- 默认就是:IdentityFile ~/.ssh/id_ed25519
- 重点:
你账号A的密钥的实际路径和实际密钥的名字
按实际情况写
- 执行
- 此时再执行
ssh -T git@github.com-后面跟你的GitHub账号A名字
,能成功返回信息了
- Hi 账号A名字! You've successfully authenticated, but GitHub does not provide shell access.
- 切记,要记得执行一下
git remote set-url origin git@github.com-账号A名字:账号A名字/你的项目名字.git
- 此时再执行
- 后续远程commit就是使用的账号A,问题解决,验证:
- 修改文件,然后
git add .
完了再git commit -m '设置了默认密码6'
,再git log --oneline
查看本地的版本情况 git remote -v
查看远程仓库的名字及地址,git remote remove 仓库名字c
删掉之前弄错的仓库名字c的远程仓库及地址- 通过
git push -u 远程分支名字 main
执行远程提交,默认的远程分支名字为origin,也就是默认执行git push -u origin main
- 回到前一次GPT的回答:更新提交历史(如果必要)
- 执行
git commit --amend --author="GitHub账号A名字<数字+GitHub账号A名字@users.noreply.github.com>"
- 执行
git push --force
最终的本地配置(账号A)
运行 git config --global --edit 显示
[filter "lfs"]
process = git-lfs filter-process
required = true
clean = git-lfs clean -- %f
smudge = git-lfs smudge -- %f
[user]
name = xxxxxxuser用户a
email = xxxxxxuser用户a@users.noreply.github.com
[credential "https://gitee.com"] #之前曾使用过gitee
provider = generic
[http]
proxy = https://127.0.0.1:10809
version = HTTP/1.0
sslbackend = openssl
sslverify = true
[https]
proxy = https://127.0.0.1:10809
[http "https://github.com"]
proxy = socks5://127.0.0.1:10808
[https "https://github.com"]
proxy = socks5://127.0.0.1:10808
[credential]
helper = cache
运行 git config --global --list 显示
git config --global --list
filter.lfs.process=git-lfs filter-process
filter.lfs.required=true
filter.lfs.clean=git-lfs clean -- %f
filter.lfs.smudge=git-lfs smudge -- %f
user.name=xxxxxxuser用户a
user.email=xxxxxxuser用户a@users.noreply.github.com
credential.https://gitee.com.provider=generic
http.proxy=https://127.0.0.1:10809
http.version=HTTP/1.0
http.sslbackend=openssl
http.sslverify=true
https.proxy=https://127.0.0.1:10809
http.https://github.com.proxy=socks5://127.0.0.1:10808
https.https://github.com.proxy=socks5://127.0.0.1:10808
credential.helper=cache
备注
如何修改GitHub的账户名字呢
要修改 GitHub 账户名,请按照以下步骤操作:
转到你的个人资料页面。 单击右上角的个人资料图片。 选择“设置”。 在“账户”部分,单击“更改用户名”。 输入你的新用户名。 单击“更新用户名”。 注意:
你的新用户名必须符合以下要求: 长度在 1 到 39 个字符之间 只包含字母、数字和连字符 (-) 不能以连字符开头或结尾 不能包含空格 你的新用户名不能与现有用户名的任何部分相同。 更改用户名后,你的旧用户名将不可用。 更改用户名不会影响你的存储库、问题或拉取请求。 一旦你更新了你的用户名,它将在 GitHub 上的所有地方生效,包括你的个人资料、存储库和提交。
提示:
在更改用户名之前,请考虑备份你的本地存储库。 如果你正在使用 GitHub Desktop,你可能需要注销并重新登录才能看到你的新用户名。