Archives

How to Create a Repository on GitHub

Now that you have created an account with GitHub, you are ready to create a new repository. This would be a good time to check out our Git guide on using remote repositories. Basically, remote repositories are important for the following reasons:

  • Provides a “hub” for your code
  • Supports collaboration
  • Decentralizes your code to protect against loss

How to Create a Repository at GitHub

While you can create your own repository on your private server, the steps below demonstrate how you can create a repository at GitHub.

  1. Log into GitHub
  2. Under Your repositories click New repository
  3. Fill in the Repository name
  4. Click Create repository

Explanation of Options

While you can create a repository with only a name, there are some other options you can select in advance that may help optimize your repository.

Description The optional description of your repository should be short and sweet. You don’t need too much information here, but it might help to distinguish this repository in a large list.
Public or Private If you have a free, personal account with GitHub then Public is your only choice.
Initialize this repository with a README The README file is a detailed description of the project which can include special instructions, contribution policy, FAQ, and more. If your program requires more detailed documentation, you should consider using the Wiki feature that comes with your repository.
Add .gitignore Various programming languages and operating systems create extraneous files you don’t need to track. GitHub lets you select a programming language and automatically creates a .gitignore files with rules that will ignore those extraneous files. This will keep unwanted files out of your project.
Add a license This option lets you quickly and easily add a popular softare license to your repository. This will help other people know how they are permitted to use your code.

Where to go from here…

Well done on your completion of this tutorial. In the above article we discussed how you can create a remote repository at GitHub as opposed to hosting one of your own. While it’s easy to host your own repository, GitHub has done a lot of the work for you. You may be wondering what the next step might be.

If you have any questions about this article or using Git in general, please leave a comment below.

 

Using Branches in Git

Git allows you to do work in new “branches” separate from your main “master” branch. In effect, “checking out” a branch means actually copying and swapping your working directory files, so you can change them as you please, and switch back to another branch later. This means you can do as much testing and tinkering as you need without affecting your project. Later, you can merge changes from one branch into another. In this article, we’re going to discuss using branches effectively.

Typical Use Cases

We will go through two typical use cases for the branching feature. In both of these cases it will be important to keep our working directly clean and work in a separate branch. When changes are finalized, they can be merged back into a “master” branch and pushed to a production repository.

Testing a New Feature

An example of a typical situation involves a “master” branch, which is pushed to your production repository. Locally, if you want to test a new feature without affecting the master, you can create a new branch called “test”. The files in your working directory are bsically an exact copy of the master files, which you can alter as needed. If you like the changes you’ve made, you can switch back to “master” and merge the “test” changes into “master”.

Applying a Quick Fix

Likewise, imagine you are working on “test” but you receive an urgent request to apply a fix. You can switch back to master and “check out” a new branch called “fix”. Once you have applied the fix, switch back to master, merge “fix” into “master” and push changes to the production repository. Then, you can go back to “test” and keep working.

  1. In your working directory, apply the following command to create and “check out” your files to a new branch:
    git checkout -b fix
    
  2. Having created a new branch, it is safe to edit the files
  3. Once the fix has been applied, commit the changes:
    git commit -am "fixed that broken thing"
    
  4. Now that changes have been made and tested, switch back to the master branch:
    git checkout master
    
  5. Merge the fix branch with master:
    git merge fix
    

The steps for merging the “fix” and “test” branch will be the same.

How to Use Tags in Git

Throughout the life of your project, you will reach various milestones. With Git, we mark significant improvements and modifications with commits, but often there will be events that will require more annotation, such as version changes. In this article, you’ll learn about how to use tags in Git to mark significant events in the life of your project.

If you are new to Git, be sure to check out our introduction to Git and installation guide.

Before proceeding with tagging, we will want to highlight the different types of tags available. There are two main tag types: lightweight and annotated.

For a quick tag, merely a reference to a certain commit, the lightweight tag is sufficient. However, the annotated tag is recommended because it contains more detailed metadata that might be helpful to you later.

