公众号关注 「奇妙的 Linux 世界」

设为「星标」,每天带你玩转 Linux !

对于大多数使用 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 的二进制包进行安装。

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

# 这里以 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 配置一个自动完成。

# 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" >> ~/.zshrcecho "eval "$(hub alias -s)"" >> ~/.zshrc

使用 Hub

常用命令介绍

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 onework on the current change (see also: git help everyday)add        Add file contents to the indexmv         Move or rename a file, a directory, or a symlinkreset      Reset current HEAD to the specified staterm         Remove files from the working tree and from the indexexamine the history and state (see also: git help revisions)bisect     Use binary search to find the commit that introduced a buggrep       Print lines matching a patternlog        Show commit logsshow       Show various types of objectsstatus     Show the working tree statusgrow, mark and tweak your common historybranch     List, create, or delete branchescheckout   Switch branches or restore working tree filescommit     Record changes to the repositorydiff       Show changes between commits, commit and working tree, etcmerge      Join two or more development histories togetherrebase     Reapply commits on top of another base tiptag        Create, list, delete or verify a tag object signed with GPGcollaborate (see also: git help workflows)fetch      Download objects and refs from another repositorypull       Fetch from and integrate with another repository or a local branchpush       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 browserci-status      Show the CI status of a commitcompare        Open a compare page on GitHubcreate         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 issuespr             Work with pull requestspull-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 这个项目。

$ 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 为例,来看一看整体过程。

$ 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 如下:

$ 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 PreferencesFirst 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 就行。

$ 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.git16df764..e25031f  master -> master

然后,提交 PR,如下:

$ hub pull-request
https://github.com/sb2nov/mac-setup/pull/27

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

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

# 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

你可能还喜欢

点击下方图片即可阅读

用好了下一代文件系统 Btrfs 这些新特性,从此数据安全乐无忧!

点击上方图片,打开小程序,加入「玩转 Linux」圈子

更多有趣的互联网新鲜事,关注「奇妙的互联网」视频号全了解!

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

  1. 想在命令行下高效管理百度网盘吗?或许你应该用下这款神器!

    提到百度网盘,想必大家都很熟悉吧. 百度网盘自 2012 年上线运行以来,迅速积累了大量用户.但是狗改不了吃屎,作为百度的产品,百度网盘现在是越来越恶心了,不给充钱就限速,官方居然还不承认(百度网盘 ...

  2. kvm--virsh命令行下管理虚拟机

    virsh 既有命令行模式,也有交互模式,在命令行直接输入 virsh 就进入交互模式, virsh 后面跟命令参数,则是命令行模式: (1)基础操作 --- 命令行下管理虚拟机 virsh list ...

  3. ubuntu命令行启动浏览器_Ubuntu 秘笈之命令行下管理浏览器书签

    所有的现代浏览器都提供了一些形式的管理工具,虽然它们严格上来讲功能较少.如果你已经厌倦了这些内置在浏览器中的主流工具,你或许想要寻找一个替代品.这里介绍 Buku:一个命令行下的书签管理器.它不仅可以 ...

  4. Ubuntu 秘笈之命令行下管理浏览器书签

    导读 浏览器书签虽然不常被提及,但是作为互联网浏览的一部分.没有好的书签功能,网站链接可能会丢失,下次再不能访问.这就是为什么一个好的书签管理器很重要. 所有的现代浏览器都提供了一些形式的管理工具,虽 ...

  5. linux虚拟机启动网卡命令,命令行下无法联网怎么办,vmware下安装archlinux实现网络连接,实机grub引导启动linux...

    安装archlinux可参考: 百度·贴吧里也有置顶帖给出了官方维基: 由于用的电信宽带需要客户端(大学苦逼,需要客户端),无线网卡也不支持(cmcc和chinanet都需要登陆网页,命令行下无线网连 ...

  6. 查linux有哪些task_linux命令行todo列表管理工具Taskwarrior介绍

    Taskwarrior 是一款在命令行下使用的TODO列表管理工具,或者说任务管理工具,灵活,快速,高效. 安装 在ubuntu 14.04 中,可从官方仓库安装task软件包 sudo apt-ge ...

  7. WEBMIN在命令行下的安装

    从Webmin的主页(http://www.webmin.com)介绍,可以知道,Webmin内置一个web server,web server和所有的cgi程序都是用perl 5编写的.目前的Web ...

  8. 命令行下jq才是JSON 处理利器呀

    jq 简介 JSON 是一种轻量级的数据交换格式.其采用完全独立于语言的文本格式,具有方便人阅读和编写,同时也易于机器的解析和生成.这些特性决定了 JSON 格式越来越广泛的应用于现代的各种系统中.作 ...

  9. 在cmd命令行下编译运行C/C++源文件

    一直用java来写程序,java配置好jre路径之后,在cmd下编译运行,很方便. 刚好要给一个舍友改下C程序,想到可不可以像java一样在环境变量里配置好C的编译路径呢? 于是上网搜了一下,得到如下 ...

最新文章

  1. C# winform combobox默认选中项方法
  2. fast system call 快速系统调用
  3. paddleocr win10 编译
  4. SQL注入原理深度解析
  5. PAT_B_1029_Java(20分)
  6. oracle授权操作
  7. php腾讯云+视频上传失败,腾讯云视频上传和播放尝试总结
  8. 苹果新机发布在即 供应链齐泼冷水:卖不了7000万台
  9. 装饰器python详解_python装饰器详解
  10. java类学习_Java常用类学习
  11. 计算机的使用知识,计算机基础知识计算机的使用方法
  12. ESP-Tuning Tool 使用手册
  13. Delft3D建模、水动力模拟方法及在地表水环境影响评价丨Delft3D标量输运、波浪、拉格朗日粒子及溢油模型
  14. 软件资源版权声明与免责声明
  15. 工控机CF卡槽无法使用的解决方案
  16. 【实战篇】——keras合并多个模型
  17. 播放器实战07 av_read_frame与av_seek_frame
  18. QTP工具简单操作使用说明
  19. MySQL(十三):分区表( Partitioning Table)
  20. 聊斋2聂小倩java华语版,九个版本的聂小倩,看全的没有几个,经典也不是王祖贤!...

热门文章

  1. 一个人自律有多可怕?
  2. Android Studio中进行单元测试和UI测试
  3. DVDRip论坛不完全手册
  4. 【优化模型】面试顺序模型
  5. Drm 例程2 双dumb buffer显示
  6. 编码器知识汇总(增量式/绝对式/绝对值)
  7. 日记侠:你真的知道怎么发朋友圈吗?附发送时间表
  8. 上海宝付的反套路灵魂“八”问,扎心又清醒
  9. linux运行国服英雄联盟,从UKUI 3.0桌面截图当中得知,Linux能运行腾讯游戏英雄联盟了...
  10. 1109:开关灯(C C++)