My encounter with GIT

I started with some basic programming in C++ and QT and was exposed to the world of FOSS. I searched the net about everything related to Open Source Programming. This is where I first encountered the concept of  “Revision Control”, the most famous being the GIT!

What is GIT?

Git is a revision control tool that aids a developer in programming more efficiently. Basically it stores the whole code in a remote location and every time you make a change in the code it stores it. And if you happen to mess it up, you can even revert back to the previous versions of the code. Different programmers can change a small portion of code and then patch in into the repository. This way simantaneously multiple people work on the same project.

How does GIT do it? 

The GIT uses a special file structure for this. All the different samples of codes which are pushed into git are stored in “Versions” and combining these versions we get a “repository”.

How to use it?

  • Windows: Download Git Bash and it teaches you what’s going on a bit better.
  • Linux or OS X: There are handy-dandy installers for integrating Git commands, such as “sudo apt-get install git” if I recall correctly.
I am using the Windows one which uses simple linux like functions as “mkdir” and etc.
So now, let’s assume you have some code that you want to start a revision control system on.
We’ll call it hello_world.cpp, in a folder called PROJECT_TEST.
On your remote hosting site (Bitbucket has unlimited free repositories, or folders-of-code, and Github has something similar for students IIRC), you can click the big MAKE NEW REPOSITORY button, and it will create a new empty remote repository.
You can run a command (in your respective Terminal, be it Terminal in Linux/OSX, or Git Bash in Windows) on your code folder, as prompted by the $ symbol:
 $ git init
to initialize that folder to be compatible with Git. You’ll have to do this once per project.
Next  link that folder to your remote repository by telling it where EXACTLY to push things back up to the cloud.
 $ git remote add origin http://some.github.repository/PROJECT_test.git
What this says is “this local codebase is linked to a remote codebase, which we’ll call ‘origin’, found on this git repository at this address.”
Follow it up with:
$ git add .
$ git commit -m "Initial Commit."
$ git push -u origin master
Let’s run through the commands you just used one by one real quick.
 $ git add .
Adds all NEW/CHANGED files (i.e. hello_world.cpp in this case) to a pending STAGING area.
 $ git commit -m "message" 
Commits that staging area, locking it down as some kind of feature or bugfix or whatever MESSAGE is. This is known as a “commit”.
 $ git push
Actually pushes this code up to your remote repo.
You can see what files have been last changed between commits with:
 $ git status
And pull down changes from other users/computers with
 $ git pull
Now let’s consider a different computer.
On a different computer (we’ll call it C2), if you run:
 $ git clone http://some.github.repository/SENIOR_DESIGN_PROJ.git
You’ll be prompted for your password, then your computer will go off and grab the most recent code and store it in a folder called PROJECT_test.
You make some changes to the source code. Fix a bug, whatever.
SO:
 $ git add .
 $ git commit -m "I fixed that stupid memory leak."
 $ git push
And back on C1:
 $ git pull
Congrats, you just pulled back down the bugfix that you wrote on a entirely different computer! This is how teams are able to work on a codebase simultaneously — they all commit and merge it all into branches. In our case, we, again, only care about the Master branch.
I messed UP!
Or, you make some changes, decide these were bad changes, and you want to roll back:
 $ git checkout <filename>
resets a single file by removing it from the Staging area.
 $ git reset HEAD --hard
resets your entire codebase to the previous commit.
 $ git checkout commit-number
resets your entire codebase to an arbitrary commit-number (IIRC). This is VERY handy if you mess up your code base, but an earlier version worked.
That’s basically the basics! With this you can store all your code remotely, navigate the remote location to find commit numbers, roll back code as need arises!
Advertisements

3 thoughts on “My encounter with GIT”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s