How to add a Lightweight Tag

Adding a lightweight requires only a quick and easy “tag” command with no options:

git tag v1.0

Now, a tag of “v1.0” will reference the most recent commit. To use this tag as a reference use this command:

git show v1.0

This output of this command will be a display of the commit referenced and changes that were made in that commit. The below command line output demonstrates this:

christopher@server $ git show v1.0
commit 0659595374f673bdffcc5a9d8b08efb145834132
Author: ChristopherM <chris@example.com>
Date: Wed Nov 1 10:13:25 2017 -0400

Menu position adjustment

How to Add an Annotated Tag

In order to add an annotated tag, you will use the same “tag” command. However, there are a few options to add in this case. Use the -a option to make this tag “annotated” and the -m command to provide the tag with a message (similar to a commit message).

git tag -a v1.2 -m "my version 1.2"

Then, to show the output of this tag’s reference, us the Git “show” command again, as in the below command line instance:

christopher@server $ git tag -a v1.2 -m “my version 1.2”
christopher@server $ git show v1.2
tag v1.2
Tagger: ChristopherM <chris@example.com>
Date: Wed Nov 1 16:08:36 2017 -0400

my version 1.2

commit 0659595374f673bdffcc5a9d8b08efb145834132
Author: ChristopherM <chris@example.com>
Date: Wed Nov 1 10:13:25 2017 -0400

Added new test feature

Well done! You now know how to add tags with Git.

How to Sign Tags and Commits with Git

You may remember in our previous articles, we learned how to commit changes in our project to Git. We also learned how to add tags to various commits in order to mark special milestones. Now we’re going to learn how to “sign” tags and commits with GPG.

Signing commits and tags is a way of verifying that a certain commit or tag has been verified by a certain user. This can be part of a contribution policy or as a protective measure to make sure that a commit is coming from a legitimate source.

Before you can sign tags and commits, you must make sure you have a GPG (or GnuPG) key available. This key is installed on your computer, and you can use it to sign and encrypt emails or to encrypt and decrypt files. In the example below, we will use this key to sign our tags and commits.

How to Add a GPG Key to Your User Configuration

In order to use the GPG key on your computer, you will need to add it to the config file. You may remember in the article on adding files to Git, we configured our username and email address we wanted to use for Git. We are going to follow a similar procedure here to add our key.

First, make sure you have a key ready to use.

christopher@server$ gpg --list-keys

Here is the output of the above command:

pub   1024R/84487D41 2017-11-15 [expires: 2017-11-22]
uid                  Chris Maiorana (CC Team) <chris@example.com>
sub   1024R/32B16A59 2017-11-15 [expires: 2017-11-22]

The key you will want to use for signing is your public key labelled “pub” above.

In order to add your key to the Git configuration, open your terminal app and run this command with the numbers following the forward slash after “pub”:

git config --global user.signingkey 84487D41

How to Sign Commits

Signing commits is easy. All we need to do is add the -S option to the git commit command.

  1. Open your project via command line or SSH
  2. Navigate to the project directory
  3. Use the git commit with the following options:
    git commit -a -S -m "Your commit message"

The -a option adds changes to the staging index automatically, the -S options signs the commit with your GPG key, and the -m option allows you to put your commit message in quotes following the command.

The output of the above command will look similar to this:

christopher@server$ git commit -a -S -m "Your commit message"

You need a passphrase to unlock the secret key for
user: "Chris Maiorana (CC Team) <chris@example.com>"
1024-bit RSA key, ID 84487D41, created 2017-11-15

master
[master ef3fab3] Your commit message
 1 file changed, 748 insertions(+), 740 deletions(-)

How to Sign Tags

It’s easy to sign tags with the addition of the -s option to the git tag command. Remember that the tag will be assigned to the most recent commit.

  1. Open your project via command line or SSH
  2. Navigate to the project directory
  3. Enter the following command:
    git tag -s your tag -m "your tag message"

After you have signed your tag, you can view it later with the git show command:

