用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

相关推荐
Shin_chan2 小时前
Linux 内核史诗级提权漏洞:CVE-2026-31431 复现与分析
github
具身小佬4 小时前
#!/usr/bin/env python3是干嘛的
ubuntu
CoderJia程序员甲5 小时前
GitHub 热榜项目 - 日榜(2026-05-03)
ai·大模型·llm·github·ai教程
谢尔登5 小时前
10_从 React Hooks 本质看 useState
前端·ubuntu·react.js
yyuuuzz6 小时前
aws 基础认知与实践注意点
运维·服务器·网络·云计算·github·aws
weixin_514253186 小时前
430-github-aguvis tmux
github
阿里嘎多学长7 小时前
2026-05-03 GitHub 热点项目精选
开发语言·程序员·github·代码托管
努力努力再努力wz7 小时前
【MySQL进阶系列】一文打通事务机制:从锁、Undo Log 到 MVCC 与隔离级别
c语言·数据结构·数据库·c++·mysql·算法·github
武超杰7 小时前
Git 从入门到精通教程
git
zhangrelay8 小时前
三分钟云课实践速通--单片机原理与应用--Arduino--SimulIDE--
linux·单片机·嵌入式硬件·学习·ubuntu