git push上傳代碼到gitlab上,報錯401/403(或需要輸入用戶名和密碼)


 

之前部署的gitlab,采用ssh方式連接gitlab,在客戶機上產生公鑰上傳到gitlab的SSH-Keys里,git clone下載和git push上傳都沒問題,這種方式很安全。

后來應開發同事要求采用http方式連接gitlab,那么首先將project工程的“Visibility Level”改為“Public”公開模式,要保證gitlab的http端口已對客戶機開放。

后面發現了一個問題:
http方式連接gitlab后,git clone下載沒有問題,但是git push上傳有報錯:
error: The requested URL returned error: 401 Unauthorized while accessing http://git.xqshijie.net:8081/weixin/weixin.git/info/refs
fatal: HTTP request failed

或者
The requested URL returned error: 403 Forbidden while accessing

實例如下:
假設git的url為http://git.wangshibo.net
[root@test-huanqiu ~]# mkdir /root/git
[root@test-huanqiu ~]# cd /root/git
[root@test-huanqiu git]# git init .
[root@test-huanqiu git]# git clone http://git.wangshibo.net:8081/weixin/weixin.git
Initialized empty Git repository in /root/git/weixin/.git/
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 10 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (10/10), done.

上面可以看出,已經能成功git clone下代碼
[root@test-huanqiu git]# ll
total 4
drwxr-xr-x. 3 root root 4096 Nov 30 15:58 weixin
[root@test-huanqiu git]# cd weixin/
[root@test-huanqiu weixin]# ll
total 8
-rw-r--r--. 1 root root 15 Nov 30 15:58 heihei
-rw-r--r--. 1 root root 1 Nov 30 15:38 README.md

現在測試下git push
[root@test-huanqiu weixin]# git rm heihei
[root@test-huanqiu weixin]# touch test.file
[root@test-huanqiu weixin]# echo "123456" > test.file
[root@test-huanqiu weixin]# git add .
[root@test-huanqiu weixin]# git commit -m "this is a test"
[root@test-huanqiu weixin]# git push                                      //或者git push -u origin master
error: The requested URL returned error: 401 Unauthorized while accessing http://git.wangshibo.net:8081/weixin/weixin.git/info/refs

fatal: HTTP request failed

解決辦法:
在代碼的.git/config文件內[remote "origin"]的url的gitlab域名前添加gitlab注冊時的“用戶名:密碼@
另外發現這個用戶要在對應項目下的角色是Owner或Master才行,如果是Guest、Reporter、Developer,則如下操作后也是不行。
如下,gitlab的用戶名是wangshibo,假設密碼是HU@wew12378!h8

查看gitlab界面里的登陸用戶名:

然后修改代碼里的.git/config文件
[root@test-huanqiu weixin]# cd .git
[root@test-huanqiu .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://git.wangshibo.net:8081/weixin/weixin.git
[branch "master"]
remote = origin
merge = refs/heads/master

修改如下:
[root@test-huanqiu .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://wangshibo:HU@wew12378!h8@git.wangshibo.net:8081/weixin/weixin.git
[branch "master"]
remote = origin
merge = refs/heads/master

然后再次git push,發現可以正常提交了!
[root@test-huanqiu weixin]# git push
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 297 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://wangshibo:HUIhui1987521@git.xqshijie.net:8081/weixin/weixin.git
8fcb559..6c97b56 master -> master

------------------------------------------------------------------------------------------------------------------------------------
可以創建一個用戶名作為admin管理員,然后將這個用戶名和密碼添加到項目代碼的.git/config里面,如上操作!
如果不是管理員,則至少對當前代碼具有owner或master權限。

這樣,在.git/config文件里添加這個用戶名和密碼權限,然后其他人在git push的時時候都使用這個文件進行覆蓋。

其他人在首次git clone下載代碼的時候,需要進行--global全局配置,然后就可以在gitweb控制台里追蹤到每個操作者的提交記錄了!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM