以前部署的gitlab,采用ssh方式链接gitlab,在客户机上产生公钥上传到gitlab的SSH-Keys里,git clone下载和git push上传都没问题,这种方式很安全。git
后来应开发同事要求采用http方式链接gitlab,那么首先将project工程的“Visibility Level”改成“Public”公开模式,要保证gitlab的http端口已对客户机开放。web
后面发现了一个问题:
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 accessingssh
实例以下:
假设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.gitlab
上面能够看出,已经能成功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/refsfetch
fatal: HTTP request failedui
解决办法:
在代码的.git/config文件内[remote "origin"]的url的gitlab域名前添加gitlab注册时的“用户名:密码@”
另外发现这个用户要在对应项目下的角色是Owner或Master才行,若是是Guest、Reporter、Developer,则以下操做后也是不行。
以下,gitlab的用户名是wangshibo,假设密码是HU@wew12378!h8this
查看gitlab界面里的登录用户名:url
而后修改代码里的.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控制台里追踪到每一个操做者的提交记录了!