Saturday, January 19, 2013

Quick Start Guide for Github

According to the Wiki, “Github is a distributed revision control and source code management (SCM) system with an emphasis on speed.” 
To understand better, a comparison of Github with the Version Control Systems is a great option since most of us have used some or the other VCS. Here goes the comparison:
  •  Git thinks of its data more like a set of snapshots of a mini filesystem and stores every version as a reference to the snapshot of the filesystem at that point, unlike VCSs which think of the information they keep as a set of files.
  • Git stores the entire history of the project on your local disk, due to which most operations in Git only need local files and resources to operate unlike a VCS where most operations have that network latency overhead.
  • Git provides data integrity through its check-summing mechanism.
  • All the actions Git allows are undoable. As in any VCS, you can lose or mess up changes you haven’t committed yet; but after you commit a snapshot into Git, it is very difficult to lose.

The Three stages of Github

Git has three main states that your files can reside in:
  1. Committed: means that the data is safely stored in your local database.
  2. Modified:  means that you have changed the file but have not committed it to your database yet.
  3. Staged: means that you have marked a modified file in its current version to go into your next commit snapshot.

Setup and Start Using Github

After taking a walk through the Github basics, getting to the very aim of this post i.e Setup and Start Using Github

  • Firstly install git on your machine using synaptic or the command 
            sudo apt-get install git
  • Next create an account on git, which requires any email id, a username and a password.
  • Once the account is setup, you need to set the ssh keys required for authenticating your account/local repo to github. Before this if any operation is attempted like :  
            git  push -u origin master  
       are tried, you'll get errors like :
            “Permission denied (publickey).
            fatal: The remote end hung up unexpectedly ”
       So proceed for setting up the ssh keys.

  • Steps for setting up SSH Keys
1. Check for SSH keys: 
$ cd ~/.ssh
Checks to see if there is a directory named ".ssh" in your user directory. If yes, proceed with the step 2, else jump to step 3.

2. Backup and remove existing SSH keys :
i. $ ls (Lists all the subdirectories in the .ssh folder)
config  id_rsa  known_hosts
authorized_keys  id_dsa  known_hosts
ii. $ mkdir key_backup (makes a subdirectory called "key_backup" in the current directory_
iii. $ cp id_rsa* key_backup (Copies the id_rsa and files into key_backup)   
iv. $ rm id_rsa*
3. Generate a new SSH key.
        $ ssh-keygen -t rsa -C ""
       (this email id is the one you must have used when creating  the github account)
Output would be like :
      Generating public/private dsa key pair.
      Enter file in which to save the key (/home/abc/.ssh/id_dsa): <press enter>
      Enter passphrase (empty for no passphrase): ******
      Enter same passphrase again:  ******
      Your identification has been saved in /home/abc/.ssh/id_dsa.
      Your public key has been saved in /home/abc/.ssh/
              The key fingerprint is: 

  •  Add your SSH key to GitHub. 
    Open the SSH Keys option in your Github account and click on the "Add SSH Key" button. Copy the content of the /home/abc/.ssh/ file and paste here.  

    Then hit the Add button.

  • Test everything out.
       Try the command below :                  
            $ ssh -T
   If an error like :
           “Agent admitted failure to sign using the key.
           Permission denied (publickey).”
   occurs then try :
           $ ssh-add ~/.ssh/id_dsa
   Output would be :
           Enter passphrase for /home/abc/.ssh/id_dsa:******
           Identity added: /home/abc/.ssh/id_dsa (/home/abc/.ssh/id_dsa)
    Now try the same command :
          $ ssh -T
          Hi Jayati! You've successfully authenticated, but GitHub does not provide shell access.

Commit your code to a remote repository   

To add code to some remote repo on github:
  • Clone the repository to which you need to add a module/subfolder. 
            “git clone ” 
      This creates a folder sample_repository in the current working directory.
  • Copy the folder to be added to this cloned folder and place it the way you wish to add it to the repository. Cd to the cloned copy of the repository and run the following command to add the folder to the local repository
             “git add sample_sub_folder/*” 
             “git add sample_sub_folder/pom.xml” 
    if only one of the components(for eg. pom.xml) of the folder is to be added.
  • Next commit the added folder using   
             “git commit -m “message”
             “git commit -a”  (this opens a shell to add the commit message) 
  • Run the pull command:
             “git pull”
  • Run the push command:
             “git push”

  • To revert a commit: 
             “git revert HEAD ” 
That's it. Enjoy Githubing ... 

No comments:

Post a Comment