一、基本的 Git 命令
-
初始化一个新的 git 存储库
bashgit init
-
克隆现有存储库
bashgit clone https://github.com/user/repo.git
-
检查您的存储库的状态
bashgit status
-
暂存所有提交更改
bashgit add .
-
用有意义的信息做出承诺
bashgit commit -m "Add feature X with proper validation"
-
将提交推送到远程分支
bashgit push origin main
-
从远程提取最新更改
bashgit pull origin main
-
创建一个新分支并切换到它
bashgit checkout -b feature/new-ui
-
切换分支
bashgit checkout develop
-
在本地删除分支
bashgit branch -d feature/old-branch
二、GitHub 特定的命令和技巧
-
使用 GitHub CLI 创建新存储库
bashgh repo create my-new-project --public --description "A new repository"
-
在 GitHub 页面上打开当前存储库
bashgh repo view --web
-
列出 repo 的所有拉取请求
bashgh pr list
-
在本地签出拉取请求
bashgh pr checkout 42
-
通过 CLI 创建拉取请求
bashgh pr create --title "Add new login flow" --body "Implemented OAuth2 login" --base main --head feature/login
三、分支与合并
-
将分支合并到当前分支中
bashgit merge feature/new-ui
-
将当前分支变基到 main
bashgit rebase main
-
如果发生冲突,则中止合并或变基
bashgit merge --abort
四、撤消和重置
-
提交前取消暂存文件
bashgit reset HEAD <file>
-
丢弃文件中未暂存的更改
bashgit checkout -- <file>
-
通过创建新提交来恢复提交
bashgit revert <commit_hash>
-
重置提交和更改(危险:谨慎使用)
bashgit reset --hard HEAD~1
五、有用的 Git 别名示例
-
快速状态命令
bashgit config --global alias.s 'status -s'
-
用图表和颜色记录一条线
bashgit config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
六、管理远程存储库
-
添加远程源
bashgit remote add origin https://github.com/user/repo.git
-
更改远程源的 URL
bashgit remote set-url origin git@github.com:user/repo.git
-
移除遥控器
bashgit remote remove origin
七、标记发布
-
创建带注释的标签
bashgit tag -a v1.0 -m "Version 1.0 release"
-
将标签推送到遥控器
bashgit push origin v1.0
-
删除远程标签
bashgit push --delete origin v1.0
八、GitHub Actions 片段
-
Node.js项目的基本工作流程
yamlname: Node CI on: [push, pull_request] jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 with: node-version: '16' - run: npm install - run: npm test
-
在多个 Node 版本上运行测试
yamlstrategy: matrix: node-version: [12, 14, 16] steps: - uses: actions/setup-node@v3 with: node-version: ${{ matrix.node-version }}
-
GitHub Actions 中的缓存依赖项
yaml- name: Cache NPM modules uses: actions/cache@v3 with: path: ~/.npm key: ${{ runner.os }}-npm-${{ hashFiles('package-lock.json') }} restore-keys: | ${{ runner.os }}-npm-
-
上传工件
yaml- uses: actions/upload-artifact@v3 with: name: my-artifact path: path/to/file
-
在 GitHub Actions 中通知 Slack
yaml- name: Slack Notification uses: 8398a7/action-slack@v3 with: status: ${{ job.status }} fields: repo,message,commit,author env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
九、GitHub CLI 片段
-
列出当前用户的存储库
bashgh repo list --limit 10
-
创建一个新问题
bashgh issue create --title "Bug report" --body "There is an issue with login"
-
查看拉取请求详细信息
bashgh pr view 123 --web
-
关闭问题
bashgh issue close 42
-
将自己分配给一个问题
bashgh issue edit 42 --add-assignee @me
十、GitHub Markdown 片段
-
任务列表语法
markdown- [x] Item 1 done - [ ] Item 2 pending
-
表格示例
markdown| Feature | Supported | | -------- | --------- | | Login | ✅ | | Signup | ❌ |
-
嵌入图像
markdown
十一、合作与审查
-
通过 CLI 合并拉取请求
bashgh pr merge 123 --squash --delete-branch
-
请求团队成员审核
bashgh pr review 123 --request "username"
-
查看 PR 中的更改
bashgh pr diff 123
十二、使用 GitHub Actions 实现自动化
-
PR 上的自动标签
yamlname: Label PRs on: pull_request: types: [opened, synchronize] jobs: label: runs-on: ubuntu-latest steps: - uses: actions/labeler@v3
-
自动关闭过时问题
yamlname: Close stale on: schedule: - cron: '0 0 * * 0' jobs: stale: runs-on: ubuntu-latest steps: - uses: actions/stale@v3
十三、Git 提示与技巧
-
显示目录中每个文件的上次提交
bashgit ls-files | xargs -I{} git log -1 --format="%h %ad %an" -- {}
-
以交互方式压缩最后 N 个提交
bashgit rebase -i HEAD~3