Accessing GitHub from the command line

Reading Time: 3 minutes

I am always looking for ways to speed up my workflow, using tools to either automatize the tasks or making them easier to perform. Recently I was working on a project in which I had to create several pull requests per day and very often. So I looked for a way to improve this process and I found an awesome tool that helps me do it.

Hub

hub is a command-line wrapper for GitHub, it gives you several commands to interact directly with the web interface and also makes some improvements to several git commands.

Installation

If you are on a OS X environment (and you have brew already running), all you need to do is run brew install hub.

In a Unix-based system you can install it from source with the following commands:

git clone https://github.com/github/hub.git
cd hub
rake install prefix=/usr/local

Hub will ask you for your GitHub credentials the first time you use it. After that it will use an OAuth token for future calls to the API.

Usage

Hub will enhance some git commands, among which is git init, so let's say you are initializing a new project, then, if you do:

hub init -g

Hub will initialize a new git repository locally. It will create the repo using your GitHub credentials and finally it will add this repository as remote origin.

But, where I think Hub really shines is when you use the custom commands that it adds.

For instance, if you have a git repo already initialized, you can run:

hub create [NAME] [-p] [-d DESCRIPTION] [-h HOMEPAGE]

This will create a new GitHub repository and add it as remote origin, in which NAME is the name of the repo. If it is omitted, the name of the working directory is taken as the name of the repository. p is a flag to make the repo private and, well, the other options are pretty descriptive.

Browse

You can browse among the subpages of a project using the browsecommand.

hub browse [-u] [[USER/]REPOSITORY] [SUBPAGE]

By default Hub takes the current directory project as the project to browse if you don't specify one. Also, the default subpage is tree (index), but you can tell Hub the subpage you want to visit. For example, if I'm in the buggy project directory:

#/projects/buggy

hub browse

It opens the buggy GitHub page in the default browser.

#/projects/buggy

hub browse -- issues

Opens buggy's issues subpage in the browser.

To specify a different page.

#/projects/buggy

hub browse ribbit commits

Opens the ribbit commits subpage.

To specify a different user, do:

#/projects/buggy

hub browse rails/rails pulls

Which opens the rails pull requests subpage.

The -u flag is to display the URL instead of opening the page in the browser.

Compare

You can compare branches, tags and SHA1s, by giving a range of one of them.

hub compare [-u] [USER] [[START...]END]

For instance if you want to compare the feature_a branch with the feature_b branch, the command will be:

hub compare feature_a...feature_b

This will open a tab on your browser with the comparison of the two specified branches.

If you don't specify a starting point it will take the 'base' branch which by default is master:

hub compare new_feature

It will open a GitHub compare view of the new_feature branch against master.

Pull requests

To create a pull request, use this command:

hub pull-request [-f] [-m MESSAGE|-F FILE|-i ISSUE|ISSUE-URL] [-b BASE] [-h HEAD]

If you don't specify the message with -m, it will prompt the default editor in which you can put the message and the description for the pull request. In the same way, the -b option will take master as the base branch and the -h will take the current branch as HEAD, so if I'm in the important_feature branch:

hub pull-request

Will make a pull request from important_feature to master.

This command will check if you have local commits that are not yet pushed to the remote, in which case, it will abort the operation. But you can skip this validation by using the -f flag.

Conclusion

As part of my daily routine, I always have an open terminal tab for my git commands, so Hub fit perfectly and seamlessly. It helped me to make my daily interactions with GitHub faster and easier. If you have a chance, check it out, it's a pretty cool tool.

Thanks for reading.

0 Shares:
You May Also Like