用Git在 Ubuntu 22.04(Git 2.34.1)把 ROS 2 工作空间上传到全新的 GitHub 仓库 步骤

下面给你一套 在 Ubuntu 22.04(Git 2.34.1)把 ROS 2 工作空间上传到全新的 GitHub 仓库 的"从零到推送"步骤,并确保不把 build/install/log/ 等临时目录传上去。


一次搞定:把 ROS2 工作空间推到 GitHub

假设你的工作空间路径是 ~/ros2_ws(里面通常有 src/build/install/log/)。

0)准备:确认 Git 身份(只需做一次)

bash 复制代码
git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"   # 用 GitHub 绑定过的邮箱

1)在 GitHub 上创建一个空仓库

  • 打开 GitHub → New repository

  • 不要 勾选 "Add a README"、".gitignore"、"License"(保持完全空仓库,初次推送最省事)

  • 复制仓库地址(HTTPS 或 SSH)

    • HTTPS 形如:https://github.com/<your-username>/<repo>.git
    • SSH 形如:git@github.com:<your-username>/<repo>.git

若你必须创建时就勾了 README,也没关系,后面给你同步办法。

2)在本地工作空间根目录初始化仓库

进入你的 ROS2 工作空间(根目录!不是 src/ 里):

bash 复制代码
cd ~/ros2_ws
git init
git branch -M main   # 直接把默认分支命名为 main(推荐)

3)添加 .gitignore:忽略构建产物与缓存

~/ros2_ws 创建或覆盖 .gitignore(下面这个模板够用而且安全):

bash 复制代码
cat > .gitignore << 'EOF'
# --- ROS 2 colcon workspace ---
build/
install/
log/
.colcon*
.ros/

# --- CMake / compile cache ---
CMakeCache.txt
CMakeFiles/
cmake-build-*/
compile_commands.json

# --- Editors/IDE ---
.vscode/
.idea/
*.code-workspace

# --- Python ---
__pycache__/
*.py[cod]
*.pyo
*.pyd
*.egg-info/
.eggs/

# --- Misc ---
*.swp
*.swo
.DS_Store

# Auto-generated launch parameter files
launch_params_*

# TF tree Graphviz outputs
frames_*.gv
frames_*.pdf

# Ignore the save_datas folder in control_planner
src/control_planner/control_planner/save_datas/

# --- Optional large/binary (按需启用) ---
# *.bag
# *.onnx
# *.pt
# *.bin
# *.stl
# *.fbx

EOF

说明:

  • 放在工作空间根目录即可覆盖所有包。
  • src/ 里的源码会被提交;build/install/log 会被忽略。
  • 如果你想把空目录(比如某些包的空资源目录)保留在 Git,可在空目录里放一个 .gitkeep 文件。

4)首轮提交

bash 复制代码
git add .
git status      # 确认没有把 build/install/log 混进来
git commit -m "feat: initialize ROS2 workspace (ignore build/install/log)"

5)绑定远程并推送

<REMOTE_URL> 替换为你在步骤 1 复制的仓库地址:

bash 复制代码
git remote add origin <REMOTE_URL>
git push -u origin main
  • HTTPS :首次推送会要求登录。若启用了 2FA,密码处应使用 Personal Access Token (classic)
  • SSH:确保本机已配置 SSH key 并添加到 GitHub。

✅ 到这里,你的代码就上线了:src/ 等源码在 GitHub,build/install/log 等被忽略,不会上传。


(可选)如果你在 GitHub 创建仓库时勾了 README

这会导致远程已有提交,首次推送前先把它拉下来再推:

bash 复制代码
git remote add origin <REMOTE_URL>
git pull --rebase origin main   # 把远端 README 合并到本地提交之前
git push -u origin main

后续日常更新(工作流)

~/ros2_ws

bash 复制代码
# 1. 查看状态
git status

# 2. 选择要提交的改动
git add -A              # 或者:git add <path/to/file>

# 3. 写清楚提交信息(建议英文动词开头)
git commit -m "fix: correct TF broadcaster for NED<->base_link"

# 4. 推送
git push

相关推荐
小天源26 分钟前
Cacti在Debian/Ubuntu中安装及其使用
运维·ubuntu·debian·cacti
独自归家的兔2 小时前
ubuntu系统安装dbswitch教程 - 备份本地数据到远程服务器
linux·运维·ubuntu
m0_694845572 小时前
tinylisp 是什么?超轻量 Lisp 解释器编译与运行教程
服务器·开发语言·云计算·github·lisp
ONE_SIX_MIX2 小时前
ubuntu 24.04 用rdp连接,桌面黑屏问题,解决
linux·运维·ubuntu
June`2 小时前
muduo项目排查错误+测试
linux·c++·github·muduo网络库
春日见2 小时前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
weixin_6685 小时前
GitHub 2026年AI项目详细数据汇总表-AI分析-分享
人工智能·github
老师用之于民19 小时前
【DAY21】Linux软件编程基础&Shell 命令、脚本及系统管理实操
linux·运维·chrome·经验分享·笔记·ubuntu
CoderJia程序员甲21 小时前
GitHub 热榜项目 - 日榜(2026-02-05)
ai·开源·大模型·github·ai教程
stevenzqzq1 天前
git 常用操作
大数据·git