首页 > 其他分享 >【Github CLI】Take GitHub to the command line

【Github CLI】Take GitHub to the command line

时间:2023-01-22 10:03:38浏览次数:44  
标签:Git git CLI -- GitHub Github gh


目录儿~

  • ​​一、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),但还是点进取来学习一下。

【Github CLI】Take GitHub to the command line_git

以前的提交、推送、克隆这些工作都是在相应的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.com/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.com/​

2.2 gh的使用

官方命令文档:​​https://cli.github.com/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.com
? 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.com in your browser... 7080-B9B3
✓ Authentication complete.
- gh config set -h github.com git_protocol ssh
✓ Configured git protocol
HTTP 422: Validation Failed (https://api.github.com/user/keys)
key is already in use

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

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

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

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

2.22 常用命令

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

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

(base) PS D:\chatGPT_based> gh browse
Opening github.com/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,CLI,--,GitHub,Github,gh
From: https://blog.51cto.com/cquptlei/6021424

相关文章