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 is an extremely fast, efficient, distributed version control system ideal for the collaborative development of software.
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 "email@example.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:
git add *
git commit -m "commit message"
Let me explain each command I typed:
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:
ssh-keygen -t rsa -C "firstname.lastname@example.org"
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
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 email@example.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:
To unset the environment variable:
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:
git remote add origin firstname.lastname@example.org: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).