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控制台里追踪到每个操作者的提交记录了!

***************当你发现自己的才华撑不起野心时,就请安静下来学习吧***************
本文转自散尽浮华博客园博客,原文链接:http://www.cnblogs.com/kevingrace/p/6118297.html ,如需转载请自行联系原作者
相关文章
|
10天前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
37 18
|
9天前
|
安全 开发工具 git
git分布式版本控制系统及在码云上创建项目并pull和push
通过本文的介绍,我们详细讲解了Git的基本概念和工作流程,并展示了如何在码云上创建项目及进行pull和push操作。Git作为一种分布式版本控制系统,为开发者提供了强大的工具来管理代码变更和协作开发。希望本文能帮助您更好地理解和使用Git及码云,提高开发效率和代码质量。
29 16
|
1月前
|
Devops Shell 网络安全
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
86 16
git使用之如何将一套代码同时推送至github|gitee|gitcode|gitlab等多个仓库-含添加ssh-优雅草央千澈完美解决-提供整体提交代码
|
6月前
|
开发工具 数据安全/隐私保护 git
记录一次使用git工具拉取coding上代码密码账号错误的经历
这篇文章记录了作者在使用Git工具从Coding平台克隆项目时遇到的账号密码错误问题,并分享了通过清除电脑凭证中错误记录的账号密码来解决这个问题的方法。
记录一次使用git工具拉取coding上代码密码账号错误的经历
|
3月前
|
网络安全 开发工具 git
解决fatal:remote error:You can’t push to git://github.com/username/*.g
通过上述诊断与修复步骤,绝大多数的推送错误都能得到有效解决,确保您的Git工作流顺畅无阻。
302 1
|
5月前
|
开发工具 git
GIT:如何合并已commit的信息并进行push操作
通过上述步骤,您可以有效地合并已提交的信息,并保持项目的提交历史整洁。记得在执行这些操作之前备份当前工作状态,以防万一。这样的做法不仅有助于项目维护,也能提升团队协作的效率。
220 4
|
5月前
|
开发工具 git
GIT:如何合并已commit的信息并进行push操作
通过上述步骤,您可以有效地合并已提交的信息,并保持项目的提交历史整洁。记得在执行这些操作之前备份当前工作状态,以防万一。这样的做法不仅有助于项目维护,也能提升团队协作的效率。
316 3
|
6月前
|
开发工具 git 开发者
|
存储 开发工具 数据安全/隐私保护
https 方式使用git@osc设置密码的方式
https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输入密码的困扰而且又享受https带来的极速 设置记住密码(默认15分钟): git config --global credential.
860 0
|
3月前
|
开发工具 git
git 常用命令
这些只是 Git 命令的一部分,Git 还有许多其他命令和选项,可根据具体需求进行深入学习和使用。熟练掌握这些命令能够帮助你更高效地管理代码版本和协作开发。