将git托管的项目代码推送到一个空的远程仓库

场景

将windows PC中的本地代码推送到编译服务器上进行编译。

配置服务器访问权限

设置远程仓库所在服务器的SSH访问权限,让git客户端能够访问(push和pull代码)。在这个例子中,我们使用authorized_keys方法对用户进行身份验证。
首先,创建名为git的用户,并在git用户目录下创建.ssh目录。

adduser git
# 设置git用户的密码
passwd git
mkdir .ssh && chmod 700 .ssh
touch .ssh/authorized_keys && chmod 600 .ssh/authorized_keys

然后,将一些开发者的SSH公钥添加到authorized_keys文件中。公钥看起来如下所示:
在这里插入图片描述

windows系统下的用户ssh公钥文件位于C:\Users\xxx.ssh目录下

创建并push代码到远程仓库

下面以将我本地的vpp代码为例。在我的PC本地中已从github上clone了一份vpp代码。vpp代码目录如下:
在这里插入图片描述

  1. 在服务器端创建一个名为vpp.git的目录,然后使用git init命令将该vpp.git目录初始化为一个空的git仓库。
mkdir vpp-test.git
cd vpp-test.git/
git init

在这里插入图片描述

  1. 修改本地仓库中VPP代码的远程仓库地址,将远程仓库地址修改为git@192.168.161.100:/home/git/vpp-test.git.其中,git为服务器上添加的git账号名,192.168.161.100为服务器地址,网络IP地址需要为改为你自己的git服务器地址,/home/git/vpp-test.git为服务器端的git仓库地址。
    在这里插入图片描述
    修改本地git仓库的远程仓库地址后,执行git push命令,将代码推送到远程仓库:
git push

结果推送失败,报错如下:

$ git push origin master
Enumerating objects: 128349, done.
Counting objects: 100% (128349/128349), done.
Delta compression using up to 8 threads
Compressing objects: 100% (63795/63795), done.
Writing objects: 100% (128349/128349), 166.20 MiB | 28.99 MiB/s, done.
Total 128349 (delta 64701), reused 126936 (delta 63299), pack-reused 0
remote: Resolving deltas: 100% (64701/64701), done.
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: is denied, because it will make the index and work tree inconsistent
remote: with what you pushed, and will require 'git reset --hard' to match
remote: the work tree to HEAD.
remote:
remote: You can set the 'receive.denyCurrentBranch' configuration variable
remote: to 'ignore' or 'warn' in the remote repository to allow pushing into
remote: its current branch; however, this is not recommended unless you
remote: arranged to update its work tree to match what you pushed in some
remote: other way.
remote:
remote: To squelch this message and still keep the default behaviour, set
remote: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To 192.168.161.100:/home/git/vpp-test.git
 ! [remote rejected]     master -> master (branch is currently checked out)
error: failed to push some refs to '192.168.161.100:/home/git/vpp-test.git'

翻译过来意思是:默认情况下,不允许更新非裸仓库中的当前分支,因为这将导致索引和工作树与你推送的内容不一致,并且需要执行"git reset --hard"来匹配工作树和HEAD.
但是可以在远程仓库中将"'receive.denyCurrentBranch"配置项设置为"ignore"或者"warn",以允许推送当前分支。

我们按上述提示信息,在远程仓库中将receive.denyCurrentBranch配置为ignore之后,本地仓库中的代码就能成功推送到远程仓库中了。

git config receive.denyCurrentBranch ignore

参考文档:
https://www.cnblogs.com/jeffry/p/5446213.html
https://blog.csdn.net/inthat/article/details/124489805
Git使用基础—git init 与git init --bare的区别
https://www.progit.cn/

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值