Category Archives: Basic

Crash Course – Tutorial – Learn – GIT

I have read many Articles/Manuals of git on Internet and think all the articles are messy. No body tried to explain, how to use it just have given the commands and their article is complete. So, today I am here describing my experience about Git and provide the real using of Git and how to do the things.

What is Git?

Most of the persons know what is Git but for those who doesn’t know let me explain them. In software development, Git is a distributed version/revision control and source code management (SCM) system with an emphasis on speed.

What do you mean by revision/Source Code Management system

The software development is very laborious work and to maintain the backup/Management of Source Code is a very critical thing. Let me go through an Example to understand version more easily.
Example:
A client comes to you and told you write certain code for certain project and do have done it easily, as there is no problem upto this stage. You write the Code and optimized it and given to the client.
Now, after sometime the same client comes to you and told you to do certain improvement in the same package and you have done that very easily and have no problem in it.
Now, after the lapse of some large time the same client comes to you and tell you earlier project has that feature and want to implement it. Now, here is the problem, either you have to work again or if you have taken the backup then it is easy. Here we are interested in not taken backup situation. Now, if you have done the project through Version Control system the version system would take the necessary backup at different points and you can view it easily.

Advantage of using Source Code Management system

There are many advantages of using SCM but the important advantages of using SCM are:
  1. Backups: If you accidentally delete some file (or part of a file) you can undelete it. If you change something and want to undo it, the SCM can do so.
  2. Portability:SCM are designed to help to solve the problem of Portability to your code between different computers. You do not need to bother if you always copied the newest version; the SCM will do that for you.
  3. Branching/Debuggin:Say you have developed the project as there are many things involved in it. As such Designing, Security, DBMS etc. things and now if you want to fix come thing in particular branch then you open the branch and debugging or up-gradation would start immediately rather than to see the full code again where the particular branch starts and where it ends.
  4. Simultaneously Working:Say you have developing the project as there are many things involved in it. As such you create the branches of the project and distribute them to different developers and those developers can work simultaneously without interfering with another developer work and each would be having its own new code and master branch with all working simultaneously.
  5. Cross Platform Working:This the piratical advantage that if one developer is familiar with working with one type of system and another in another type the SCM system make the both codes merged and cross-platform development would be easy development as both developers can know that things are required by other. The best example is open source projects such as Apache, MySQL, PHP and many more, which run on the cross-platform.

Does Git is the only SCM and what about cross platform running

No, Git is not only SCM available, there are many others available and that to run on cross-platform but there are certain advantages which makes Git a little superior to others. Other popular SCM are CVS, Sub-Version, Mercurial, Bazaar, Perforce and Team Foundation Server. There are many available but the TOP three are Git, CVS, Subversion. To know why Git is better than others we would have to look on the others SCM and know what are the advantages we are getting.
  1. Git allows your to work offline, with others it is not possible. Even a simple log command cribs for the server.
  2. With the philosophy of Git branching, checkout, tagging is a flash. SVN is good with branching and tagging but with checkout it is slow(goes to the server for each file).
  3. Git being content addressable, can be checked for inconsistencies(CVS had corruption).
  4. There are great workflows which suite them completely and have been worked out great with Git, here.
  5. With facilities like interactive rebasing and interactive add, stashing etc, you do not need to make bulk commits or create un-necessary branches for each bug/feature.
  6. Git is blazing fast with cloning too — it packers files and hence the transfer achieves connection speeds. With SVN and CVS there is no such tarring which makes it inherently slow over the wire.
  7. Non-significant but git metadata is almost half that of CVS and SVN and there have been better reported results with Mozilla’s transition of SVN->GIT.

Important terminology – GIT

