A complete GitHub code commit and push workflow. Handles the full process from local commit to remote push, including remote repository configuration, historical merge conflict resolution, and SSH push.
✨ Features
- 🔗 Automatically configure Remote - Automatically add the GitHub remote repository
- 📝 Standardized commit messages - Follow the Conventional Commits specification
- 🔀 Smart merge - Automatically handle unrelated histories conflicts
- 🔐 SSH push - Push securely using an SSH key
- 📊 Commit status check - View changed files and commit history
Prerequisites
# 确保已安装 git
git --version
Make sure a GitHub SSH key is configured
ls ~/.ssh/id_*.pub
If not, first generate an SSH key: ssh-keygen -t ed25519 -C "your_email@example.com"
## 快速开始
### 1. 检查当前状态
```bash
cd your-project
git status
git remote -v
2. Configure the remote repository (if not already configured)
# 添加 GitHub 远程仓库(SSH 方式)
git remote add origin git@github.com:username/repo.git
# 或者使用 HTTPS(需要 token)
git remote add origin https://github.com/username/repo.git
3. Commit changes
# 查看变更
git diff --cached --stat
# 添加文件
git add <file>
# 提交(遵循 Conventional Commits)
git commit -m "type(scope): description
详细说明...
fixes #123"
4. Push to remote
# 先获取远程更新
git fetch origin
# 检查是否有冲突
git log HEAD..origin/main --oneline
# 合并远程更改(如果有)
git pull origin main --allow-unrelated-histories --no-rebase
# 推送
git push origin main
Complete workflow script
Create scripts/commit-and-push.sh:
#!/bin/bash
# GitHub Commit & Push 脚本
# 用法: ./commit-and-push.sh "commit message" [branch]
set -e
COMMIT_MSG="${1:-Update files}"
BRANCH="${2:-main}"
REMOTE_URL="${3:-}"
echo "🚀 GitHub Commit & Push"
echo "======================"
# 1. 检查是否在 git 仓库
if ! git rev-parse --git-dir > /dev/null 2>&1; then
echo "❌ 当前目录不是 git 仓库"
exit 1
fi
# 2. 检查远程仓库
if ! git remote -v > /dev/null 2>&1; then
if [ -z "$REMOTE_URL" ]; then
echo "❌ 未配置远程仓库,请提供仓库 URL"
echo "用法: $0 'commit msg' branch 'git@github.com:user/repo.git'"
exit 1
fi
echo "🔗 添加远程仓库..."
git remote add origin "$REMOTE_URL"
fi
echo "📍 当前分支: $(git branch --show-current)"
echo "🌐 远程仓库: $(git remote get-url origin)"
# 3. 检查是否有变更
if git diff --cached --quiet && git diff --quiet; then
echo "⚠️ 没有要提交的变更"
exit 0
fi
# 4. 显示将要提交的文件
echo ""
echo "📁 变更文件:"
git status --short
# 5. 添加所有变更
echo ""
echo "➕ 添加文件..."
git add -A
# 6. 提交
echo ""
echo "💾 提交更改..."
git commit -m "$COMMIT_MSG"
echo "✅ 提交成功: $COMMIT_MSG"
# 7. 获取远程更新
echo ""
echo "📥 获取远程更新..."
git fetch origin
# 8. 检查是否需要合并
LOCAL=$(git rev-parse @)
REMOTE=$(git rev-parse origin/$BRANCH 2>/dev/null || echo "null")
if [ "$REMOTE" != "null" ] && [ "$LOCAL" != "$REMOTE" ]; then
echo "🔀 需要合并远程更改..."
if git merge-base --is-ancestor origin/$BRANCH HEAD 2>/dev/null; then
echo "⚡ 本地领先远程,直接推送"
else
echo "🔀 合并远程更改..."
git pull origin $BRANCH --allow-unrelated-histories --no-rebase || {
echo "❌ 合并冲突,请手动解决"
exit 1
}
fi
fi
# 9. 推送
echo ""
echo "📤 推送到远程..."
git push origin $BRANCH
echo ""
echo "✅ 推送成功!"
echo "🔗 查看: $(git remote get-url origin | sed 's/\.git$//' | sed 's/git@github.com:/https:\/\/github.com\//')"
Common commands quick reference
Commit specification
# feat: 新功能
git commit -m "feat(auth): add login with OAuth"
# fix: 修复
git commit -m "fix(api): handle null pointer exception"
# docs: 文档
git commit -m "docs(readme): update installation guide"
# refactor: 重构
git commit -m "refactor(utils): simplify date formatting"
# chore: 杂项
git commit -m "chore(deps): update dependencies"
Handle common issues
Issue 1: remote not configured
fatal: 'origin' does not appear to be a git repository
# 解决
git remote add origin git@github.com:username/repo.git
Issue 2: unrelated histories
fatal: refusing to merge unrelated histories
# 解决
git pull origin main --allow-unrelated-histories --no-rebase
Issue 3: authentication required
fatal: could not read Username for 'https://github.com'
# 解决 - 切换到 SSH
git remote set-url origin git@github.com:username/repo.git
Issue 4: permission denied
Permission denied (publickey)
# 解决 - 检查 SSH key
ssh -T git@github.com
# 如果没有 key,生成并添加到 GitHub
ssh-keygen -t ed25519 -C "email@example.com"
cat ~/.ssh/id_ed25519.pub
# 然后复制到 GitHub Settings -> SSH Keys
Best practices
- Commit frequently: move in small steps, with each commit completing one logical unit
- Write good commit messages: follow the Conventional Commits specification
- Pull before push: avoid overwriting others' changes
- Use SSH: safer and more convenient than HTTPS
- Check status: before committing, use
git statusandgit diffto check
Automation script
Create .clawdhub/manifest.json:
{
"id": "github-commit-push",
"name": "GitHub Commit & Push",
"version": "1.0.0",
"description": "Complete git commit and push workflow for GitHub",
"author": "xdrshjr",
"tags": ["git", "github", "commit", "push"],
"entry": "scripts/commit-and-push.sh"
}
Usage example
# 进入项目目录
cd ~/clawd
# 查看状态
git status
# 添加远程(如果没有)
git remote add origin git@github.com:xdrshjr/JR-Agent-Skills.git
# 添加文件
git add skills/remotion-synced-video/SKILL.md
# 提交
git commit -m "docs(remotion-synced-video): add audio sync best practices
- Add measure-audio.sh script for duration measurement
- Update index.tsx to use dynamic duration calculation
- Add troubleshooting for audio-visual sync issues"
# 获取远程更新并合并
git pull origin main --allow-unrelated-histories --no-rebase
# 推送
git push origin main
Pro Tip: Configuring the remote using SSH can avoid entering a password on every push; it is recommended to use SSH for all GitHub repositories.