Using GitHub’s web interface may be a tedious and time-consuming affair. This can consequently slow down your work process and make the entire workflow rather complex. But thanks to GitHub, there’s a better and more efficient tool that oils the cogs of your workflow and makes the process run smoothly. GitHub is an online platform for hosting repositories and allows users to collaborate on projects. Hub is a command line tool from GitHub that wraps itself around Git and allows you to extend additional features when working with GitHub. This thereby increases the efficiency of working on your workflow and saves time. First I will start with the hub installation on different Linux distros and then will show few examples of hub command.
Installation of Hub
For MAC OSX users:
$ brew install hub $ alias git = hub
Installation from source code
$ git clone https://github.com/github/hub.git $ cd hub make install prefix=/usr/local alias hub = git
Debian 8 and Ubuntu 17.04
First, we will install ruby and we will use apt-get command
# apt-get install ruby
Run the below curl command to download the file
# curl https://hub.github.com/standalone -Lo /usr/bin/hub
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 82495 100 82495 0 0 36386 0 0:00:02 0:00:02 --:--:-- 36405
Edit file permissions
Change permissions chmod 755 /usr/bin/hub
Run hub –-version to check your version of hub
# hub --version git version 2.11.0 hub version 1.11.0-1-g7639acd
Fedora 22 / 23
DNF, package manager is available from Fedora 18 version and you can simply run below command to install hub.
# dnf install hub hub version git version 2.9.3 hub version 2.2.3
You can easily use yum command to install hub package using below command
# yum install hub
After downloading Git, a quick and easy way to verify if it’s installed and working properly is by issuing the command git help
# git help
Hub works best when aliased as git. Git commands will still work only that hub spices things up.
# alias hub=git
Creating a repository
We need to first create a directory in our PC.
# mkdir hub-demo
Navigate to the directory
# cd hub-demo/
Initialize the directory as a repository
# hub init
Initialized empty Git repository in /home/jamie/hub-demo/.git/
The above command initializes a git local repository. It turns directory on the PC into a git repository. This gives you the power to create a version control history for the directory’s contents.
Keeping track of local changes
For you to visualize the staging area and keep abreast with the changes in your local repository, run hub status command. This will keep track of each file’s status in the initialized git repo. The statuses include staged for commit, modified and untracked.
On branch master Initial commit nothing to commit (create/copy files and use "git add" to track)
Beginners are encouraged to use this command to keep track of what’s going on in their repositories.
Checking hub’s man page
Head out to your terminal and type
hub help hub
HUB(1) Hub Manual HUB(1) NAME hub - git + hub = github SYNOPSIS hub [--noop] COMMAND OPTIONS hub alias [-s] [SHELL] Expanded git commands: git init -g OPTIONS git clone [-p] OPTIONS [USER/]REPOSITORY DIRECTORY git remote add [-p] OPTIONS USER[/REPOSITORY] git remote set-url [-p] OPTIONS REMOTE-NAME
Adding files waiting for commit status
This stages previously unmarked or untracked files but modified.
First, I’m going to create a new file
# touch script.py
I’ll then add the script.py file
# hub add script.py
A flag suffix -A can be used to stage all untracked files in the repository
# hub add -A
Saving all staged files
Hub commit -m “Message goes here”
Hub commit commits files to code history. It saves a snapshot of all staged files together with the old versions of previously unstaged files.
e.g. hub commit -m "First commit"
In our hub-demo directory run
# hub commit -m “First Commit”
[master (root-commit) 60e549f] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 script.py
The -m flag facilitates creation of a commit message detailing the changes made in the current commit. It therefore keeps you organized and able to track progress of your workflow.
Viewing entire history
This gives you a glimpse of your entire history, commit by commit. It captures the commit message, timestamp and unique commit identifier
# hub log
commit 60e549ffe5a85193d5d4279c4e189ee21b3d3048 Author: NaiCity <[email protected]> Date: Sun Nov 19 13:00:45 2017 +0300 First commit
This visualizes your code history and reverts to old commits where things were working as desired.
Seeing line by line changes made in your files
To see any changes previously made to your files,run hub diff command. I’m going to add a line to my script.py
# echo "print "hello" " >> script.py
# hub diff
diff --git a/script.py b/script.py index e69de29..7b5c34c 100644 --- a/script.py +++ b/script.py @@ -0,0 +1 @@ +print hello
The last line indicates the modification made to the file.
What advantages does working with remote repos offer?
Local repos allow you to create a saved history of your repos organized by commits. Remote repositories offer extended advantages over local repositories. Remote repositories allow you to showcase your code and come in handy especially when employers would like to have a view of what you’ve been working on. They also allow you to back up your code on an online platform and facilitate collaboration with other programmers and developers.
Updating a remote repository
This is where you link your local repo to a remote repo.
First head to GitHub, https://github.com. Sign up using your email Id, username and password. Click on ‘New Repository’ to create a repository. Provide a repo name and select your preferred options for the other choices. Hit on ‘Create Repository’ button. Copy the link of your repo and head back to your terminal.
Use hub remote add origin
# hub remote add origin https://github.com/NaiCity/HTML5-CSS3.git
You’ll be prompted for a username and password
Username for 'https://github.com': NaiCity Password for 'https://[email protected]': Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 290 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/NaiCity/HTML5-CSS3 60e549f..0174a5d master -> master Branch master set up to track remote branch master from origin.
If you refresh your repository page, you’ll see your newly synchronized repository.
Collaborating with hub
Cloning a repository
Cloning a repository refers to taking a remote repository and making a copy out of it. It initializes a local repository containing contents from a remote repository. This, therefore creates a connection between local and remote repositories. Let’ back out of our hub-demo directory
# cd ..
Create a new repository in GitHub. Let’s call it shared-projects. copy its link location and head back to your terminal.
Type hub clone
# hub clone https://github.com/NaiCity/shared-projects.git
Cloning into 'shared-projects'... remote: Counting objects: 3, done. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 Unpacking objects: 100% (3/3), done.
Verify the existence of shared-projects directory
# ls # jamie shared-projects
This synchronizes your remote repository with your local repository and ensure they are up to date. It’s a combination of hub fetch + hub merge.
Head over to GitHub and make a few changes to the README.md file located in shared-projects repo and commit the changes. Now, the README file will be different from the one in the shared-projects in the local repository.
Go to the terminal and execute hub pull
# hub pull
A branch is a parallel history of commits to the repo’s main history (Master branch). Branches are used to develop features and work on bugs without affecting the progress and integrity of master branch. They work both in remote and local repos and it’s imperative that they are kept in sync. This is used to develop features in isolation and merge them back to the main code. The default branch is always the master.
NOTE: local branches are not seen in remote repo until you issue hub push command.
Creating a new branch Magnum
# hub checkout -b "Magnum"
M README.md Switched to a new branch 'Magnum'
Type hub branch to switch branch to ‘Magnum’
# hub branch
* Magnum Master
Create a new file and edit it.
# echo “Branching demo” > > JAMIE.md
# hub add JAMIE.md
Commit the file
# hub commit -m “Adds JAMIE branch”
[Magnum 4874e3e] Adds Jamie 1 file changed, 1 insertion(+) create mode 100644 JAMIE.md
Update the remote repo
# hub push origin Magnum
Username for 'https://github.com': NaiCity Password for 'https://[email protected]': Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 298 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) To https://github.com/NaiCity/shared-projects.git * [new branch] Magnum -> Magnum
When you head out to Github you’ll get a notice of a newly created branch under the shared-projects repository
As you can see, ‘Magnum’ branch has different files from Master file. Hub fetch updates local branches with tracking information about remote branches. It, however does not overwrite code you’ve been working on.
Hub merge combines the history of both local and remote branches.
Switch back to master
# hub checkout master
M README.md Switched to branch 'master' Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded. (use "git pull" to update your local branch)
# hub merge origin/Magnum
Updating 308f6f2..4874e3e Fast-forward JAMIE.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 JAMIE.md
check out the history
# hub log
commit 4874e3e8f3aa8c01387f9f4a0ffb9f6f589f3ec2 Author: NaiCity <[email protected]> Date: Sun Nov 19 17:34:50 2017 +0300 Adds Jamie
It has added Jamie branch!
Hope you enjoyed using this tool and we really appreciate your comments on this article.