Getting started with Git

A version control system keeps track of changes in code. It keeps track of every change made by every developer. It allows code bases to be branched and then merged back. You can also go back in time to a point before you introduced something in the code. Version control is absolutely essential for software development.

Git is a version control system developed by Linus Torvalds, the creator of Linux kernel. It is a distributed version control system as opposed to a central version control system. Subversion and CVS store the code in a central repository which is accessed by all developers. In Git, every developer copy of repository contains full history. There is still a canonical repository but individual developer don't need to access server resources for their work as all their changes managed by their local repository. For example:

Linus clones central repository
Linus checks out a branch in the repository
Linus makes code changes and commits to local respository about 20+ times per day
When Linus is satisfied that his code is good enough, he pushes code to central repository

The code for this would look something like:

git clone https://github.com/nazimrahman/xmlsplitter.git
git checkout dev
git add -A; git commit -m "did this for this reason"
git push origin dev

We will cover each of this in sufficent detail.

Installing Git

Git is free and opensource.

To install git on Windows, I would recommend that you simply download and install git bash

To install git on Mac, download and install from git-scm.com

To install git on Ubuntu Linux, simply type:

sudo apt-get install git

To test whether git install correctly on any operating system, simply go to the terminal and type

which git

Cloning the central repository

Cloning a repository means downloading a local copy. To clone, simply type the following with the relevant git url:

git clone https://github.com/nazimrahman/xmlsplitter.git

This creates a local repository on your system.

Checking out a branch

When working with your repository, you need to check out a branch as follows:

git checkout dev

where your branch is dev.

Add/Removing your changes

To tell git about a new file you added or a file you updated, simply do the following:

git add README.md
git add somedirectory
git add -A

The first command is to add a file. The second to add a directory. The third is to add everything. The following command removes a file from git.

git remove README.md

Commiting a change

The following command allows you to commit your changes.

git commit -m "I changed this for this reason"

The text following -m is the commit comment. It is very important to take a moment and write a meaningful commit comment. This is what your future self will read when you will have to revert changes or isolate the change that caused something to break.

It is good habit to check status before committing.

git status

This command basically tells you which files will be committed. This is a good time to verify that only the files you intended to modify are being committed.

Pushing your code to central repository

When you are satisified that your code is good enough to be shared with your colleagues, push the code to the central repository as follows:

git push origin dev

Conclusion

There is much more to GIT. This is a quick startup for beginners. Most organizations have a workflow that all developers are expected to follow. Git is a bit different from Subversion and CVS. If you are merging, stashing, or rebasing, read and understand the documentation before you proceed.

Actions: