【技能学习】Git中Clone仓库后更新与推送本人仓库

1、创建fork分支

2、替换子模块的远程地址

msfl_ws工作空间中,将A-LOAM-NOTED子模块指向你 Fork 的版本。

  • 进入A-LOAM-NOTED子模块目录
1
cd src/A-LOAM-NOTED
  • 修改远程地址

​ 将子模块的远程地址替换为你的 Fork 仓库地址(以下示例使用你的 GitHub 用户名YourUsername):

1
git remote set-url origin https://github.com/YourUsername/A-LOAM-NOTED.git
  • 验证远程地址

​ 确认远程地址已被修改:

1
git remote -v

​ 输出应显示类似以下内容:

1
2
origin  https://github.com/YourUsername/A-LOAM-NOTED.git (fetch)
origin https://github.com/YourUsername/A-LOAM-NOTED.git (push)

3、修改提交到 Fork 仓库

  • A-LOAM-NOTED目录中,确认当前分支:

    1
    git branch
  • 提交修改

    将本地的修改提交到当前分支,或创建一个新分支来提交:

    1
    2
    3
    git checkout -b ubuntu20.04-mods
    git add .
    git commit -m "Adapted A-LOAM-NOTED for Ubuntu 20.04"
  • 推送到你的 Fork 仓库

    将修改个体到 Fork 仓库的分支:

    1
    git push origin ubuntu20.04-mods

4、更新主仓库对子模块的引用

在主仓库中,更新A-LOAM-NOTED的子引用模块,指向你的 Fork 仓库和分支

  • 返回主仓库目录

    1
    cd ../..
  • 更新子模块引用

    修改子模块配置,导出指向你的Fork仓库和分支:

    1
    2
    git config -f .gitmodules submodule.src/A-LOAM-NOTED.url https://github.com/YourUsername/A-LOAM-NOTED.git
    git config -f .gitmodules submodule.src/A-LOAM-NOTED.branch ubuntu20.04-mods
  • 同步子模块

    同步子模块的配置:

    1
    git submodule sync
  • 提交更新

    将主仓库对子模块的更新提交:

    1
    2
    3
    git add .gitmodules src/A-LOAM-NOTED
    git commit -m "Updated submodule A-LOAM-NOTED to fork repository"
    git push origin main

Tips:一般到这里就完成了

5、验证 Fork 和更新

  • 验证主仓库对子模块的引用

    运行以下命令,确认子模块已指向你的Fork仓库和分支:

    1
    git submodule status
  • 验证远程仓库同步

    在其他工作空间中克隆主仓库时,确保正确取模块:

    1
    git clone --recursive https://github.com/YourUsername/msfl_ws.git
  • 如果已存在本地仓库,更新子模块:

    1
    git submodule update --init --recursive

Tips:修改branch及同步

  • 远程仓库设置:当fork的分支使用的是master等名称时,需要进入到项目的设置中,选择branch,点击rename

  • 本地仓库设置:在本地的子模块中,首先进行分支重命名,执行如下指令:

    1
    git branch -m master main

    运行后执行git branch操作,输出会显示*main表示已经成功重命名本地分支,后续进行push操作即可

  • 注意如果运行git push操作等其他操作时让你重新输出用户名和密码,证明之前检查git remote -v的输出是https链接的,已经被github弃用了,但之前肯定你配置了ssh链接,那就不需要重新配置,只需运行下述指令,重新将本地库链接修改为以git链接的即可,即修改下方的QianYuan1437/LIO-SAM-Modified.git为自己的fork仓库,其余信息与上方操作2提到的一致

    1
    git remote set-url origin git@github.com:QianYuan1437/LIO-SAM-Modified.git