admin管理员组

文章数量:1606664

目录儿~

  • 一、Git、Github、GitLab
  • 二、Github CLI——gh
    • 2.1 gh简介
    • 2.2 gh的使用
      • 2.21 Github身份验证(必选)
      • 2.22 常用命令
        • (1)在Github仓库中打开当前项目
        • (2)gh配置 gh config
        • (3)克隆仓库 gh repo clone
        • (4)issue
        • (5)pull
        • (6)add、commit和push
    • 2.3 总结
  • 三、 Github访问优化

最近在Github上看到这个 Github CLI,从这个界面看貌似这个也能克隆仓库,而且下面小字说用这个官方的命令行界面速度更快。

虽然我一直都很快(指访问Github),但还是点进取来学习一下。

以前的提交、推送、克隆这些工作都是在相应的IED(比如在pycharm里面使用Git)里面进行的,有时也在git bash(或者powershell命令行)里面进行。

安装了Git之后,一般都可以在命令行使用git 开头的各种命令,如:

(base) PS C:\Users\dahu> git
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           [--super-prefix=<path>] [--config-env=<name>=<envvar>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone     Clone a repository into a new directory
   init      Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add       Add file contents to the index
   mv        Move or rename a file, a directory, or a symlink
   restore   Restore working tree files
   rm        Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect    Use binary search to find the commit that introduced a bug
   diff      Show changes between commits, commit and working tree, etc
   grep      Print lines matching a pattern
   log       Show commit logs
   show      Show various types of objects
   status    Show the working tree status

grow, mark and tweak your common history
   branch    List, create, or delete branches
   commit    Record changes to the repository
   merge     Join two or more development histories together
   rebase    Reapply commits on top of another base tip
   reset     Reset current HEAD to the specified state
   switch    Switch branches
   tag       Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch     Download objects and refs from another repository
   pull      Fetch from and integrate with another repository or a local branch
   push      Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some
concept guides. See 'git help <command>' or 'git help <concept>'
to read about a specific subcommand or concept.
See 'git help git' for an overview of the system.

一、Git、Github、GitLab

这里简单介绍一下这些概念,相比大家都比我熟悉了。

Git:
Git是一个开源的分布式版本控制系统,可以有效、高速地处理从很小到非常大的项目版本管理。这是官方解释,结合自己经验来说,一般在不大不小的公司里,开发的大佬提交代码就是用这个工具的。

Github:
GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git作为唯一的版本库格式进行托管,故名GitHub。

GitLab:
GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务。安装方法是参考GitLab在GitHub上的Wiki页面。一般来说中等规模的公司里面会用,毕竟相比于公有仓库这个可以私有化且免费。

Git是一种版本控制系统,就是一种工具软件,用于代码的存储和版本控制。

GitHub是一个基于Git实现的在线代码仓库,是目前全球最大的代码托管平台,可以帮助程序员之间互相交流和学习,当然不公开的仓库有收费服务。

GitLab也是一个基于Git实现的在线代码仓库,你可以用GitLab自己搭建一个类似于GitHub一样的仓库,但!GitLab有完整的管理界面和权限控制,中规模企业会搭建Git私服。

GitHub和GiLlab都是基于Web的Git远程仓库,都提供了分享开源项目的平台,为开发者和团队提供了存储、分享、发布和合作开发项目的中心化云存储的场所。从代码的私有性上来看,GitLab 是一个更好的选择。但是对于开源项目而言,GitHub 依然是代码托管的首选。

二、Github CLI——gh

2.1 gh简介

CLI:一般指命令行界面。 命令行界面(command-line interface)。

所以Github CLI即Github的命令行界面,CLI并不是该工具的名称,这款命令行工具的名字叫:gh

项目地址:https://github/cli/cli

官方介绍:

gh is GitHub on the command line. It brings pull requests, issues, and other GitHub concepts to the terminal next to where you are already working with git and your code.

安装:
直接到上面的仓库下载你的系统对应的release版本就可以了。还支持winget、conda等方式安装。

也可以在这里一键下载windows版本:https://cli.github/

2.2 gh的使用

官方命令文档:https://cli.github/manual/gh

在进行下面的设置之前现在命令行或者powershell(都行)输入gh看看有没有输出,确保成功安装gh

2.21 Github身份验证(必选)

或者说使用gh登录到你的Github账户。

命令行输入:

 gh auth login

接下来会有许多选项供你选择,一般是通过键盘的上下键来选择,并用enter确认

可以参考我的选择:

(base) PS C:\Users\dahu> gh auth login
? What account do you want to log into? GitHub
? What is your preferred protocol for Git operations? SSH
? Upload your SSH public key to your GitHub account? C:\Users\dahu\.ssh\id_rsa.pub
? Title for your SSH key: (GitHub CLI)

? Title for your SSH key: GitHub CLI
? How would you like to authenticate GitHub CLI? Login with a web browser

! First copy your one-time code: 7080-B9B3
Press Enter to open github in your browser... 7080-B9B3
✓ Authentication complete.
- gh config set -h github git_protocol ssh
✓ Configured git protocol
HTTP 422: Validation Failed (https://api.github/user/keys)
key is already in use

(最后一行的错误不用管)

这里我用的是ssh-key,之前使用Git的时候就已经配置了的,gh会自动找到公钥。
如果出现了问题,可以看我之前的文章:Git配置

完成后输入gh auth status即可看到登录状态:

(base) PS C:\Users\dahu> gh auth status
github
  ✓ Logged in to github as CQUPTLei (oauth_token)
  ✓ Git operations for github configured to use ssh protocol.
  ✓ Token: *******************

2.22 常用命令

(1)在Github仓库中打开当前项目

进入本地仓库目录下,使用gh browse命令

(base) PS D:\chatGPT_based> gh browse
Opening github/CQUPTLei/chatGPT_based in your browser.

可选参数:

-b, --branch
打开传入的分支名
-c, --commit
打开上一次提交
-n, --no-browser
不打开浏览器,打印目标URL
-p, --projects
打开项目仓库的Project页面
-R, --repo <[HOST/]OWNER/REPO>
使用[HOST/]OWNER/REPO格式选择另一个存储库
-s, --settings
打开项目设置
-w, --wiki
打开项目仓库的wiki页面

(2)gh配置 gh config

查看全部gh配置:gh config list
查看指定项的配置:gh config get
对某项进行设置:gh config set,如gh config set editor vim,将编辑器设置为vim

当前支持的配置项:

git_protocol: 克隆、推送使用的协议 (default: "https")
editor: 文本编辑器
prompt: toggle interactive prompting in the terminal (default: "enabled")
pager: 在终端中切换交互式提示,默认开启
http_unix_socket: the path to a Unix socket through which to make an HTTP connection
browser: 用于打开URL的web浏览器

如:

(base) PS C:\Users\dahu> gh config list
git_protocol=ssh
editor=
prompt=enabled
pager=
http_unix_socket=
browser=
(3)克隆仓库 gh repo clone

此方式比较简便只要用户名和仓库名(OWNER/REPO)就可以了,如:gh repo clone cli/cli
(使用仓库url也同样支持)

将cli克隆到D盘:

(base) PS D:\chatGPT_based> cd ..
(base) PS D:\> gh repo clone cli/cli
Cloning into 'cli'...
remote: Enumerating objects: 43767, done.
remote: Counting objects: 100% (415/415), done.
remote: Compressing objects: 100% (246/246), done.
remote: Total 43767 (delta 224), reused 317 (delta 168), pack-reused 43352 eceiving objects: 100% (43767/43767), 26.41 MiB | 6.34 MiB/s
Receiving objects: 100% (43767/43767), 26.62 MiB | 6.36 MiB/s, done.
Resolving deltas: 100% (29558/29558), done.

总30几M,时间2秒左右。不过我不能确定是不是gh的功劳,因为我本来就快😁😁😁。大家可以试试。

我删除又使用git克隆了一下: git clone git@github:cli/cli.git
用时5s左右,感觉差不多。

又用两者克隆了一个700M的仓库,感觉差不多,可能是我网络好(开了proxy)。

目前,在我这里,gh repo clonegit clone的区别主要是命令的精简性好。

(4)issue

命令有:

  1. gh issue create
  2. gh issue list
  3. gh issue status
  4. gh issue close 等等
    如:
(base) PS D:\chatGPT_based> gh issue create --title "I found a bug" --body "Nothing works"

Creating issue in CQUPTLei/chatGPT_based

https://github/CQUPTLei/chatGPT_based/issues/1

更多示例:

$ gh issue create --title "I found a bug" --body "Nothing works"
$ gh issue create --label "bug,help wanted"
$ gh issue create --label bug --label "help wanted"
$ gh issue create --assignee monalisa,hubot
$ gh issue create --assignee "@me"
$ gh issue create --project "Roadmap"
(5)pull

Create a pull request :gh pr create

$ gh pr create --title "The bug is fixed" --body "Everything works again"
$ gh pr create --reviewer monalisa,hubot  --reviewer myorg/team-name
$ gh pr create --project "Roadmap"
$ gh pr create --base develop --head monalisa:feature

List pull requests in a GitHub repository:gh pr list

List PRs authored by you
$ gh pr list --author "@me"

List only PRs with all of the given labels
$ gh pr list --label bug --label "priority 1"

Filter PRs using search syntax
$ gh pr list --search "status:success review:required"

Find a PR that introduced a given commit
$ gh pr list --search "<SHA>" --state merged
(6)add、commit和push

好像现在只能用原来的git命令实现这些功能。

之前好像有gh repo push之类的命令。

我正在质问Github CLI官方,稍后更新。

作者刚刚回复我了:

意思就是说gh没有提交、推送的功能,这几项缺失的功能还是使用以前的git commit等命令来实现。

gh的优势就是

• Users, organizations, access-controlled repositories,
• Issues, Discussions,
• Pull Requests, code review,
• GitHub Actions, Codespaces,
• Gists,
• and more.

本文就当是了解了一下Github CLI吧,目前大部分工作还是会用Git,可能未来这个工具会被广泛应用吧。

2.3 总结

基础功能上感觉和Git的命令行功能差不多,这个功能更多,可能我还没用到。

克隆仓库倒还好用,

三、 Github访问优化

一般改改hosts就行了,把Github的DNS解析添加到本地就行了。

或者使用那个,你懂得。

修改hosts文件,可以用这个插件:https://github/aardio/Ghips

下载了打开就能用:先刷新,在更改hosts就行了,没必要打开自动刷新,发现打不开了在再用这个软件修改下就行了。

更改git混DNS解析后记得刷新DNS缓存:

ipconfig/flushdns

本文标签: CLIGitHubLINEcommand