Date Archives

February 2014

Create A Repository

In the previous post, GitHub For Beginners , I showed you how to set up Git. In this post i will show you how to create your first Repository.

Let’s get familiar with some git commands that we will use or you will come across them.

Git-Specific Commands

There are a lot of commands used in Git. However, we will go through the basic terms.

git init: Initializes a new Git repository. Until you run this command inside a repository or directory, it’s just a regular folder. Only after you input this does it accept further Git commands.

git config: Short for “configure,” this is most useful when you’re setting up Git for the first time.

git help: Forgot a command? Type this into the command line to bring up the 21 most common git commands. You can also be more specific and type “git help init” or another term to figure out how to use and configure a specific git command.

git status: Check the status of your repository. See which files are inside it, which changes still need to be committed, and which branch of the repository you’re currently working on.

git add: This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.

git commit: Git’s most important command. After you make any sort of change, you input this in order to take a “snapshot” of the repository. Usually it goes git commit -m “Message here.” The -m indicates that the following section of the command should be read as a message.

git branch: Working with multiple collaborators and want to make changes on your own? This command will let you build a new branch, or timeline of commits, of changes and file additions that are completely your own. Your title goes after the command. If you wanted a new branch called “cats,” you’d type git branch cats.

git checkout: Literally allows you to “check out” a repository that you are not currently inside. This is a navigational command that lets you move to the repository you want to check. You can use this command as git checkout master to look at the master branch, or git checkout cats to look at another branch.

git merge: When you’re done working on a branch, you can merge your changes back to the master branch, which is visible to all collaborators. git merge catswould take all the changes you made to the “cats” branch and add them to the master.

git push: If you’re working on your local computer, and want your commits to be visible online on GitHub as well, you “push” the changes up to GitHub with this command.

git pull: If you’re working on your local computer and want the most up-to-date version of your repository to work with, you “pull” the changes down from GitHub with this command.

Now that we have learnt how to walk, let’s take a walk in the park. 🙂

Creating an Online Repository

Both Git and GitHub refer to this as a Repository, or “repo” for short, a digital directory or storage space where you can access your project, its files, and all the versions of its files that Git saves.

Go to the GitHub and click New Repository. Give your project a name, “MyRepo“.  If u are working on a top secret project you will have to make it private. You will have to pay for that. The world needs to see you created a Repo. So go ahead and make it public. The Description section is used to give some info about the projects.

Don’t click the checkbox next to “Initialize this repository with a README.” Leave it unchecked for now A Readme file is usually a text file that explains a bit about the project. But we can make our own Readme file locally for practice.

CreateRpo

Finally click “Create Repository“. You have now created an online repository. Told you it would be a walk in the park. So let’s create a Local Repository.

Creating a Local Repository

We just created an Online Repository, but since we will be working locally (on your computer) , let’s mirror that repository we just made as a local directory. Brace yourself for some command line typing. Well you should be getting used to it already.

Step 1: Create a Directory

I like to keep my folders organised. I will create a folder called “GitProjects” that i will use to store all my Git related projects. This is optional though. Open Terminal and type

mkdir ~/GitProjects
mkdir GitProjects/MyRepo

mkdir is short for make directory. It’s a navigational command. The ~/ ensures that we’re building the repository at the top level of your computer’s file structure (Home Directory).

mkdir GitProjects/MyRepo creates a folder called MyRepo in GitProjects. 

Next navigate to MyRepo by typing:

cd ~/GitProjects/MyRepo

cd stands for change directory. It is also a navigation command.

Next type

git init

git. init stands for “initialize.” It tells the computer to recognize this directory as a local Git repository. If you open up the folder, it won’t look any different, because this new Git directory is a hidden file inside the dedicated repository.

git_init

Next, type:

touch README.md

touch really means “create.” This means we are creating a file called README. If you go to your folder, you’ll see an empty README.md file.

Open the README (Home/GitProjects/MyRepo) file and type “This is my first Repo :-)” Save and close the file.

In Terminal, type:

git status

We use the git status command to see what has been modified and staged.

git_status

 

Step 2: Add The README File

It’s time to commit. A commit is essentially a snapshot of all the files in your project at a particular point in time. Before we commit we need to mark the changes that have been committed. This is done by adding the new and changed files to the staging area. This creates a snapshot of the affected files.

We do so by typing the following in the Terminal:

