git本地仓库与远程仓库同步

  在学习和工作中,我们经常遇到这样的场景,我们已经在本地创建了一个git仓库,并添加了文件和修改记录。后面你又想在github或者gitlab上新建一个空白git仓库,并且让这2个仓库进行远程同步并且保存之前本地仓库的修改记录。这样一来github或者gitlab上的仓库即可作为备份,又方便多人协作。

具体操作如下:

1.创建本地仓库,使用git管理并添加文件以及修改记录

2.创建Git的远程仓库(可使用GitHub、GitLab、Gitee)

3.打开Git Bash命令行界面,切换目录到本地工作项目路径下,依次输入下面的命令:

//添加主机名为 origin 的远程仓库
git remote add origin 远程仓库地址

//将本地 master 分支推送到 origin主机的 master 分支,如果想推送到分支,把master替换成对应的分支名
git push -u origin master

在进行本地仓库与远程仓库同步过程中,会遇到如下报错:

 ! [rejected]        feature/sc8f6770 -> feature/sc8f6770 (fetch first)
error: failed to push some refs to 'xxx.xxx.com:xxx.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

可以使用以下指令解决冲突:

git pull origin master --allow-unrelated-histories

接着将本地的文件推送上去即可:

git push origin master

执行上述操作之后,就能在远程仓库上看到push上去的文件以及之前在本地仓库的修改记录。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值