前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >你会在命令行下高效管理 Github 上的项目吗,用上这个神器后助你秒实现!

你会在命令行下高效管理 Github 上的项目吗,用上这个神器后助你秒实现!

作者头像
iMike
发布2020-04-15 10:34:22
4910
发布2020-04-15 10:34:22
举报
文章被收录于专栏:运维之美运维之美

对于大多数使用 Git 作为版本管理的技术人员来说,应该都接触过 GitHubGitHub 就像技术人员的淘宝一样,里面充满了好东西,时时刻刻都可能给你惊喜!

很多人可能不仅在 GitHub 上寻找合适的车轮子,还可能会为造车轮子贡献自己的力量,往往会使用一些基本操作来完成,典型的为:

  • Fork
  • PR (pull request)

当然,如果你是项目的维护者,还会使用 Merge 等操作。

但是,我想很少人会使用过 GitHub 的命令行接口 Hub, 通常的操作我们都可以通过友好的 Web 界面,点几个按钮来完成,简单实用!所以很少有需求会迫切需要一个命令行工具来完成这些操作,但是如果需要批量操作时 (比如:清除多个 Repositories 的时候),你会发现一个一个在 Web 上来操作的确不够高效。这时如果有命令行工具可以快速进行批量操作,那就是极好的。

今天就给大家推荐一个 GitHub 的命令行工具 Hub,其官方主页上是这样介绍的:

git + hub = github

Hub 命令是对 Git 命令的一层封装,利用 GitHubAPI 可以轻松的扩展 Git 的能力,比如常见的 Pull Requests 都可以通过命令行来实现。

项目地址:https://github.com/github/hub

安装 Hub

Hub 的安装很简单,基本上所有的主流平台上都支持一键安装。

由于 Hub 是对 Git 命令的封装,安装前请保证机器上的 Git 版本在 1.7.3 或以上。

如果你使用平台不在上面列表中,你也可以直接在官方项目的 Releases 页面下载 Hub 的二进制包进行安装。

为了快速实现通过二进制包安装,你还可以使用下面这个脚本来简化操作步骤。

代码语言:javascript
复制
# 这里以 Linux 平台为例,如果是其它版本或平台,只需简单替换 VERSION 变量和对应文件名前缀即可。
VERSION="2.12.8"
wget https://github.com/github/hub/releases/download/v$VERSION/hub-linux-amd64-$VERSION.tgz
tar xzvf hub-linux-amd64-$VERSION.tgz
sudo ./hub-linux-amd64-$VERSION/install

配置 Hub

当第一次和 GitHub 有交互时会弹出用户名和密码用来生成 OAuth TokenToken 保存在 ~/.config/hub 文件中。或者你也可以通过 GITHUB_TOKEN 环境变量来进行授权,其值是拥有 Repo 权限的 Access Token

如果你使用的是 ZSH,还可以给 Hub 配置一个自动完成。

代码语言:javascript
复制
# Setup autocomplete for zsh:
mkdir -p ~/.zsh/completions
cp ./hub-linux-amd64-$VERSION/etc/hub.zsh_completion ~/.zsh/completions/_hub
echo "fpath=(~/.zsh/completions $fpath)" >> ~/.zshrc
echo "autoload -U compinit && compinit" >> ~/.zshrc

echo "eval "$(hub alias -s)"" >> ~/.zshrc

使用 Hub

常用命令介绍

代码语言:javascript
复制
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <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 # 使用 hub clone 命令,可以省去指定 GitHub 端仓库的部分。
   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
   reset      Reset current HEAD to the specified state
   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
   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
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   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   # hub push 命令支持通知向多个远程仓库进行 push 操作。

'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.

These GitHub commands are provided by hub:

   browse         Open a GitHub page in the default browser
   ci-status      Show the CI status of a commit
   compare        Open a compare page on GitHub
   create         Create this repository on GitHub and add GitHub as origin # hub create 命令适用于本地已经创建仓库,但 GitHub 端没有创建仓库的情况。
   fork           Make a fork of a remote repository on GitHub and add as remote # hub fork 命令的功能与 GitHub 页面的 Fork 按钮相同。
   issue          List or create issues
   pr             Work with pull requests
   pull-request   Open a pull request on GitHub # hub  pull-request 命令为我们提供了创建 Pull Request 的功能,利用这个命令可以在不访问 GitHub 页面的情况下创建 Pull Request。
   release        List or create releases

使用实例

这里以一个开源项目贡献者的身份为例,你可以使用命令来拉取代码、浏览页面、Fork Repos 和提交 Pull Requests 等等。

  1. Fork 一个项目

要在 GitHub 上进行开发,往往会基于一个已有的开源项目,所以首先需要 Fork 这个项目。

