Web dev

The benefits of using Git

Introduction

(Previously published on PHPClasses.org)

As you may be aware, Git is a version control software application created by Linus Torvalds (the creator of Linux) with the collaboration of many other developers.

A version control application keeps track of all the changes that you do in the files of your project. Every time you make changes to files of a project, you can push those changes to a repository. Other developers can pull your changes from the repository and continue to work with the improvements that you added to the project files.

There are many version control applications. This article focuses on the benefits of using Git.

Git features

Working offline

Some version control applications popular in the past like CVS and SubVersion used the concept of central repository accessible somewhere in the network where all the changes needed to be commited.

Git uses the concept of local repository where you have a copy of the “complete repository” of your project. You can commit the changes you make to the files of your project to the local repository. So Git allows you to work completely offline, i.e. even when you do not have access to a remote repository.

Later you can synchronize or sharing the changes you made when you have online access to the remote repository.

Fast to Work With

Most of the Git operations are fast, mainly because they are performed on your local repository copy.

Repositories Are Smaller

A typical Git repository is smaller than for instance one using SubVersion. If you want to compare, you can try this test page. You can read more about this learning how Git packs information.

Moving or Adding files

If you want to move a file inside your repository Git automatically track the moves. This was not possible in old version control applications like CVS. Moving a file would typically require to create a new file and remove the old one, thus losing the changes history.

Also if you want to add only certain files with some extension with Git you can use wildcards. For example to add only .php files you can run:

Ignore Certain Files

Sometimes you have files being generated by your project, like for instance log files or files generated by your IDE, that you do not want to store in project repository because they are not really part of your source code.

You can tell Git to ignore certain files in the local repository directories using a file named .gitignore. The files listed in .gitignore are excluded from the version control process as if they are not there. You can share those rules committing the file or just keep it locally.

Branches

Sometimes you need to work on new experimental features without interfering with the main code of your project.

You can achieve this by creating new branches to try the code of those experimental features.

Branches also allow different developers to work on different features without interfering with each other work. Then when the features are ready, they can merge the branch changes in the main branch.

Check the Status of Your Changes

Check the status of the changes you made to your repository is pretty straightforward. The git status command lets you see what would happen if you committed your changes at a given moment. It can help to avoid the mistakes of using different externals branches of your project.

Stash Branches

If you are working on a branch of your project but you do not want to commit the changes, you can save the current status of that branch to return to it in the future. You can switch your work to another branch and insert the stashed modification in it.

With git stash command, you can save the current status. With git stash pop command you will apply the stashed modifications.

Cherry Pick Changes from Branches

Git allows to pick one commit from some branch and apply it into the current branch. This operation is helpful for testing purposes. Imagine you want to test some temporary modification or pick some commits done in other branches.

Find version that Introduced a bug using Binary Search

If you have an issue in your code and you want to know when it was introduced and what it is, with the git bisect command you could go back to every commit till you will find the bad one one which the issue was introduced. You can learn more about this on the documentation about Git tools for debugging.


Collaboration Between Developers

Previously branches were described as a means to work on new experimental features without interfering with the main code of your project.

Lets see how branches and other features of Git can be used in practice to promote better collaboration between developers working on the same project.

What is Git Flow?

Git Flow is a model for creating branches with Git, created by Vincent Driessen. It became popular among the community of developers that work with Git because it is very convenient for collaboration within development teams and works well when the teams grow.

When a developer starts working on a new feature or bug fix, he creates a new branch. When he is done with the new feature or the fix, he merges the new branch back to the main branch.

image: Vincent Driessen blog

GitFlow supports feature branches and hot fixes. The feature branch will be merged back to the develop branch (some kind of staging area) ready to be release. The hot fixes are done on tagged release that will only contain emergency fixes, so they are treated in isolation.

image: Vincent Driessen blog

Git Flow makes parallel development easy by isolating new development from finished work.

For developers, changing between the branches they are working on, or starting new feature branches in the middle of something is fast and easy to do with Git. Git keeps track of the modifications without creating a new repository from scratch.

It is easy to see and follow what each collaborator is doing, inspecting the commits and jumping on branches.

You may learn more about Git Flow on Vincent Driessen blog. He also provides tools to make it easier to implement Git Flows in your projects.

Who to Blame for bugs?

If you have found a bug in your code and you want to know when was introduced and why, you can use git blame command. This is a command that shows when each line of a given file was last edited and by whom.


Web platforms using Git

Git has been supported by many project hosting sites. Here follow some considerations on the two most popular Web based project hosting service based on Git.

GitHub

Github is the largest project host on the planet with over 25.2 million repositories. Some of the main features are the integrated issue tracking, the collaborative code reviews, and easy team management.

Bitbucket

Bitbucket is sometimes the company choice because of the strong team management tool it provides.

Bitbucket manages the GitFlow in an easy way, and it is totally integrated with Jira, their issue tracking application.

Bitbucket also provides additional features like:

  • Online tools for comparing branches
  • Management of pull requests, with comment and easy inspection about changes (useful often in a review process)
  • It is easy inspection of commits and see the flow of the structure
  • Allow comments
  • Create and track issues
  • Integrations and support for other 3rd party tools like IDE, Web platform, services, and others

Migrating from SubVersion to Git

If you have been using SubVersion version control application and you would like to switch to Git, migrating from a SubVersion repository to Git is quite easy using several tools that are available on the Web like svn2git and subgit.

Every tool mainly follows two steps to perform the conversion:

1. Checkout the svn repository

2. Conversion to Git including the commit history

Some tools provide the possibility to synchronize back to SubVersion after having done modifications to the project using Git.


Conclusion

This article covered some main Git concepts but you can find more information in the offical documentation.

Leave a Comment