Git和GitHub终极秘籍:50个命令让你从新手秒变专家

一、基本的 Git 命令

  1. 初始化一个新的 git 存储库

    bash 复制代码
    git init
  2. 克隆现有存储库

    bash 复制代码
    git clone https://github.com/user/repo.git
  3. 检查您的存储库的状态

    bash 复制代码
    git status
  4. 暂存所有提交更改

    bash 复制代码
    git add .
  5. 用有意义的信息做出承诺

    bash 复制代码
    git commit -m "Add feature X with proper validation"
  6. 将提交推送到远程分支

    bash 复制代码
    git push origin main
  7. 从远程提取最新更改

    bash 复制代码
    git pull origin main
  8. 创建一个新分支并切换到它

    bash 复制代码
    git checkout -b feature/new-ui
  9. 切换分支

    bash 复制代码
    git checkout develop
  10. 在本地删除分支

    bash 复制代码
    git branch -d feature/old-branch

二、GitHub 特定的命令和技巧

  1. 使用 GitHub CLI 创建新存储库

    bash 复制代码
    gh repo create my-new-project --public --description "A new repository"
  2. 在 GitHub 页面上打开当前存储库

    bash 复制代码
    gh repo view --web
  3. 列出 repo 的所有拉取请求

    bash 复制代码
    gh pr list
  4. 在本地签出拉取请求

    bash 复制代码
    gh pr checkout 42
  5. 通过 CLI 创建拉取请求

    bash 复制代码
    gh pr create --title "Add new login flow" --body "Implemented OAuth2 login" --base main --head feature/login

三、分支与合并

  1. 将分支合并到当前分支中

    bash 复制代码
    git merge feature/new-ui
  2. 将当前分支变基到 main

    bash 复制代码
    git rebase main
  3. 如果发生冲突,则中止合并或变基

    bash 复制代码
    git merge --abort

四、撤消和重置

  1. 提交前取消暂存文件

    bash 复制代码
    git reset HEAD <file>
  2. 丢弃文件中未暂存的更改

    bash 复制代码
    git checkout -- <file>
  3. 通过创建新提交来恢复提交

    bash 复制代码
    git revert <commit_hash>
  4. 重置提交和更改(危险:谨慎使用)

    bash 复制代码
    git reset --hard HEAD~1

五、有用的 Git 别名示例

  1. 快速状态命令

    bash 复制代码
    git config --global alias.s 'status -s'
  2. 用图表和颜色记录一条线

    bash 复制代码
    git 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"

六、管理远程存储库

  1. 添加远程源

    bash 复制代码
    git remote add origin https://github.com/user/repo.git
  2. 更改远程源的 URL

    bash 复制代码
    git remote set-url origin git@github.com:user/repo.git
  3. 移除遥控器

    bash 复制代码
    git remote remove origin

七、标记发布

  1. 创建带注释的标签

    bash 复制代码
    git tag -a v1.0 -m "Version 1.0 release"
  2. 将标签推送到遥控器

    bash 复制代码
    git push origin v1.0
  3. 删除远程标签

    bash 复制代码
    git push --delete origin v1.0

八、GitHub Actions 片段

  1. Node.js项目的基本工作流程

    yaml 复制代码
    name: 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
  2. 在多个 Node 版本上运行测试

    yaml 复制代码
    strategy:
      matrix:
        node-version: [12, 14, 16]
    
    steps:
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
  3. 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-
  4. 上传工件

    yaml 复制代码
    - uses: actions/upload-artifact@v3
      with:
        name: my-artifact
        path: path/to/file
  5. 在 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 片段

  1. 列出当前用户的存储库

    bash 复制代码
    gh repo list --limit 10
  2. 创建一个新问题

    bash 复制代码
    gh issue create --title "Bug report" --body "There is an issue with login"
  3. 查看拉取请求详细信息

    bash 复制代码
    gh pr view 123 --web
  4. 关闭问题

    bash 复制代码
    gh issue close 42
  5. 将自己分配给一个问题

    bash 复制代码
    gh issue edit 42 --add-assignee @me

十、GitHub Markdown 片段

  1. 任务列表语法

    markdown 复制代码
    - [x] Item 1 done
    - [ ] Item 2 pending
  2. 表格示例

    markdown 复制代码
    | Feature  | Supported |
    | -------- | --------- |
    | Login    | ✅        |
    | Signup   | ❌        |
  3. 嵌入图像

    markdown 复制代码
    ![Alt text](https://via.placeholder.com/150)

十一、合作与审查

  1. 通过 CLI 合并拉取请求

    bash 复制代码
    gh pr merge 123 --squash --delete-branch
  2. 请求团队成员审核

    bash 复制代码
    gh pr review 123 --request "username"
  3. 查看 PR 中的更改

    bash 复制代码
    gh pr diff 123

十二、使用 GitHub Actions 实现自动化

  1. PR 上的自动标签

    yaml 复制代码
    name: Label PRs
    
    on:
      pull_request:
        types: [opened, synchronize]
    
    jobs:
      label:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/labeler@v3
  2. 自动关闭过时问题

    yaml 复制代码
    name: Close stale
    
    on:
      schedule:
        - cron: '0 0 * * 0'
    
    jobs:
      stale:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/stale@v3

十三、Git 提示与技巧

  1. 显示目录中每个文件的上次提交

    bash 复制代码
    git ls-files | xargs -I{} git log -1 --format="%h %ad %an" -- {}
  2. 以交互方式压缩最后 N 个提交

    bash 复制代码
    git rebase -i HEAD~3
相关推荐
最贪吃的虎7 小时前
GitHub推送又超时了?试试SSH
运维·ssh·github
smachao7 小时前
Redis Desktop Manager(Redis可视化工具)安装及使用详细教程
redis·git·bootstrap
szcsun58 小时前
git的常用命令
git
jian110588 小时前
android studio 解决git用户名和用户邮箱不一致的问题
git
jian110589 小时前
Mac git配置账号和邮箱,可以修改
git·macos
笨笨饿11 小时前
博客目录框架
c语言·开发语言·arm开发·git·嵌入式硬件·神经网络·编辑器
白玉cfc11 小时前
git协作开发
git·团队开发·远程工作
四千岁12 小时前
Obsidian + jsDelivr + PicGo = 免费无限图床:一键上传,全平台粘贴即发
前端·程序员·github
wusfe12 小时前
适配 Anthropic 兼容 AI 提供商的环境配置快速切换工具
github