One must understand, the terms used by git manual or used in other blogs and website and without understand them, we can’t learn using Git. The important terminology used by Git are:
  1. Repository: The purpose of Git is to manage a project, or a set of files, as they change over time. Git stores this information in a data structure called a repository. The Git repository is stored in the same directory as the project itself, in a subdirectory called .git. A git repository contains, among other things, the following:
    1. A set of files and directories
    2. A set of commit objects (i.e. the finalized code or different stages of backup of code or upgrading of code)
    3. Historical record of changes in the repository. A set of references to commit objects, called heads
  2. Branches: A branch is a separate code line with its own history. You can create a new branch from an existing one and change the code independently from other branches. One of the branches is the default (normally named master). The user selects a branch and works in this selected branch, which is called the “working copy”. Selecting a branch is called “checkout a branch”.
    Example: Say you are working on a paper. You’ve gotten a first draft out, submitted for review. You then get a new batch of data, and you’re in the process of integrating it into the paper. Halfway in, however, the review committee calls you up and tells you that you need to change some of your section headings to conform to format specifications. What do you do?
    Obviously you don’t want to send them your half-baked revisions with corrected headings. What you want to do is jump back to the version you sent out, change the headings on that version, and send off that copy, all the while keeping your recent work safely stored somewhere else.

    This is the idea behind branching, and Git makes it easy to do.

  3. Tagging: Git has the ability to ‘tag’ specific points in history as being important/milestone achieved – generally people use this to mark release points (‘version 1.0’, etc). There are two types of tags available in Git are:
    1. Lightweight: Lightweight tags are very much like branches that don’t change – it’s just a pointer to a specific commit.
    2. Annotated: Annotated tags, however, are stored as full objects in the Git database. They are checksum, contain the taggers name, email and date, have a tagging message and can be GPG signed and verified.
    It’s generally recommended to create annotated tags so you can have all this information, but if you want a temporary tag or for some reason don’t want to keep that other information, lightweight tags are available too.
  4. Commit: Committing is a relatively straight forward process that adds your changes to the history of your repository and assigns a commit this name to it. The change is not sent to a central repository, though. Other people can pull the change from you, or you can push the change to some other repository, but there’s no automatic updating. You can use commit in multiple ways to commit changes to your repository, but every commit requires a log message. You can add a message by adding -m “your message”. The message can be any valid string. You can also specify multiple paragraphs by passing multiple -m options to git commit.
  5. Revision: Represents a version of the source code. Git identifies revisions with SHA1 ids. SHA1 ids are 160 bits long and are represented in hexadecimal. The latest version can be addressed via “HEAD”, the version before that via “HEAD~1” and so on.
  6. HEAD: is used by your repository to define what is currently checked out:
    1. If you checkout a branch, HEAD symbolically refers to that branch, indicating that the branch name should be updated after the next commit operation.
    2. If you checkout a specific commit, HEAD refers to that commit only. This is referred to as a detached HEAD, and occurs, for example, if you check out a tag name.
Those who doesn’t like GUI or want to learn Command line operation the list of important commands is very necessary to learn are:

Important GIT Command

GIT Command Name Command Description
Add Add is used for adding the contents of the file to index.
bisect If any bug is created after update then the command is used to find the changes that introduced a bug in project.
branch branch command is used to create, view, list or delete the branches
checkout Checkout is used for switching between branches
clone clone means duplicate, in git it is used to duplicate the project at different location
commit commit is update the changes to index, note it doesn’t update the remote repository
diff To view the changes between two commits
fetch If you want to use the objects from different project then fetch is used to fetch the particular tag or branch
init create a empty git repository or re-initialize the an existing one
log It is used to show the message sent with commits
merge it is used to join two or more histories
pull pull is the important command used to fetch in case of multiple developers doing work on same project. It fetch from and merge with another repository or local branch
push after commit the project push is required to update the remote project.
rebase In case you deleted the file and wanted it again rebase is used to Forward-port local commits to updated upstream head
reset Moving the head to specified commit or state.
show used to view the different types of objects used in a project
status An important command to view the current status of branch either the code is edited by some body locally or not and if yes then in which files.
tag Tag is used to create, delete, list or verify the tag with GPG

Perquisites to understand GIT

The only thing required for Git is the software git installation, as users from Linux are much know about GIT and I don’t talk about Linux system. My target is for Windows and on Windows Git Can be installed by two ways: i) Either Install Cygwin/Migwin on the system first and then install/download the program through cygwin, ii) installing MSysGit.
I have not used Mac/Embedded systems or other systems and thus my personal interest in Windows and using the Open Source Tools and using GCC for development with cross-platform.
I personally like the second way, as installing the cygwin will be time-consuming and to understand cygwin one has to learn the command line commands and in second option download the simple package and install it and start working with GUI or command line as choice is yours. MSysGit can be downloaded here
I am going to talk about the Remote Git and using Git Locally and update the code Remote and also side by development and distribution with many developers maintaining the code. So, search about free git providers and signup and install GIT locally and generate the SSH key and upload the Key at website, so that you can easily update the code. At this point I am presuming that you have installed the Git and signed up a free/paid Git Provider and also setup the SSH key to update the key generated through your system.
Note: For downloading the SSH Key is not required the SSH Key is only required to update the source code to confirm the developer only update the source code and not by any person.