git add *

By using * we are telling git to add All the files in the Directory.

Type git status command again to see the current status.

git_statusadded

Step 3: Commit The README File

After adding the files to the Git staging area, you can commit them to the Git repository. This creates a new commit object with the staged changes in the Git repository and the HEAD reference points to the new commit. The -m parameter allows you to specify the commit message. You should always write your commands in present tense.

Type:

git commit -m "This is the Initial Commit"

git _commit

Now that we’ve done a little work locally, it’s time to “push” our first commit up to GitHub.

So there is only one problem, our online and repository are not in sync.

Push Your Commit

Having both an Local and Remote(Online) Repository is the best present you could ask for. You can edit,delete and update your project without internet connection, at the same time show the world your finished project on GitHub.

This setup makes it easy to have multiple developers working on the same project and later upload or “push” your changes up to the GitHub repository when they’re ready.

To connect your local repository to your GitHub account, you will need to set a remote for your repository and push your commits to it. You will need to know the link you your Online Repository project. In my case it’s https://github.com/username/MyRepo.git.  The username in your case should me different.

Type the following in Terminal. You will be required to enter your GitHub credentials:

You will get the link from your Github online repository Repository. So replace the link in bold with the one from your online repo

Online_repo
git remote add origin https://github.com/username/MyRepo.git
git push origin master

You will be asked for your github username and password.

The following commands will enable git to  save your password in memory for some time:

git config --global credential.helper cache

This will save your password for some minutes.

git config --global credential.helper 'cache --timeout=9200'

origin onto it to indicate a new place from which files will originate.remote is a descriptor of origin, to indicate the origin is not on the computer, but somewhere online.

git_push

Now if you look at your repository on GitHub, you will see your README has been added to it.

repo

Mission Complete!

Congratulation, You are now a Git user, well if you have gotten this far. 😀

You will need to practice practice practice to become a git ninja.

Git Resources

What is have shown you is just the tip of the iceberg, that is can assure you. I did my best to show you how to walk. If you want to fly, you can use the following  resources:

  • Pro Git. An open source book on learning and using Git.
  • Try Git. CodeSchool and GitHub teamed up to make this speedy tutorial. If you want a little more practice with the basics, this should help. And if you have some extra money and want to learn everything there is to know about Git, Code School’s Git Real should do the trick.
  • GitHub Guides. If you’re a visual learner, GitHub’s official YouTube channel is worth your time. You may want to check out Git Basics four-part series.
  • Git Reference. Got the basics down but find yourself always forgetting the commands? This handy site is great as a glossary reference
  • Git – the simple guide. This tutorial is short and sweet, but it was a little too fast for me as a beginner. If you want to refresh on the basics of Git, this should be all you need.

GitHub For Beginners

There are already plenty of guides that explain the particular steps of getting Github on your machine. However, I had difficulty time trying to set it up for the first time. After several failed attempts at getting set up with GitHub Pages, I vowed that if I ever successfully set it up, I’d write a blog. Fortunately, I did eventually figure it out. Being a man of my words, here if the blog. 🙂 Don’t be scared if it did not work the first time. We will have things up and running .

Not being able to use Git in 2014 is a crime punishable by death. Hopefully by the end of this post you will be exonerated. So let’s get started.

What is Github?

Get this right, Github is not a development tool. GitHub is a web-based hosting service for software development projects that use the Git revision control system. It manages changes to a project without overwriting any part of that project. Why use something Git? Say you and a team mate are both updating a script on the same project. You make your changes, save them, and upload them back to the project. Mission complete. Hold your horses. Your coworker is  also working on the same script as you at the same time. After a long day of head banging and Googling, one of you is about to go home having your work overwritten and erased.

Version control application like Git keeps that from happening. You and your team mate can each upload your revisions to the same script, and Git will save both copies. You can later merge your changes together without losing any work. Now you can all go home smiling. You can even revert to an earlier version at any time, because Git keeps a “snapshot” of every change ever made. It has completely changed the way we work.

The GitHub site has extensive documentation on getting started, and if you’re not a fan of using the command line, the official GitHub apps for Mac and Windows are great. It is one of the best revision control systems that I have used. I use Github to manage and store some of my code on the cloud. Best of all, its all FREE. I mean who doesn’t like free stuff. :-). Thanks to Github, I can now access my code no matter where I am. So let’s go through the basics.