christopher@server$ git show v1.4
tag v1.4
Tagger: ChristopherM <chris@example.com>
Date:   Wed Nov 15 12:35:44 2017 -0500

Version 1.4 Signed
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iJwEAAECAAYFAloMevAACgkQ19n8W4RIfUGwBgQArTjp8UtKvmt1RBEvlNvZ7Qe0
W+jzYRUQxLh2eXBA5jIpaqHRT4RMQ7qEpwJs1w+Iwj1XcaEWZuH3dFE+Ic3KhY3h
msiJdIxTOQU8MyJ9c5f5DBSSVULNFj0ibmU3P85XF0W9DMGIoyUtReBnFImPYaoo
CjPowzRQ9Vi7SDRtz04=
=ojrH
-----END PGP SIGNATURE-----

commit 136c043c1cb6b5baa72ead64aba468e1982e60d0
Author: ChristopherM <chris@example.com>
Date:   Wed Nov 8 18:04:35 2017 -0500

    Version 4 (commit message)

Well done! You now know how to sign tags and commits in Git using GPG keys.

Using Your Github Account

In this article, we’re going to cover some of the basic functions and settings you have in your GitHub account. Using your GitHub account properly may mean different things for different users. In this article we’re going to cover the following:

  • Log into your account
  • Access your settings
  • Delete your account

After reading this article you will know how to move in and out of your GitHub easily. To make sure you have a solid foundation in Git, check out our Introduction to Git article.

How to Log Into GitHub and Find Your Account Settings

Easy enough, you can log into GitHub right from the front page of the site. Then, you will have an avatar you can click on to find your settings.

  1. Proceed to GitHub.com
  2. Click the Sign In button at the top right of the page
  3. Use your GitHub Username or the Email Address associated with your account
  4. Click on your avatar and select Settings

You are now able to access your account settings. Here you can tinker with a variety of settings from your public profile to security and notifications and lots of stuff in between.

How to Delete Your GitHub Account

  1. Go to your settings page
  2. Click the Account tab
  3. Click Delete your account under the Delete account heading
  4. Fill in the deletion form by confirming your username and printing “delete my account”

Next Steps

You now know how to access your account settings and delete your account with Github. Using your GitHub account effectively is one step on the road to web mastery. Be sure to check out some of our other Git articles for tutorials ranging from beginner to intermediate.

 

Connect Local Project to a Github Repository

In this article, we’re going to learn how to connect a local project to a Github repository and—on the flip side—how to “clone” a GitHub repository to your computer. We have already demonstrated how you can use your InMotion Hosting account as a Git server, but we are using free Github hosting in this example.

There are two cases we will take into account here.

  1. Connect a local project to a remote repository hosted by GitHub
  2. Clone a GitHub repository as a local project

The second case automatically creates a working directory with all the remote repository settings in place. This is the popular option if you want to work on somebody else’s code or if you are going to be working on your code from a different workstation.

How to Add Your GitHub Repository to an Existing Project

In this first case, we are going to take the following points as given:

  • You have a local project under Git management
  • You want to use a remote repo at GitHub
  • All you need to do is add the GitHub repo as a remote location for your local project

As we have done in previous tutorials, we’re going to be using your operating system’s native command line utility to complete these steps. All you need to do is open your computer’s default Terminal app.

  1. Once you are on a command line, navigate to your project’s directory
  2. Optional: to make sure you’re in the right place, you can type git status to receive git-related output:
    chris@computer# git status
    On branch master
    Your branch is up-to-date with 'origin/master'.
  3. With your web browser, log into your GitHub account
  4. Open your desired repository
  5. Copy the remote repository URL by clicking the Clone or download button followed by the copy icon
  6. Back in your terminal app, copy the remote repository URL after the git remote add command (substitute “name-of-repo” with whatever word you would like to associate with this remote repository, or call it “origin”):
    chris@computer# git remote add name-of-repo <URL>
  7. Use git remote -v to check if the remote repository was added successfully

How to Clone a Repository from GitHub

