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
相关推荐
~央千澈~3 小时前
git添加远程仓库报错To add an exception for this directory解决方案-优雅草卓伊凡
git
掘金安东尼3 小时前
前端周刊433期(2025年9月22日–9月28日)
前端·javascript·github
绝无仅有3 小时前
面试真题之收钱吧问题与总结
后端·面试·github
绝无仅有3 小时前
真实面试经历之比亚迪线下hr面+一面+线上二面面经
后端·面试·github
绝无仅有4 小时前
远景集团面试后端Java岗位问答与总结汇总
后端·面试·github
爱吃烤鸡翅的酸菜鱼4 小时前
深度掌握 Git 分支体系:从基础操作到高级策略与实践案例
分布式·git·后端·gitee·github
卡布奇诺-海晨5 小时前
2025版本的idea解决Git冲突
java·git·intellij-idea
好好沉淀12 小时前
ide进去git突然报Cannot identify version of git executable: no response的错误
git
宇宙超级无敌霸王龙捏15 小时前
Git 分支完整操作指南
git