Before introducing Git let me explain you what is version control system, Version Control System also know as VCS is simply a tool that allows you to manage changes to documents or source code and it’s often used by developers. While you working with any document or code file and in case you need to revert your old changes back to work directory and/or you accidently remove your local directory and you need the whole directory back VCS helps you in this typical situation. VCS is not just a tool which keeping track or backing up your data its can also shows which user update data at which time and at which line. And with the beauty of Version control system you can revert any changes.
What is Git?
Git is currently the most popular and advanced distributed version control system, let me mark a pin to the word distributed. There are Localized and Centralized version control systems but at this time we will only pin to the distributed version control system which Git have.
In distributed Version control system each user have his/her own local copy of the repository on his/her own computer. Another users can copy the an existing repository this method is called cloning. Each clone copy have the full history of the collection of the file and a cloned repository which have the same files and data as the original repository.
Git originates from the Linux kernel development and was founded in 2005 by Linus Torvalds. Nowadays it is used by many popular open source projects, e.g., the Android or the Eclipse developer teams, as well as many commercial organizations.
The core of Git was originally written in the programming language C, but Git has also been re-implemented in other languages, e.g., Java, Ruby and Python.
In the Git official website http://git-scm.com/ it defines information about installing Git on Various OS like Linux, IOS and Windows. At this point we are assuming you have already installed git into your local system. let us know in the comment if you got any trouble installing git.
The first step is to initialize Git in a directory. This is done using the init command. This action creates a .git folder under the directory which contains all the information related to that Git repository.
Lets start with a simple html project named “mysite”. Now you need to log in or register into web based Git repository like https://github.com/, or https://bitbucket.org/ . Assume you have already signed up and create a remote repository named same “mysite”.
Here in our local we create a folder named “mysite” under our destination directory. And initiate that directory into git repository by using command
$ git init
If you have just installed git you need to add the credential information for all your local repository. You don’t need to go throw this step every time you create repository.
$ git config –global user.name “hardik”
$ git config –global user.email “firstname.lastname@example.org”
Now lets create index.html file under “mysite” and add some html code.
Git Adding Files
Add this file into git repository by using command
$ git add *
$ git add *.html
you should be careful with this git add command as certain files like complied files which you don’t want to add but still exist in your repository will add if you use reclusively. Always be as specify with commands as possible.
Git Removing Files
Lets assume you have added a wrong file in to git tracking and you don’t want to remove it from the tracking list but still you want to keep that file into repository.in this case if you use command
$ git rm “file_name”
it will remove that file not from the git tracking list but also from the current repository. To only remove it form git tracking you need to run following command.
$ git rm –cached “file_name”
Git Committing changes
Committing file changes means keeping a snapshot of the changes and here you can also define a message of that particular commit so you can remember when and why you did this changes to that file, which you can provide via –m prefix. Lets commit our staged files by simply typing command
$ git commit –m “My commit message”
Make sure you provide a detail commit message that can not only help you to identify but it also help other developer or user who are working with that same repository and with that same file. This message will become more useful when your files having conflict. We will discuss more about conflict in other post.
At anytime when you want to know the status of your repository you can simply type
$ git status
By using this command displays the state of the working directory and the staging area. It lets you see which changes have been staged, which haven’t, and which files Git isn’t tracking. Status output does not show you any information regarding the committed project history. For this, you need to use
$ git log
Git remote command help to add files into remote or you can say live repository which we have already created in first step as I mentioned you can use any git cloud like GitHub, GitLab, or BitBucket in example we are using Github.
First add live repository url by command
$ git remote add origin https://github.com/joshihardik/mysite.git
$ git push –u origin master
This will push all your changes into remote repository.
At this stage we have only covered a basic step of the distributed subversion system Git There are lots of commands available at https://git-scm.com/docs/ an official git portal. If you have any questions and query related to git let us know in the comment below will try to help you out.