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
相关推荐
天若有情6731 小时前
自己开发一款极简 Vanilla 原生前端框架,已开源上架 NPM & GitHub
前端框架·npm·github
fthux2 小时前
用了 GitZip 这么多年,我动手做了一个「Pro」版
人工智能·开源·github
金銀銅鐵2 小时前
[git] 浅解 git reset 命令
git·后端
zhangfeng11333 小时前
部署到服务器上 宝塔系统 使用宝塔在线编辑器 FTP 批量上传 Git 部署 打包上传 codebudyy 编程程序开发
服务器·git·编辑器
学习是种信仰4 小时前
Git工作流
git·深度学习
yuanyuan2o25 小时前
Git merge 的几种不同模式
git·github
视觉小萌新6 小时前
关于Vscode配置企业Git
git
zh_xuan6 小时前
使用命令行把安装包上传到github
c++·git·libcurl·c++工程打包
AC赳赳老秦7 小时前
财务报销自动化:用 OpenClaw 自动识别发票信息、填写报销单、校验报销规则,减少手工操作
运维·网络·eclipse·github·visual studio·deepseek·openclaw