GitHub for beginners (debian/ubuntu/linux)

Just yesterday I was trying to setup my first github repository, but it wasn’t that easy I thought it would be. YES I know, github has a support page, but  it didn’t seemed to work for me.

So here, I’ll give you a little introduction on the initial setup of git on your local machine and setting up of your cloud repository on github. This tutorial is for debian/ubuntu users.

GIT
Git is an extremely fast, efficient, distributed version control system ideal for the collaborative development of software.

GITHUB
GitHub is the best way to collaborate with others. Fork, send pull requests and manage all your public and private git repositories. In simple terms, it is a cloud storage for all your repositories, be it local or distributed.

SETTING UP OF GIT REPOSITORY ON LOCAL MACHINE
NOTE: GIT CAN BE USED WITH/WITHOUT GITHUB. GIT is a subversion control system which can just be used solely on your local machine without using github at all.

First we’ll setup git on the local machine and create a local repository.
Install GIT using the command on your terminal:

sudo apt-get install git

This will install git on your machine. Now you we’ll start by doing a bit of configuration. Every commit you make will have your name and email address to identify the ‘owner’ of the commit, so you should start by giving it those values. To do so, run these commands:

git config --global user.name "your name"
git config --global user.email "your@email.com"

So Git knows who you are now. Let’s work upon the local repository we were talking about,  just imagine you are creating a simple PHP web app. (Of course, the bigger the project, the brighter Git shines, but we’re just learning the tools, right?) We’ve got an empty directory called ‘project’. Add all your project files in the new created empty directory. This is the base directory for your repository. To get started with your very own repository, first change your current working directory to the base repository directory and initialize git. Type:

cd /path/to/project/
touch README.md
git init
git add *
git commit -m "commit message"
git status

Let me explain each command I typed:

  • cd /path/to/project/: This just changes your current working directory to the project directory for setting up of repository.
  • touch README.md: README.md is a file used by github as a default readme file. So we create an empty file. This step is not necessary, but it surely makes you ready for uploading your repository on github.
  • git init: this initializes your git repository by making a “.git” directory as a sub-directory to your project directory. This is the magic black box used by git where all the different branches, logs and commits are kept.
  • git add *: This adds all the files to be committed to a staging area. The staging area is a spot to hold files for your next commit; maybe you don’t want to commit all your current changes, so you put some in the staging area. “*” is used to add all the files. You can be specific in adding files. Just use ‘*.js’ to only add javascript files.
    you can remove files from the staging area and subsequently from your commit using the command: git rm file_name
  • git commit -m “commit message”: It finally commits the staged files and logs the given commit message for you to recognize your commit later on when you want to rollback or branch.
    For skipping the staging area and updating all the already committed files use:

    git commit -am "commit updated"
  • git status: The git status command allows you to see the current state of your code. We’ve just done a commit, so git status will show us that there’s nothing new. If you continue working on your imaginary project, you’ll see that the status changes. If you have any new files, it will show you the files under “untracked files.” or if you have made changes to the files already committed, it will show you the files under “changed but not updated”. Again running the add and commit commands will add these new/changed files to a new commit.

You can checkout all the available commands/features on git here: http://git-scm.com/book/commands

you can similarly setup more repositories on your local machine. 🙂

SETTING UP GITHUB REPOSITORY

Git is a great way to share code with others and work on projects together. There are a number of Git repository hosting sites. We’ll just look at one.
First I expect you to make an account on GitHub here: https://github.com/

When you’re done with it, create a cloud repository. Instructions are given here: https://help.github.com/articles/creating-a-new-repository

So when your’re done with all this, you can go further and start committing your code on the cloud. For committing your code online, you will need a SSH public key, so let’s create that right now! Open your terminal and type:

cd ~
ssh-keygen -t rsa -C "your@email.com"

This creates a SSH public key to be used in connecting to GitHub. The t option assigns a type, and the C option adds a comment, traditionally your email address. You’ll then be asked where to save the key; just hitting enter will do (that saves the file to the default location). Then, just hit enter again twice when it asks you for a pass-phrase (pass-phrase is just a headache. If you type one, you will have to type one everytime you connect to github).

The key is stored in the default directory which is ~/.ssh/
Copy the contents of the public key. Here’s how you can do it from the terminal:

sudo apt-get install xclip

cd ~/.ssh/
cat id_rsa.pub | xclip

This copies the content of your public key to your clipboard. If you directly copy it using a text editor, remember not to add any newlines or white-space.
Now go to your GitHub account. Go to account settings. Click on SSH keys tab and finally click on add SSH key. Copy the contents of the clipboard in the key section. You can give any title you want. Now try connecting to the github server using SSH. Just type in your terminal:

ssh -vT git@github.com

You should be able to connect to the github server. If not, and getting an agent forwarding error you may have to unset SSH_AUTH_SOCK. You can read more about ssh and public keys and agent forwarding here:
http://www.unixwiz.net/techtips/openssh.html
http://www.unixwiz.net/techtips/ssh-agent-forwarding.html

To unset the environment variable:

unset SSH_AUTH_SOCK

Try and connect to the github server once again. If still not able to connect, use the github help: https://help.github.com/articles/

Once you are able to contact the github server, you can start uploading your commits to your cloud repository. Here’s how you can do that:

cd /path/to/project/
git remote add origin git@github.com:username/cloud_repository_name.git
git push origin master

This pushes the master branch to the origin remote. Now you’re project is available to the world! Head back to your project page and see your project. If you are getting an error in uploading the, again try connecting to the github server using ssh and debug the connection.

You might be on the other end of a project: you’re a contributor instead of the owner. When the owner pushes a new commit to the repository, you can use git pull to get the updates. Git pull is actually a combo tool: it runs git fetch (getting the changes) and git merge (merging them with your current copy).

git pull

7 responses to “GitHub for beginners (debian/ubuntu/linux)

  1. Hi Chirag,

    That was a very helpful article. There is an errata – on line 3 in the codebox for pushing master to origin, the command should read as :

    git push origin master

    Thanks much !

  2. Good article.

    I have found another errata in the the command “cat id_rsa.pub | xclip”, line 4 for copying the contents of RSA public key. You should replace by “cat id_rsa.pub | xclip – sel clip”, for accomplishing clipboard copy from the RSA file.

    Keep smiling!

Leave a Reply

Your email address will not be published. Required fields are marked *