将修改后的代码从一个克隆的仓库推送到新的 GitHub 仓库(以ORBSLAM2_with_pointcloud_map仓库为例)

将修改后的代码从一个克隆的仓库推送到新的 GitHub 仓库

本文介绍了如何从一个已经克隆下来的 GitHub 仓库中,将修改后的代码推送到一个新创建的 GitHub 仓库。

前提条件

  • 已经克隆了一个现有的 GitHub 仓库到本地。示例仓库为:https://github.com/gaoxiang12/ORBSLAM2_with_pointcloud_map.git
  • 已经在 GitHub 上创建了一个新的空仓库。示例仓库为:git@github.com:cgm-free/ORBSLAM2_with_pointcloudmap.git

步骤

1. 克隆现有的 GitHub 仓库

如果你还没有克隆现有的仓库,可以通过以下命令进行克隆:

git clone https://github.com/gaoxiang12/ORBSLAM2_with_pointcloud_map.git

2. 进入项目目录

打开终端并切换到克隆下来的项目目录:

cd ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified

3. 查看当前远程仓库

通过以下命令查看当前配置的远程仓库:

git remote -v

这将显示原始的仓库 URL。

cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git remote -v
origin  https://github.com/gaoxiang12/ORBSLAM2_with_pointcloud_map.git (fetch)
origin  https://github.com/gaoxiang12/ORBSLAM2_with_pointcloud_map.git (push)

4. 移除原有的远程仓库

移除与原始 GitHub 仓库的关联:

git remote remove origin

5. 添加新的远程仓库

添加新创建的 GitHub 仓库为远程仓库:

git remote add origin git@github.com:cgm-free/ORBSLAM2_with_pointcloudmap.git

6. 添加和提交更改(可选)

如果对代码进行了更改,使用以下命令添加和提交这些更改:

git add .
git commit -m "你的提交信息"

7. 推送到新的远程仓库

最后,使用以下命令将代码推送到新创建的 GitHub 仓库:

git push -u origin main

结论

现在,你应该已经成功地将修改后的代码从一个克隆的仓库推送到了新的 GitHub 仓库。以后,你可以通过简单的 git addgit commitgit push 命令来更新你的代码。

我的运行命令:

cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git remote -v
origin  https://github.com/gaoxiang12/ORBSLAM2_with_pointcloud_map.git (fetch)
origin  https://github.com/gaoxiang12/ORBSLAM2_with_pointcloud_map.git (push)
cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git remote remove origin
cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git remote add origin git@github.com:cgm-free/ORBSLAM2_with_pointcloudmap.git
cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git add .
cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git commit -m "修改CMakeLists.txt文件的C++11为c++14"
[master 7065bcd] 修改CMakeLists.txt文件的C++11为c++14
 1 file changed, 33 insertions(+), 10 deletions(-)
cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git push -u origin main
error: 源引用规格 main 没有匹配
error: 无法推送一些引用到 'git@github.com:cgm-free/ORBSLAM2_with_pointcloudmap.git'
cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git branch
* master
cgm@cgm:~/ORBSLAM2_with_pointcloud_map/ORB_SLAM2_modified$ git push -u origin master
枚举对象中: 632, 完成.
对象计数中: 100% (632/632), 完成.
使用 16 个线程进行压缩
压缩对象中: 100% (384/384), 完成.
写入对象中: 100% (632/632), 7.99 MiB | 1.60 MiB/s, 完成.
总共 632 (差异 218),复用 624 (差异 214)
remote: Resolving deltas: 100% (218/218), done.
To github.com:cgm-free/ORBSLAM2_with_pointcloudmap.git
 * [new branch]      master -> master
分支 'master' 设置为跟踪来自 'origin' 的远程分支 'master'

解决 Git 推送错误 "源引用规格 main 没有匹配"

当我们尝试从一个已经克隆下来的仓库推送代码到新的 GitHub 仓库时,有时会遇到 源引用规格 main 没有匹配 这样的错误。本文将针对该问题提供解决方案。

问题描述

在尝试使用以下命令推送代码时:

git push -u origin main

遇到如下错误:

error: 源引用规格 main 没有匹配
error: 无法推送一些引用到 'git@github.com:cgm-free/ORBSLAM2_with_pointcloudmap.git'

原因分析

该错误的主要原因是本地分支名和远程分支名没有对应。

解决方案

方案一:使用相同的分支名进行推送

  1. 检查本地分支名

    通过运行以下命令查看所有本地分支:

    git branch
    

    如果你在使用 master 分支(通常命令输出会显示 * master),那么你需要确保推送到具有相同名称的远程分支。

  2. 推送到远程分支

    使用以下命令推送到 master 远程分支:

    git push -u origin master
    

方案二:重命名本地分支

  1. 重命名本地分支

    使用以下命令将本地的 master 分支重命名为 main

    git branch -m master main
    
  2. 推送到新的远程分支

    使用以下命令推送到名为 main 的新远程分支:

    git push -u origin main
    

总结

通过以上两种方案,你应该能成功解决 源引用规格 main 没有匹配 这一问题。这样,你就能成功地将代码推送到新的 GitHub 仓库。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楚歌again

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值