Git SSH GitHub 命令笔记
创建于 2026-05-09
目录
Git / SSH / GitHub 命令笔记
Summary
Git 负责代码版本,SSH 负责安全连接,GitHub Actions 负责 CI/CD。对 MeetYou 来说,安全链路应该是:本地开发/测试 → commit/push → GitHub Actions CI → deploy → 远程 Core `/health` 验证,而不是随手在服务器乱改代码。相关:00-MOC-命令行与部署知识库总览|03-Linux-命令笔记|06-MeetYou-Core-部署运维手册|07-常见排错与小点整合
1. Git 基本工作区
Working Tree 暂未提交的文件
Staging Area git add 后的暂存区
Repository git commit 后的本地仓库
Remote GitHub 上的远程仓库
最常用链路:
git status
git diff
git add .
git commit -m "feat: add xxx"
git push
2. 查看状态与差异
git status
git diff # 工作区和暂存区差异
git diff --staged # 暂存区和上次提交差异
git diff --name-only HEAD~1..HEAD
查看历史:
git log --oneline --graph --decorate -n 20
git show HEAD
git show --name-only HEAD
3. 分支
git branch # 查看本地分支
git branch -a # 查看全部分支
git switch main # 切换分支
git switch -c feature/xxx # 新建并切换
git merge feature/xxx # 合并分支
删除分支:
git branch -d feature/xxx
4. 拉取与推送
git fetch origin # 只取远程信息,不改当前代码
git pull --ff-only # 只允许快进合并,部署时推荐
git push origin main
部署服务器上推荐:
git fetch origin
git pull --ff-only
Warning
服务器上不建议随手 `git pull` 产生 merge commit,也不建议直接改代码再 `git commit`。服务器应该尽量只作为部署目标。5. 判断这次是否需要更新远程 Core
本地:
git diff --name-only 旧提交..新提交
服务器:
cd /opt/meetyou/MeetYou
git fetch origin
git log --oneline HEAD..origin/main -- core gateway service_runtime adapters alembic deploy/systemd requirements-core.txt
判断:
- 没输出:远程新提交没碰 Core 关键路径,服务器通常不用更新。
- 有输出:Core 侧变了,需要走部署更新。
关键路径:
core/
gateway/
service_runtime/
adapters/
alembic/
deploy/systemd/
requirements-core.txt
6. SSH Key 基础
生成 key:
ssh-keygen -t ed25519 -C "meetyou-core-server"
查看公钥:
cat ~/.ssh/id_ed25519.pub
测试 GitHub:
ssh -T git@github.com
权限:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
Danger
`id_ed25519` 是私钥,绝对不要发给别人,也不要提交到仓库。`id_ed25519.pub` 是公钥,可以放到 GitHub。7. GitHub 私有仓库拉取
SSH 地址形如:
git@github.com:yourname/MeetYou.git
服务器上:
cd /opt
sudo mkdir -p /opt/meetyou
sudo chown ubuntu:ubuntu /opt/meetyou
cd /opt/meetyou
git clone git@github.com:yourname/MeetYou.git
8. GitHub Actions 基本认知
push/PR/手动触发
-> GitHub Actions runner
-> 安装依赖
-> 运行测试/构建
-> SSH 到服务器执行部署脚本
-> 健康检查
优点:
- 可重复。
- 有日志。
- 能阻止坏代码直接上线。
- 比让 Codex 长期拿服务器 shell 更安全。
9. 推荐部署脚本结构
服务器脚本:/opt/meetyou/scripts/deploy-core.sh
#!/usr/bin/env bash
set -euo pipefail
cd /opt/meetyou/MeetYou
git fetch origin
git pull --ff-only
source .venv/bin/activate
python -m pip install -r requirements-core.txt
sudo systemctl restart meetyou-core
sudo systemctl status meetyou-core --no-pager
注意:
chmod +x /opt/meetyou/scripts/deploy-core.sh
10. CRLF 换行问题
报错:
/usr/bin/env: ‘bash\r’: No such file or directory
原因:脚本用了 Windows CRLF 换行,Linux 识别成 bash\r。
修复:
sed -i 's/\r$//' script.sh
或者安装:
sudo apt install -y dos2unix
dos2unix script.sh
Git 设置:
git config --global core.autocrlf input # Linux/macOS 推荐
git config --global core.autocrlf true # Windows 常见
项目中更稳的是加 .gitattributes:
*.sh text eol=lf
*.ps1 text eol=crlf
*.bat text eol=crlf
*.cmd text eol=crlf
11. 常见 Git 操作
撤销未暂存修改:
git restore file.py
撤销暂存:
git restore --staged file.py
改最后一次提交信息:
git commit --amend
查看某文件历史:
git log --oneline -- path/to/file
临时保存工作区:
git stash
git stash pop
12. Codex / 人类协作建议
安全协作路线:
本地/分支开发
-> Codex 修改代码
-> 本地测试
-> commit
-> push
-> PR / main
-> CI
-> Deploy
-> 远程 /health
-> 本地 Desktop 真实测试
不要把生产服务器变成“实验场”。
13. 自测题
git fetch和git pull --ff-only的区别是什么?- 为什么服务器部署时推荐
--ff-only? - SSH 私钥和公钥分别是什么?哪个不能泄露?
/usr/bin/env: bash\r的根因是什么?
我的新增记录
反向链接:00-MOC-命令行与部署知识库总览|03-Linux-命令笔记|06-MeetYou-Core-部署运维手册|07-常见排错与小点整合