Skip to main content

Introduction to Git commands and Version control system

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.

introduction-to-git-6-728

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.

Git commands:

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.

Git init

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

Screen Shot 2016-02-14 at 2.23.50 PM

Git Config

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 “hardik@mail.com

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 *

or

$ 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.

Screen Shot 2016-02-14 at 2.30.46 PM

Git Status/log

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

Screen Shot 2016-02-14 at 2.31.32 PM

Git Remote/push

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.

Screen Shot 2016-02-14 at 2.31.57 PM

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.

Screen Shot 2016-02-14 at 2.35.34 PM

Conclusion

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.

Enjoy Git.

Hardik Joshi

Hardik Joshi is Founder, Developer and Writer at eCodezone.com, He loves to write usable code for the Web and other digital sources.

  • Nancy

    Great tutorial for git can you please explain how can i download already created repository from the git hub ?

    Please explain little more im new to git
    Thanks a ton.. nice work..

    • If you want to download the already created repo from git hub you should use git clone command to make a repo copy, the syntex is
      $ git clone [git url]

      so as in our example you can use
      $ git clone https://github.com/joshihardik/mysite.git

      This will copy whole remote repository into your local machine, before this command make sure you step into specific path you can use command $ cd /your/folder/ to do so.

      Let us know if you still have any queries. Will post whole git command list with specification in next post.
      Thanks..

  • nice into Hardik

  • Pingback: Understanding Git Branches - eCodezone()

  • This is interesting since I was using Bazaar VCS and I had heard of Git. They share the same distributed model and have similar commands. It should be easy if I want to use Git.

    • Yes you right @ninzo:disqus Git is basically a VCS and most of the commands are same, so it would be easy to understand Git commands if you know any VCS.