Thursday, June 2, 2016

Git with windows - using GitHub


As explained in last post you came to know how to commit the changes done at your local working directory. Lets start with Github and working with its repositories



GitHub


GitHub is a hosting service for Git repositories. You can store your local committed files in Github repositories and collaborate with your team members.


Create your account in GitHub using this link : GitHub



We need to generate SSH key for our GitHub account. Well, lets form the SSH key using following command:




$ ssh-keygen -t rsa -C "Enter your email id here"



Example code:



$ ssh-keygen -t rsa -C "sukhesh222@gmail.com"



After executing this command your bash terminal will asks for file to save public key and passphrase. Enter the details. Now it will displays the file location where your public key has been stored. Copy the key from the file and paste in GitHub and then click on "Add SSH key" button as specified in below path:




Login to GitHub account->Profile->settings->SSH and GPG Keys




Now create your repository in GitHub account. For demo I had created repository named "DemoProject".



Push operation



Now we need to add the remote to our local project. Lets run the following command.




$ git remote add DemoOrigin https://github.com/sukhesh222/DemoProject.git




As given in the command we are adding a new remote location for our project and given a name for our remote as "DemoOrigin" and the remaining url is a path of our repository.



Now lets push our locally committed folder to the repository using following command.




$ git push -u DemoOrigin master




This command pushes and stores all the files presented in the staging area to GitHub repository. Refer the detailed coding sample screenshot given here:





You can access these files using your github url. As shown in examples here I can access my Github repository and project using the link "https://github.com/sukhesh222/DemoProject.git".









Clone operation


Clone command is used for downloading the files from your Github repository to your local system. we will create a folder named "downloaded" inside our local working directory "myProject". Then lets execute the clone command as given below.




$ git clone https://github.com/sukhesh222/DemoProject.git c:/xampp/htdocs/myProject/downloaded




This command downloads files from "DemoProject.git" repository to our local folder "downloaded".




Git with windows - Basics

Git is a source code management system. It was basically developed by Linus Torvalds.It is a open source distributed version control system (DVCS).


Advantages


1. Here data which presents on your local machine mirrors the data in GitHub repository, hence chances of data loss are very less.


2. As the name "version control system" indicates, we can manage various versions of same project, and also helpfull to reverse or backtrack the changes.


3. A group of people can work together, all using the same files. Files are stored on Github, every team members can download these files and work locally i.e. on the same project.


Installation on windows


You can download and install it from this link: Git For Windows

As GUI is also available, but I prefer the command line.


Lets introduce myself to the Git. Run the following commands on your terminal i.e. on GitBash


Syntax:




git config --global user.name "Enter your name here"
git config --global user.email "Enter your mail id here"


Here is a example code:



$ git config --global user.name "sukhesh"
$ git config --global user.email "sukhesh222@gmail.com"



Lets create our local project directory "myProject" using following commands



$ cd c:
$ cd xampp
$ cd htdocs
$ mkdir myProject
$ cd myProject


Lets initialize the Git:


Assume it contains 2 files (index.php, jsonLogin.php) and two folders(css,js). For demo purpose I had created these files in "myProject" folder. Now, lets initialize the Git in this folder. Hit the following command :



$ git init


Now you can see .git folder has created inside "myProjects" folder.






Adding files to the staging area:


Now you need to add files to the  staging area. Staging area is a place which holds the files ready for next commit. So that you can commit those files/folders.


Either you can add all the files/folders at single shot to the staging area using "git add ." command or you can also add individual files as shown in the below example



$ git add .


You can add individual files as shown below:



$ git add css
$ git add js
$ git add index.php
$ git add jsonLogin.php



Checking the status


Now you have been successfully added your files to the staging area. You can check it using "git status" command




$ git status



Commit command


Now you can commit the files by using following command



$ git commit -m "some description about commit comes here"



Here is a demonstration code:



$ git commit -m "committing the initial changes"



Here is the complete code segment: