hub:使用命令行使用 GitHub

hub 是一个命令行 git 扩展,帮助你在不离开终端的情况下完成日常 GitHub 任务。

阅读完整文档:man hub 或访问此项目的 GitHub 页面

# install with Homebrew (macOS, Linux)
# or see other installation options
brew install hub

hub version
git version 2.25.0
hub version 2.14.2  ← it works!

# indicate that you prefer HTTPS to SSH git clone URLs
git config --global hub.protocol https

在命令行上保持高效

hub 可以轻松克隆或创建存储库浏览项目页面列出已知问题,确保你的本地分支保持最新,以及通过 Gist 共享日志或代码片段

# clone your own project
hub clone dotfiles
→ git clone git://github.com/YOUR_USER/dotfiles.git

# clone another project
hub clone github/hub
→ git clone git://github.com/github/hub.git

# fast-forward all local branches to match the latest state on the remote
cd myproject
hub sync

# list latest open issues in the current repository
hub issue --limit 10

# open the current project's issues page
hub browse -- issues
→ open https://github.com/github/hub/issues

# open another project's wiki
hub browse rbenv/ruby-build wiki
→ open https://github.com/rbenv/ruby-build/wiki

# share log output via Gist
hub gist create --copy build.log
→ (the URL of the new private gist copied to clipboard)

开始新的项目从未如此简单

# create a repo to host a new project on GitHub
git init
git add .
git commit -m "And so, it begins."
hub create
→ (creates a new GitHub repository with the name of the current directory)
git push -u origin HEAD

降低参与开源贡献的障碍

无论你是开源的初学者还是经验丰富的参与者,hub 都可以让分叉存储库,检查分支的 CI 状态,甚至从编写和提交代码的相同环境提交拉取请求变得更容易。

hub clone octocat/Spoon-Knife
cd Spoon-Knife
# create a topic branch
git checkout -b feature
# make some changes...
git commit -am "done with feature"

# It's time to fork the repo!
hub fork --remote-name origin
→ (forking repo on GitHub...)
→ git remote add origin [email protected]:YOUR_USER/Spoon-Knife.git

# push the changes to your new remote
git push origin feature

# check the CI status for this branch
hub ci-status --verbose

# open a pull request for the branch you've just pushed
hub pull-request
→ (opens a text editor for your pull request message)

为了乐趣和利润自动化任务

现在,你可以列出或创建问题、拉取请求和 GitHub 发布,并且格式自选,脚本化更容易。

# List issues assigned to you that are labeled "urgent"
hub issue --assignee YOUR_USER --labels urgent

# List the URLs of at most 20 pull requests based on the "develop" branch:
hub pr list --limit 20 --base develop --format='%t [%H] | %U%n'

# Create a GitHub Release from master using release notes from a file
hub release create --copy -F release-notes.txt v2.3.0
→ (the URL of the new release copied to clipboard)

深入了解 API

即使 hub 不支持你需要的准确特性,你也可以使用 hub api 手动向任何 GitHub API 发起请求,且 hub 可以为你处理身份验证、JSON 编码/解码和分页操作,甚至包括 GraphQL

# use contents of a file to post a comment on issue #123 of the current repo
hub api repos/{owner}/{repo}/issues/123/comments --field body=@mycomment.txt

# find a pull request that introduced a specific commit SHA into a repo
REPO="github/hub"
SHA="b0db79db"
hub api graphql --flat -f q="repo:$REPO type:pr $SHA" -f query='
  query($q: String!) {
    search(query: $q, type: ISSUE, first: 3) {
      nodes {
        ... on PullRequest {
          url
        }
      }
    }
  }
' | awk '/\.url/ { print $2 }'

请参阅 hub-api-utils 了解更多示例。

专为开源维护人员设计

维护一个项目更容易,因为你可以轻松从其他分叉中提取查看拉取请求关闭问题,甚至按 URL cherry-pick 提交

# fetch from multiple trusted forks, even if they don't yet exist as remotes
hub fetch mislav,cehoffman
→ git remote add mislav git://github.com/mislav/hub.git
→ git remote add cehoffman git://github.com/cehoffman/hub.git
→ git fetch --multiple mislav cehoffman

# check out a pull request for review
hub pr checkout 134
→ (creates a new branch with the contents of the pull request)
# make new commits, then update the pull request
git push

# close an issue
hub issue update 134 --state closed

# directly apply all commits from a pull request to the current branch
hub am -3 https://github.com/github/hub/pull/134

# cherry-pick a GitHub URL
hub cherry-pick https://github.com/xoebus/hub/commit/177eeb8

# open the GitHub compare view between two releases
hub compare v0.9..v1.0

# put the compare URL for a topic branch to your clipboard
hub compare --url feature | pbcopy

在工作中使用 GitHub

通过为代码审查开放拉取请求和一次推送多个 远程来节省工作时间。甚至支持GitHub 企业版

# have hub recognize your GitHub Enterprise hostname
git config --global --add hub.host my.example.org

# transfer an issue to another repo
hub issue transfer 123 NEWREPO

# open a pull request with title & body from a file
git push origin feature
hub pull-request --copy -F prepared-message.md
→ (the URL of the new pull request copied to clipboard)

# push to multiple remotes
hub push production,staging

请参阅 完整参考文档 以了解更多信息。