Creating the First Project on Remote Server

On Git registered website create a new Repositories with any name you like but remember whatever name you give on downloading the code through Git the project name is changed to project folder Name. Since we are using the example and best would be Hello_World.
There are two ways download the Repositories to you system i.e. i) through GUI and ii) through Bash (i.e. command line). I will let you know both the things.
GUI: Click on Start => All program’s => Git => Git GUI. Select the clone option provided there and provide the url of your project created as source and destination as any local system location of your choice.
BASH: Select the folder or drive to use as destination and right lick mouse button and choose the option of git bash from list and in command line write git clone url (url means the project remote address) and press enter and it would ask your password and after that the folder with the name of your Repositories would be created and we have to work under this folder.

First Commit/First update of Project

Now create your project under the folder created by git, here I am creating hello world example as project but you can make any project of any language. The code of hello project is as under:
#include 				/* Including the standard i/o file */
int main(){					/* Starting the main program */
	printf("hello world!!");		/* If all the right print hello world */
	return 0;				/* returning 0 to end the program */
}
At any time during coding you think a stage is reached or the time has come to take the backup then do the following steps to take a backup. Go the folder of the project created by GIT and under that folder right-click on empty screen as both GUI and Bash required this step.
GUI: Select GIT GUI. It will open a new Window which will let you the changes made in project. Under un-stacked the list of files would be displayed on which the changes are made and on clicking any listed file would show the changes made by you and if the file is new then whole new file contents. Stack the changes you want to made by selecting files and press ^T and the files would move from un-staked to stacked changes provide the comment on the small text window, you can type any thing you like to give a note for the project or any other think and press commit button. Now, the changes are staked but the stacked are not sent to remote server and you can also edit the changes upto this step and the last step is to click on push and a new window would pop and again press push and after that your project is updated on the remote server too.
Bash: Select Git Bash for command line update tool, First of all check the “git status”, as it will let you know what files are changed. After you confirm the files are correctly show then issue a command of “git add file-name”. This would all the files as stack to commit, you can also supply multiple file names in “git add file-name file-name …..”. When you have added all the files and no changes are required in files then right “git commit -m ‘your message'”, git commit will make the staked changes to final stage and those changes the original files with recent committed files or stake the changes to the original files, -m is used to write a message as comment as you can provide the description about commit and the changes made by you on the project. It is one of the important thing to write and let you know what is done at the time of that commit. Now the last step to push the changes to the remote git by “git push”, this command make your local committed copy and remote copy sync, and when again somebody or you download the project the new updated copy would be provided.

Note

Git can work with the same workflow as Subversion, with a group of developers using a single repository for exchange of their work. The only change is that their changes aren’t submitted automatically but they have to push (however, you can set up a post-commit hook that will push for you every time you commit; that loses the flexibility to fix up a screwed commit, though). The developers must have either an entry in htaccess (for HTTP DAV) or a UNIX account (for SSH). You can restrict their shell account only to Git pushing/fetching by using the git-shell login shell. In the next article, I would tell you about more advanced features for development under many developers, merging the changes and to track bugs and many more things.
Tagged , , , , , , , , , , ,

int main Vs void main Vs int main (void) Vs main

Today, while reading source of some application on web, I come across the usage of two – three types of declaration for starting the programming code. Some use void main and some use int main and in some code’s, I have also seen int main(void). The question which come in my are:

Which one is vaild?

The answer will surprise every programmer but the true statement is all are valid but in different programming environments. I will just explain it in details here that why all are valid. But first why this question pop up. From quite months I am writing articles about C on my web blogs. I learn C language in 1997 before C99 standardization. Then moved to other languages to learn gone through C++, MySQL, VB, PHP,  JavaScript, JQuery, HTML but due to some technical problem, I have to upgrade my C knowledge and thus while learning, started writing Language C concepts and my views on my Blog. After learning many chapter, I started to see some of  source codes and manuals of Open Source C Codes, Windows C and Borland C and come to know the different types of main functions and after that search web about my questions. I have read many Articles, Books and Recommendations provided by Authors and also taken the opinion about which one is valid and every one has different views from other. Now, let go through One by One main function types and see why they are valid.

main()

