Git is a very powerful DVCS ( Distributed Version Control System ). It is a program that allows you to back up and share all of your code, while providing an efficient interface for collaboration with other programmers. I think social coding and open source is an excellent practice, so I want to promote more awareness of Git and give a short tutorial of how to get started.
The first thing you want to do is download all the git components.
sudo apt-get install git-core git-gui git-doc
This next step may be a little difficult for beginners, so I’ll document it well. If you understand key-value pairing and public key authentication, you can skim through this and just get the commands. If you already have a key then skip to the next section.
Generate A Public/Private Key
To generate a key we’ll use a built in keygen to create an RSA (Rivest, Shamir, Adleman) key-value pair. At the risk of over simplifying things, RSA is a form of encryption that uses a hash table to generate a value from a given key. Basically you give it a special key, it runs it through the hash function and spits out your value. Hashing is a very powerful technique that has a wide range of uses, here however we use it to create a public/private key which is often used as a form of online identity verification/signature.
ssh-keygen -t rsa -C "email@example.com"
the ‘ -t ‘ flag specifies which type of key to create (we’re using the RSA hash function) then, the ‘ -C ‘ flag allows us to append a comment, here it’s your email. It will ask you where to save the key, I recommend saving it to the default location (Press Enter). Then you are to enter your passphrase, this should be a strong passphrase that only you know. It then saves your identification in the aforementioned directory.
Register Your Key With Github
We need to copy our key from the newly created id_rsa.pub file. We can use xclip to automatically copy everything in the file to the clipboard.
sudo apt-get install xclip cat ~./ssh/id_rsa.pub | xclip -sel clip
cat means to concatenate. ~./ssh/id_rsa.pub is the file extension to your public key. | xclip -sel clip funnels the output from cat into xclip where it copies it to the clipboard.
Now we create an account at github.
- Click on the Sign up Now button, and select the free account at the top.
- Do the typical account registration dance until you have an account.
- Click on the Account Settings link from the top tool bar.
- Select SSH Public Keys from the links on the left.
- Choose Add another public key
Leaving the title blank will use your email let’s do that, then simply left-click in the key text box and select paste. Then Click Add key
Customize Your Git Environment
Here we’re going to set a few environment variables to make your Git experience smoother. Each variable has three scopes.
- –system – This is a system wide variable change.
- –global – This is a user wide variable chane.
- Then there is a .gitconfig file in each project directory
Each subsequent scope overrides the pervious. e.g. Changing the GIT_COMMITTER_EMAIL variable in the –global scope overrides the GIT_COMMITTER_EMAIL variable in the –system scope. So let’s set up your variables.
git config --global user.name "mathnathan" git config --global user.email "firstname.lastname@example.org"
There are a few things Git does without being connected through SSH, so you should set up your account token to allow Git that freedom.
- Click on Account Settings
- Click on Account Admin
- Copy your API Token from there
and paste it here…
git config --global github.user mathnathan git config --global github.token 0123456789abcdef0123456789abcdef
Create a Repository and Get Coding
Now you just need to create a repository and begin coding! First logon to your github account.
- Click on the Dashboard link from the tool bar at the top.
- Click on the New Repository button
- Choose a project-name
- Keep the description short and sweet.
- Now click the Create Repository button.
Now cd into the directory where you plan to have your project files, then running init will establish a git database which will keep track of your progress and allow you to share your code, and join the social coding network!
cd /into/your/project/directory git init
There are a few basic git commands you should get comfortable with…
git add filename git commit -m "this is what I've done" git remote add origin email@example.com:mathnathan/project-name.git git push origin master
- The add command puts your filename in the staging area ready to be committed. That means you’re finished editing it for now and are ready to work on another.
- The commit -m “this is what I’ve done” saves your changes with a comment about what you’ve done to your local git database
- The git remote add origin firstname.lastname@example.org:mathnathan/project-name.git tells git where it is sending your local database when you use the push command.
- Once you’re finished editing for the day, or you want to back everything up, you can push your local data base which is at the master branch in your git file system to your origin which should now be email@example.com:mathnathan/project-name.git
Strengthen Your Git Muscles Here
- Help at Github – This is a good place to come for trouble shooting, checking FAQS, or asking the github community for help.
- Pro Git – This is the best resource for learning everything Git.
Contact me on github and let me know how this tutorial worked for you! Happy Coding.