Step 1: Sign Up for GitHub

This is the first and the easiest part of setting up GitHub. You will need to create a Github account if you haven’t already. This is straight forward. Keep your email close; we will be using it again in a few.

signup_git

Step 2: Download & Install

You will need to download and Install Git for Windows, Mac or Linux. If you are on Windows or Mac, all you need to do is download and run the installer. For Linux users we will need to write some commands in terminal. So what are you waiting for, you should have opened terminal already.

download_git

  2.1: Ubuntu, Debian and derived systems

Type the following commands in terminal to install git.

sudo apt-get install git

 2.2: Ubuntu, Debian and derived systems

A windows version of Git can be found on the msysgit Project site. The URL to this webpage is listed below. This website also describes the installation process.

http://msysgit.github.io/

2.3: Confirm if Github is installed

Open Command prompt in windows and terminal in linux and type

git --version

If you successfully installed git, you will see something close to this. In my case i have version 1.8.3.4 installed.

git_version

if you have come this far with no issues, give yourself a pat on the back or better yet; buy me coffee 🙂

Step 3: User Configuration

Configure your user and email for Git via the following command. Type the following in Terminal. Remember to replace the Name and Email address with yours. The email address should be the one you signed in with. Put your user name in “userName” and your eMail address in  “email@gmail.com

git config --global user.name "userName"
git config --global user.email "username@gmail.com"

To check your Git settings, type the following in terminal.

git config --list

If you we are together, you should see the username and email address you entered.
All Git settings are described under the following link: git-config manual page

Step 4: SSH Keys(Optional)

SSH uses public-key cryptography to authenticate the remote computer and allow it to authenticate the user, if necessary. There are several ways to use SSH; one is to use automatically generated public-private key pairs to simply encrypt a network connection, and then use password authentication to log on.”

An SSH key basically lets your computer uniquely identify itself when it connects to servers. If Github is aware of the key your computer is using, you won’t have to enter your Github username/password every time you connect.

Check for pre-existing SSH keys on your computer

Let’s see if your computer has one or more keys already installed:

Open terminal and navigate to the SSH directory by typing:

cd ~/.ssh

If you get the response “No such file or directory”, go to Generating a new SSH Key.

Otherwise, you’ll need to backup and remove your existing SSH keys.

Backup and remove your existing SSH keys.
cd ~/.ssh
mkdir key_backup
cp id_rsa* key_backup
rm id_rsa*
Generating a new SSH key

Now we’ll create a new SSH key to use with Github.

 ssh-keygen -t rsa -C "your_email@domain.com"

When it asks you to enter a file name in which to save the key, just press return/enter (leave the prompt blank).

You will then be asked to enter a passphrase and confirm it. Don’t make this blank, and don’t make it an easily guessable. This prevents someone from easily acquiring and using your SSH key to impersonate you. Don’t worry, we are almost done.

Press return/enter after each time you’ve entered your selected passphrase. You won’t see the characters or bullets, the cursor will stay in the same spot as if you aren’t typing.

If you make an error entering your password one of the times, just press return and it will prompt you to try again.

Once you’ve successfully set your passphrase, the terminal will report that your key has been saved and show you some cool art.

Step 5: Add Your SSH Key to Github

In order for your computer to access Github without you having to enter your username/password all the time, Github needs to know the contents of the SSH key you just generated. The following code to copy the key to your clipboard so don’t worry if you cannot see the Key.

sudo apt-get install xclip

Now we need to copy the Key. To do so, type the following in terminal.

xclip -sel clip < ~/.ssh/id_rsa.pub

Alternatively, using your favourite text editor, you can open the ~/.ssh/id_rsa.pub file and copy the contents of the file manually.

Now we’ll add your key to Github:

  1. Visit your account settings.
  2. Click Add SSH key.
  3. Enter a descriptive title for the computer you’re currently on, e.g. “Work iMac” into the Title field.
  4. Paste your key into the Key field (it has already been copied to your clipboard).
  5. Click Add Key.
  6. Enter your Github password.

Step 6: Testing Everything Out.

Now let’s test if everything worked. To do so we will need to SSH Github.

In terminal, type:

 ssh -T git@github.com

git_success

Congratulations! Home Run.

You are now ready to create a repository which we will do that in another post. My bed is calling. All code and no sleep makes Tom a dull boy. Goodbye for now. 🙂