Friday, December 26, 2014

How to keep track of your source code - Source control Mangaement System

This is how I kept track of my project back in college :(


Yeah.
As clear from the pic I was working on a Computer Networking project and the tool I was using for this purpose was "Glomosim". And that is how I kept daily track of the work done.
I always used to wonder if there was a way to manage these version but never googled it, my bad :(

So today before we get into any kind of technical jargon like scripting/compile languages, python, Chrome extensions etc, it is very important to know that how can we keep version track of the code we are writing. Developers around the world have already thought of the solution to this problem and these particular tools are called Source Control Management or simply SCM Tools.
There  are a lot of such tool but most commonly used are SVN (subversion), GIT and perforce. GIT is an open source and widely used in the industry today.
Important thing to note is that these SCM tools can not only be used to keep track of only source code. You may even maintain version of your grocery list :)

I this post I will discuss/introduce Git. So, the most important thing to remember whenever you work with GIT is that GIT doesn't know state of one standalone file. Say you commit files A,B,C in a project and then later you change file B and C to B',C' respectively, later you realise that you made a mistake and project should have been of the type A,B,C' so you just can not right click on the B' file and say replace it with previous version. Because going back to previous version will mean going back to stage of A,B,C.
Another important thing is to note the difference between commit and push. Commit just means committing to your local environment it does not mean that your friends or colleague will be able to see the changes. "Push" can upload the changes on the remote repository.
In my previous job I used to work on SVN in which committing meant uploading the changes in repository unlike GIT. So it may be confusing to newbies so just a heads up.

OVERVIEW OF REPOSITORY, TRUNK, BRANCH and TAGS
Repository : Other name of project-name. So say you are working in a project named X and you will have a repository for project X either in SVN, GIT or perforce.

Trunk : Main line of code. In other words trunk was the first copy of code and the rest code is incrementally added to the trunk. Other name for the trunk is develop or dev branch.

Branches : Since there a number of developers working on a project. So each developer takes her own copy of the latest code as kept in trunk/dev branch, works in it and then merge back.
We will cover TAGS later but let us first discuss the term merge.
Consider the merge scenario shown below

Assumption: The project consists of 100 files and both the new features will be added in file 50. That is both dev engineers are working on File 50.
Here two coders are given the responsibility to implement two feature say F1 and F2 to developer 1 and developer 2. Both make two branches from TRUNK/DEV Branch say B1 and B2 respectively. Now Developer 2 completes her work and tries to push the changes in TRUNK/DEV Branch, since the trunk has not changed therefore the merge happens perfectly fine. Dev 2 pushes the merged branch of TRUNK and B1 on TRUNK itself so that others can now see the change. But say Dev 1 tries to push her code without taking the latest checkout of TRUNK so Dev 1 gets error. Because the TRUNK's state has changed. and hence Dev  1 gets merge errors for file 50. so Dev 1 manually removes the merge errors and then pushes the changes back in TRUNK.

TAGS : After every successful release a TAG i.e. copy of the code which was released is created with  that release version.

WORKING WITH GIT
Now I will cover just basic points or commands which you will need in order to work with GIT.
Just go to git-scm link for windows or Getting-Started-Installing-Git for other operating systems This will install Git-Bash on your computer. If you want to create your repo use GitHub. Public repo are free of cost. When you initialise the repo this will create "readme.md"or use git init command.
Next you will have to create SSH key so that git repo can recognise you. If you don't want to do that use set "git config http.sslVerify "false" "
To commit changes on your local system use git commit -m "My first commit [Your message]"
Since on github you might create numerous project each with a unique link so you have to set origin for that particular git project, using git remote add origin "http://[link of git repo.git]" Origin is another name of remote server/link where your project in present.
Do not confuse branch and an origin.
To take the latest of what is lying of TRUNK use git pull or git fetch.
To checkout your repository on other system use git checkout [branch name].

If you have done something on the local copy of the checkout code but you want to revert it to what you originally checkout out use git reset --hard.

Resources :-
1.) Git Documentation - Really good
2.) Webinar on Git- youtube video



No comments:

Post a Comment