For understanding why main() is valid we have to see the History of C. The initial development of C occurred at AT&T Bell Labs between 1969 and 1973; according to Dennis Ritchie, the most creative period occurred in 1972. The origin of C is closely tied to the development of the Unix operating system, originally implemented in assembly language on a PDP-7 by Dennis Ritchie and Ken Thompson, incorporating several ideas from colleagues. Eventually they decided to port the operating system to a PDP-11. By 1973, with the addition of struct types, the C language had become powerful enough that most of the Unix kernel was rewritten in C. This was one of the first operating system kernels implemented in a language other than assembly. (Earlier instances include the Multics system (written in PL/I), and MCP (Master Control Program) for the Burroughs B5000 written in ALGOL in 1961.).
In 1978, Brian Kernighan and Dennis Ritchie published the first edition of The C Programming Language.] This book, known to C programmers as “K&R”, served for many years as an informal specification of the language. The version of C that it describes is commonly referred to as K&R C.
In 1983, the American National Standards Institute (ANSI) formed a committee, X3J11, to establish a standard specification of C. In 1989, the standard was ratified as ANSI X3.159-1989 “Programming Language C”. This version of the language is often referred to as ANSI C, Standard C, or sometimes C89.
In 1990, the ANSI C standard (with formatting changes) was adopted by the International Organization for Standardization (ISO) as ISO/IEC 9899:1990, which is sometimes called C90. Therefore, the terms “C89” and “C90” refer to the same programming language.
Even after the publication of the C90 and C99 standard, for many years K&R C was still considered the “lowest common denominator” to which C programmers restricted themselves when maximum portability was desired, since many older compilers were still in use, and because carefully written K&R C code can be legal Standard C as well.
Book contains many code’s, which also become the source of medium for many to learn. In book all the examples use main() as the starting point of the program. Standard Committee has decided that there compiler’s must support the basis described by the book but also started to deprecate the main() as starting point and started using int main() as the starting point. But due to support of all old code’s, the main() remains the valid. Since, then many compiler’s are created but they only issue a warning for using main(). The code will be compiled as without int main or void main. Thus, development of C and Book of K&R, are the base of main() and it is valid till present year November, 2011.
After reaching the point that main() is valid, I tired to compile the hello world program on Turbo C, Turbo C++ and GCC, all the produced the same result but GCC issued a warning of Standard C++ but compile’s the source code. After that I tried to run the code many code’s and to my surprise all the code’s are valid as only warning is issued by GCC only.

int main()

int main() is defined by C Standard Committee, reason given by many programmers and others. The reason is connected with the popularity of Language C. After the Development of C, the interest in programming Language’s has grown very high. Many people want to learn C rapidly and wanted to start writing code’s. The problem which pop’s out is that C Language is very near to the Assembly Language and thus people write different types of code’s and the error can only be shown when the output is displayed and another is it is impossible for a tutor who is teaching 100 students makes it difficult to check 100 students code instantly.
Now, the solution is needed to over come this situation and thus it is implied that the main () program should start with int main() and on termination would set the integer value to see the successful termination of the program. Thus, return 0; is introduced. Which means that the execution is successful and complete and thus terminate the program. Now, process is little improved with the operating system can store the returned value so that calling processes can query to find out whether an executable was successful or not. Applications usually return 0 for success and a range of non-zero error codes if unsuccessful.
So, we can say that the program can return a value to the operating system to indicate whether its execution was successful or not. Numerous shells, like bash, take advantage of the value returned by programs in examples like the following:
prog1 && prog2
The above statement will execute prog1, then it will only continue to execute prog2 if prog1 completed successfully (by returning a value of 0, or some other number which is interpreted as a successful exit).
int main() has also increased the learning process of C as the learner can now look self whether the program written is the successful or not. I am of the view that the introduction of int main() is also one of the major factor for popularity of Language C and one and learn self with little guidance and in modern time’s without teacher also.

int main(void)

The standard committee has also introduced that int main(int argc, char *agrv[]), which means that series of functions pass kind of additional information to the invoked program. However, main is still defined to take two parameters for these functions; the environment information is assigned to an external object extern char **environ. If someone doesn’t need or want to ignore the additional passing of arguments to the program then one can use int main(void). As this will ignore the additional parameters if provided.

void main()

Void means 0 in C. Thus, Some programmers interpret that void main means that return type value is always 0 but they are wrong. void main() only using in Microsoft Windows base compiler’s provided by Microsoft such as Visual C++ or Visual C#. There is a very good article available why void main() means the Wrong thing.

Which one to use?

Use int main(int argc, char *agrv[]), if you want to implement that required additional parameters required and else int main(void)
Tagged , , ,
%d bloggers like this: