GitHub 命令行 git push 提交代碼時報錯:The requested URL returned error: 403

問題

使用 git 的命令行向 GitHub 提交的時候,報錯:

[root@toolsqa QaTools]# git remote add origin https://github.com/toolsqacn/qatoolsweb.git
[root@toolsqa QaTools]# git push -u origin master
error: The requested URL returned error: 403 Forbidden while accessing https://github.com/toolsqacn/qatoolsweb.git/info/refs

fatal: HTTP request failed

解決辦法

在項目根目錄中修改 .git 目錄下的config 配置

  1. 進入 .git 目錄
cd .git
# 執行後,可以用 ls 命令查看一下,會有一個 config 文件
  1. 修改 config 文件

找到 [remote “origin”] 節點下的 url 項,

url =https://github.com/toolsqacn/qatoolsweb.git

改成

url = ssh://[email protected]/toolsqacn/qatoolsweb.git

用戶名( toolsqacn )和 倉庫名(qatoolsweb.git) 修改成自己的 用戶名 和 倉庫名

  1. 重新提交
git push origin master

當有衝突要強制提交時使用

 git push --force origin master

本地 SSH Key 生成與設置

SSH Key 生成命令 ·ssh-keygen -t rsa -C‘

[root@toolsqa .ssh]# ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
7e:e8:79:ec:d1:e6:71:b1:59:50:45:ac:2d:b7:84:92 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|               o=|
|               ..|
|            . oo |
|           E .ooo|
|        S   . ooo|
|       . . .   * |
|        o.o + +  |
[core]
|       . oo+ o   |
|        oo. .    |
+-----------------+
[root@toolsqa .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub  known_hosts
[root@toolsqa .ssh]# cat *pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyZtF4mrpmsTm64uwlmAh4Ov+I5hPH8ECdkBj/W1b4yBKgpIPJQDmePYWVqpd2dJ4YLPiFDhtC37+TNyHWsT/ssEDll406sFQXhr235Hqr61+QNhR1BFB4fePRIf42T5fqiPR1sbDrvx+qWM3xka5nieEHQbNK/vkcXGt2KiFpbbXtbQEbxFDFRanbitcZ9+lbIXvnFT73hCoEaaFd5hNduKd+c9IY2SiLMySU76UuREjLbemY3vto3wSJEYVmeItsuwAolJSZaz9UUaYxzezmDs85rWRWr4AvM8zzRzswAMz+KRIbcVe+EXu1XiGW+5WebFZ05e31IFiqjfdH/d8Sw== [email protected]

GitHub SSH Key 設置

設置地址爲:https://github.com/settings/ssh/new
自定義 SSH Key 名稱和填入本地生成的Key
界面如圖:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章