之前部署的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
[[email protected] ~]# mkdir /root/git
[[email protected] ~]# cd /root/git
[[email protected] git]# git init .
[[email protected] 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下代码
[[email protected] git]# ll
total 4
drwxr-xr-x. 3 root root 4096 Nov 30 15:58 weixin
[[email protected] git]# cd weixin/
[[email protected] 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
[[email protected] weixin]# git rm heihei
[[email protected] weixin]# touch test.file
[[email protected] weixin]# echo "123456" > test.file
[[email protected] weixin]# git add .
[[email protected] weixin]# git commit -m "this is a test"
[[email protected] 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注册时的“用户名:密码@”
如下,gitlab的用户名是wangshibo,假设密码是[email protected]!h8
查看gitlab界面里的登陆用户名:
然后修改代码里的.git/config文件
[[email protected] weixin]# cd .git
[[email protected] .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
修改如下:
[[email protected] .git]# cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = http://wangshibo:[email protected][email protected]git.wangshibo.net:8081/weixin/weixin.git
[branch "master"]
remote = origin
merge = refs/heads/master