代码语言:javascript
复制
$ hub clone github/hub
Cloning into 'hub'...
remote: Counting objects: 10646, done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 10646 (delta 4), reused 0 (delta 0)
Receiving objects: 100% (10646/10646), 3.25 MiB | 58.00 KiB/s, done.
Resolving deltas: 100% (6302/6302), done.
Checking connectivity... done.
$ cd hub/
$ hub fork
Updating chengweiv5
From git://github.com/github/hub
 * [new branch]      1.11-stable -> chengweiv5/1.11-stable
 * [new branch]      1.12-stable -> chengweiv5/1.12-stable
 * [new branch]      gh-pages   -> chengweiv5/gh-pages
 * [new branch]      master     -> chengweiv5/master
 * [new branch]      skip_completion_script_for_windows -> chengweiv5/skip_completion_script_for_windows
new remote: chengweiv5

这里和 Web 上的操作有点不同,从 Web 上是首先找到一个项目,然后点击一下 Fork, 然后会在自己的空间内创建这个项目。

而使用 Hub, 则首先是 Clone 下来原有的项目(以 hub 项目为例,hub clone github/hub),然后再执行 Fork 子命令。完成后,可以看到本地添加了一个 Remote,而且通过 Web 页面也可以看到自己的空间里已经添加了一个叫 hub 的项目,Forkgithub/hub

  1. PR (Pull Request)

在本地完成一些开发后,可能想要将 Patch 提交给 Upstream 项目,在 GitHub 中,向上游提交 Patch 通过 PR 来完成。下面我们以 sb2nov/mac-setup 为例,来看一看整体过程。

代码语言:javascript
复制
$ hub clone sb2nov/mac-setup
Cloning into 'mac-setup'...
remote: Counting objects: 1635, done.
remote: Compressing objects: 100% (49/49), done.
remote: Total 1635 (delta 33), reused 0 (delta 0)
Receiving objects: 100% (1635/1635), 3.69 MiB | 59.00 KiB/s, done.
Resolving deltas: 100% (941/941), done.
Checking connectivity... done.
$ cd mac-setup
$ hub fork

完成 Fork 后,将文档进行一个小修改,diff 如下:

代码语言:javascript
复制
$ git diff
diff --git a/SystemPreferences/README.md b/SystemPreferences/README.md
index a148d74..a7ff953 100644
--- a/SystemPreferences/README.md
+++ b/SystemPreferences/README.md
@@ -1,7 +1,7 @@
 # System Preferences
 
 First thing you need to do, on any OS actually, is update the system! For that: **Apple Icon > Software Update.**
-Also upgrade your OS incase you want to work on the latest OS. Mavericks is a free upgrade so please check that.
+Also upgrade your OS incase you want to work on the latest OS. Yosemite is a free upgrade so please check that.
 
 If this is a new computer, there are a couple tweaks you would like to make to the System Preferences. Feel free to follow these, or to ignore them, depending on your personal preferences.

git pull-request 会检查你在 GitHub 上的自己的项目和上游项目相应的 Branch 是否有不同。所以,首先将这个修改提交到自己的项目中,Push 就行。

代码语言:javascript
复制
$ git commit -asm "Yosemite is the latest Mac OS X now"
$ git push chengweiv5
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 391 bytes | 0 bytes/s, done.
Total 4 (delta 2), reused 0 (delta 0)
To git@github.com:chengweiv5/mac-setup.git
   16df764..e25031f  master -> master

然后,提交 PR,如下:

代码语言:javascript
复制
$ hub pull-request 
https://github.com/sb2nov/mac-setup/pull/27

注:为了统一命令操作,你可以直接将 Hub 命令设置为 Git 命令的别名,让执行 Git 操作的时候实际上是在执行 Hub 命令。别名设置方法如下:eval "$(hub alias -s)"

除了以上例子外,Hub 还有许多有用的命令,比如:打开浏览器查看项目、Merge PR,新建 Repo 等等。

代码语言:javascript
复制
# open the current project's issues page
$ hub browse -- issues
→ open https://github.com/github/hub/issues

# open another project's wiki
$ hub browse mojombo/jekyll wiki
→ open https://github.com/mojombo/jekyll/wiki

# Create a new repository
$ hub create sinatra/recipes
[ repo created in GitHub organization ]
> git remote add -f origin git@github.com:sinatra/recipes.git

# Delete an existing repository
$ hub delete sinatra/recipes
[ repo deleted in GitHub organization ]

这里就不再一一介绍了, 感兴趣的读者可以参考 Hub 官方文档进一步探索更多好玩好用的高级功能。

参考文档

  1. https://www.google.com
  2. https://www.jianshu.com/p/10b6e8d9420f
  3. http://www.chengweiyang.cn/2015/01/24/learn-github-hub/
  4. http://einverne.github.io/post/2018/10/use-hub-command-to-interact-with-github.html
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-04-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 奇妙的Linux世界 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装 Hub
  • 配置 Hub
  • 使用 Hub
    • 常用命令介绍
      • 使用实例
      • 参考文档
      相关产品与服务
      命令行工具
      腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档