普通视图

发现新文章,点击刷新页面。
昨天以前首页

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

作者 微芒不朽
2025年10月8日 22:11

一、基本的 Git 命令

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

    git init
    
  2. 克隆现有存储库

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

    git status
    
  4. 暂存所有提交更改

    git add .
    
  5. 用有意义的信息做出承诺

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

    git push origin main
    
  7. 从远程提取最新更改

    git pull origin main
    
  8. 创建一个新分支并切换到它

    git checkout -b feature/new-ui
    
  9. 切换分支

    git checkout develop
    
  10. 在本地删除分支

    git branch -d feature/old-branch
    

二、GitHub 特定的命令和技巧

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

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

    gh repo view --web
    
  3. 列出 repo 的所有拉取请求

    gh pr list
    
  4. 在本地签出拉取请求

    gh pr checkout 42
    
  5. 通过 CLI 创建拉取请求

    gh pr create --title "Add new login flow" --body "Implemented OAuth2 login" --base main --head feature/login
    

三、分支与合并

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

    git merge feature/new-ui
    
  2. 将当前分支变基到 main

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

    git merge --abort
    

四、撤消和重置

  1. 提交前取消暂存文件

    git reset HEAD <file>
    
  2. 丢弃文件中未暂存的更改

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

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

    git reset --hard HEAD~1
    

五、有用的 Git 别名示例

  1. 快速状态命令

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

    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. 添加远程源

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

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

    git remote remove origin
    

七、标记发布

  1. 创建带注释的标签

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

    git push origin v1.0
    
  3. 删除远程标签

    git push --delete origin v1.0
    

八、GitHub Actions 片段

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

    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 版本上运行测试

    strategy:
      matrix:
        node-version: [12, 14, 16]
    
    steps:
      - uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node-version }}
    
  3. GitHub Actions 中的缓存依赖项

    - 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. 上传工件

    - uses: actions/upload-artifact@v3
      with:
        name: my-artifact
        path: path/to/file
    
  5. 在 GitHub Actions 中通知 Slack

    - 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. 列出当前用户的存储库

    gh repo list --limit 10
    
  2. 创建一个新问题

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

    gh pr view 123 --web
    
  4. 关闭问题

    gh issue close 42
    
  5. 将自己分配给一个问题

    gh issue edit 42 --add-assignee @me
    

十、GitHub Markdown 片段

  1. 任务列表语法

    - [x] Item 1 done
    - [ ] Item 2 pending
    
  2. 表格示例

    | Feature  | Supported |
    | -------- | --------- |
    | Login    | ✅        |
    | Signup   | ❌        |
    
  3. 嵌入图像

    ![Alt text](https://via.placeholder.com/150)
    

十一、合作与审查

  1. 通过 CLI 合并拉取请求

    gh pr merge 123 --squash --delete-branch
    
  2. 请求团队成员审核

    gh pr review 123 --request "username"
    
  3. 查看 PR 中的更改

    gh pr diff 123
    

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

  1. PR 上的自动标签

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

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

十三、Git 提示与技巧

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

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

    git rebase -i HEAD~3
    
❌
❌