This case is a little easier to accomplish and requires only the following circumstances:

  • You are interested in a certain remote repository on GitHub
  • You want to copy all of the working files for that project to your computer
  • You want the local instance to have the “origin” repository already added

In order to accomplish the above, all you need to run is one simple command, git clone followed by the remote repository URL.

  1. Open your terminal app
  2. Navigate to the desired location on your computer (a spot where you would like the project directory to reside, usually your user’s home folder or “~”)
  3. Copy the remote repository URL from GitHub, as we did in the above case
  4. Run the git clone command followed by the URL you copied:
    chris@computer# git clone <URL>

 

Following this command, Git will automatically grab all of the contents of the remote repository and copy them into a new directory named after the repository itself.

 

How to Commit Changes in Git

Now that we have learned how to install git and start adding files to our staging index, we can now learn how to how to commit changes in Git.

Committing tracked files to the repository means that we are adding changes to the permanent record of the project. Imagine this process as similar to recording media to a cassette tape. New information is recorded, but you can play back your recording at any time, and you can also record over your previous recordings.

In Git, every recording is known as a “commit.” You are committing to changes you’ve made and recording them for future reference.

How to Make your Commit and Note It

To commit the changes you’ve made, you will only need to run a simple command in your terminal.

git commit

This command will launch your default text editor in order to get your “commit message.” The commit message is meant to be a brief statement regarding what this commit accomplishes. For example, if you added a new test feature, your commit message might look like this:

Added new test feature
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#       modified:   index.html
#
# Untracked files:
#       #index.org#
#       .#index.org
#

You’ll notice the commit message at the top, highlighted in green. Below, commented out, you will see which file was modified in order to make this change. You will also see information about untracked files in the project. Once you save changes and close the text editor, the commit will be complete.

Skip the Editor

Often, it is faster to skip your text editor and make your commit message on the same command line as your commit itself. In order to do this, all you need to do is add the -m option to the command:

git commit -m "Added new test feature"

This command will provide Git with a new commit along with the message “Added new test feature.”

Automatically Adding Changes

Remember from the previous article, about adding files to Git, that you must run the add command again in order to add file modifications to the staging index. Adding files only once does not mean that future changes will be staged or committed.

This means you will need to run the add command again in order to add new changes to the staging index.

git add .

Once staged, the modifications (and any new files that have just been added) can be committed to the repository.

However, it’s quite easy to skip the the add command and go straight to your commit. All you need to do is add the -am option to the commit command:

git commit -am "Added new test feature"

The above command will stage and commit any changes made to the project. Now you know how to commit changes to the repository in Git. You are ready to learn about deploying files to your server.

Why or Why Not Use Github?

We have already studied how you can use Git to manage versions of your software project. We have also shown you how you can use your InMotion Hosting server to host your own Git repository. Now, we will discuss why or why not use GitHub to host your remote repositories.

Reasons Why You Might Consider Using GitHub

Aside from providing a colorful, user-friendly history of your project, Github offers some extra benefits that may be important for some developers such as:

  • Community building
  • Free hosting
  • Project management features

We will discuss these in more detail below.

Community Building

A lot has been said about building communities around open-source software. GitHub is designed to encourage and support collaboration between developers, designers, and end users alike. An individual can even request help on their code from other members of the community.

It’s Free

GitHub provides a great deal of services to you at no cost. You can open a personal plan for free and create as many repositories as you need. Essentially, this is free hosting for your code which you can share with others.

Though GitHub is free, you can upgrade if you want more options. For example, premium members get access to private repositories. This means no one can see your code as you’re working on it.

Project Management

GitHub provides a wide variety of project management features for free. Once your project is hosted at GitHub, users can submit Issues which you can resolve and close. You can also set Milestones for your project. Though these features are all optional, they may help you plan and meet important deadlines for your project.

Reasons Why You Might Not Use GitHub

It’s possible that GitHub is not a necessary tool for your project, especially if you already have space you can use on your hosting account. When choosing whether or not to use any tool, it’s best to consider whether or not the tool adds significant value to your work. Even though GitHub is free to use, it would be a waste of time to set up an account and repositories if you’re not going to use them.

