GitHub from the command line with hub

Getting Started

Now that you have a working version of hub, I'll introduce you to some of the basic functions.

When you get started, you'll likely want to create a new repo on GitHub. To do that, navigate to any folder where you want to initialize Git:

$ cd Documents
$ mkdir Test; cd Test/
$ git init
Initialized empty Git repository in /Users/SudeshnaSur/Test/.git/

Hub has now initialized a GitHub repository for you, but at the moment, it's empty because Git only sees files that have recently been changed. So now, place any file inside your new repo, and add some text to it using echo:

$ echo "Hello" > test.txt

Next, check the git status, and you'll see that, as of right now, there are no commits:

$ git status
On branch master
No commits yet
Untracked files:
test.txt
nothing added to commit but untracked files present

Next, run git add:

$ git add .

By using this command, you have now staged the changes but not yet committed them. In order to commit the changes, you'll need a GitHub account. If you don't already have a GitHub account, you can create one using this step-by-step guide [5].

Now you need to tell your local version of Git to use your account, so you'll need to provide it with your account's username and email address. These details will be contained in every commit message you add to any repo, so that anyone else looking at the code can see that you are the author.

To add these details to your local version of Git, run the following:

git config --global user.email "email@example.com"
git config --global user.name "Your Name"

If you're worried about sending your email out to everyone who has access to a particular repo, there are also ways of hiding it. There's a feature to allow you to hide your email address, or you can just set your email address to your GitHub email address, in order that other users only see your GitHub credentials [6].

So now that everything is linked up, you can begin to make commits. First, navigate to the location you were in before – and where you created a Git repo. Then run:

$ git commit -m 'Adding a test file'
[master (root-commit) 07035c94e038] Adding a test file
1 file changed, 1 insertion(+)
create mode 100644 test.txt
$ git status
On branch master
nothing to commit, working tree clean

And that is all: You've now initialized a repo, made a change, and committed it. The change will appear in the repo against your email address. This is the basic process of working with hub, and you'll quickly see that it's far faster than working with the GitHub GUI directly.

Going Further

Up until this point, you've used hub to complete tasks that you could have done easily on the GitHub GUI. However, as just a little taste of the more advanced features of hub, I'll show you how to automatically create a new GitHub repo from the local system.

If you run hub create, hub will automatically create a new repo on GitHub with the same name as the current directory on your local system:

$ hub create
Updating origin
https://github.com/YourRepo/Test

Even better, using the following command will even link your repository with the remote mirror:

$ git remote -v
origin git@github.com:YourRepo/Test.git (fetch)
origin git@github.com:YourRepo/Test.git (push)

Everyday Tasks

Most of the basic tasks that you use GitHub for can now be done straight from the command line. These tasks include cloning or creating repositories, browsing project pages, listing issues with repositories, or just staying up to date with projects you are following.

To clone a project, run:

$ hub clone project_name

To clone another project:

$ hub clone github/hub

To fast-forward all local branches to match the latest version on the project:

$ cd myproject
$ hub sync

To list the latest issues in the current repository and limit the number of issues returned to 10:

$ hub issue --limit 10

To see all the issues of a repository on the project's issues page:

$ hub browse -- issues

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Perl: CMS with GitHub

    With its easy-to-use web interface, GitHub can be put to totally different uses than archiving code. For example, Perlmeister Mike Schilli used GitHub to deploy a content management system for simple websites.

  • Git Cheat Sheet

    Frequently used Git commands at a glance.

  • Perl: Collaborate with GitHub

    GitHub makes it easier for programmers to contribute to open source projects by simplifying and accelerating communications between project maintainers and people willing to contribute.

  • Git Hooks

    The pre-commit framework lets you automatically manage and maintain your Git hook scripts to deliver better Git commits.

  • Branch Management

    Git makes version control as simple as possible. To manage your Git branches successfully, you need to learn the ins and outs of git branch and git merge.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News