Repositories are Public by Default

This means the default, free, personal account cannot create a private repository. Everything about your project, including log messages, commit history, and the code itself, will be publicly available to anyone. This also means that proprietary information or security-sensitive data cannot be shared safely. Private repositories are available, but they come at higher cost.

If your project meets any of the criteria above, and you are interested in using GitHub to host your code, proceed to our next article about creating your free GitHub account.

 

How to Use a Custom Domain with GitHub Pages

In our previous GitHub Pages article, we showed you how you can create a static site and host it in your GitHub repository. This is a simple option for creating a site—with considerable limitations.

Your GitHub Pages site will have its own URL based on your username and the name of the repository. However, you may want to use a custom domain. In this article, we’ll show you how to use a custom domain with GitHub Pages.

Add a Custom Domain to Your Repository

Now, we will need to first add our custom domain to our GitHub repository. This will set up a record there, but the change will not take effect until we add the necessary DNS records to cPanel.

  1. Log into your GitHub account
  2. Select Settings under your repository
  3. Add your custom domain under the Custom domain section and click Save

A custom domain has now been added to your repository. Below, we’ll show how to add an A record in your cPanel that will point to GitHub.

Add A Records to Point Your Domain to GitHub

Now that we have added the necessary domain information to our GitHub account, we are going to create custom domain records in cPanel. This is like adding a physical address to your name in a phonebook. We will be pointing our domain to GitHub’s IP addresses.

  1. Check out our full tutorial on adding an A Record for cPanel
  2. Create one A Record for 192.30.252.153
  3. Create another A Record for 192.30.252.154

If your domain is not pointing to InMotion Hosting, you should be able to add DNS records at your registrar. Once these records have been created, allow at least 4-24 hours for the changes to propagate across the web.

How to Add SSH Keys to Your GitHub Account

In this tutorial:

GitHub offers a convenient SSH key feature for accessing your repositories on the command line. Instead of using usernames and passwords every time you push to or pull from your repository, you can use your key. In this article we’ll learn how to add SSH keys to your GitHub account.

Why use an SSH key?

Using an SSH key is a convenient and secure way to access your account. It’s convenient because you don’t need to remember a long password. And you can make your actual password so brutally long and secure that no human or bot could guess it. The SSH key works like an actual key that only you possess.

How to Create an SSH Key

In order to use an SSH key with Git, you must first create the key on your computer. If you already have an SSH key, you can skip these steps. In order to check if you have a key, you can run this command:

chris@server# ssh-add -l

Follow along with the steps below to create the key and copy its contents. You will need to open your computer’s default terminal application.

  1. Open the terminal app on your computer
  2. Enter the following command, substiting joe@example.com with your email address
    chris@server# ssh-keygen -t rsa -b 4096 -C "joe@example.com"
  3. Press Enter to accept the default file location
  4. Enter a secure passphrase
  5. Press Enter
  6. Enter this command to display the contents of your public key
    chris@server# cat .ssh/id_rsa.pub
  7. Copy the contents of your key to your clipboard (we will need it later)

 

It’s important to remember that the SSH key has two parts: a private key and a public key. As the name suggests, the private key is only for private use. Never ever share your private key with anyone (with exceptions made for your system administrator or other trusted people within your organization). This will be important later.

How to Add an SSH Key to your Github Account

Now we have already seen why you might want to use an SSH key. We also demonstrated how to create an SSH key on your computer.

As mentioned above, we have a private key and a public key. In the steps below you’ll learn how to upload your public key to GitHub.

  1. Log into your GitHub account
  2. Click your avatar and choose Settings
  3. Select SSH and GPG keys
  4. Click New SSH key
  5. Enter a title in the field
  6. Paste your public key into the Key field
  7. Click Add SSH key

Next Steps

Well done! You now how to add SSH keys to your GitHub account. Make sure to check out some of our other Git articles: