Sunday, April 17, 2011

[General-Dev]–Introduction to Git on Windows via GitExtensions

For those of you that haven’t heard of it, Git is a version control system created by Linus Torvalds (creator of Linux). Since it’s inception, it has gained a huge following, primarily in the open source community, especially for developers on UNIX based operating systems such as Linux, and OSX. However recently it has been gaining a foothold with Windows based developers as well, due to it’s advantages over other source control software. By origin, Git was designed to be a command line tool, which is efficient and great for automation, but can be a barrier for newcomers. However there are some projects that are providing a graphical interface to this, which I’ll talk about in this post.

If you are new to version control, basically it involves you ‘checking in’ your  code at various points of development. This keeps a version history of your project, so if you delete some code, and then later on decide you want it back, you can easily rollback changes, or check out an old version. Additionally, you can setup a version control server, which you check your code into, which effectively then serves as a backup for your code in case your main development computer dies. Subversion is a great example of a popular version control system with great support in Windows. TortoiseSVN, AnkhSVN and Visual SVN Server are a great, free way to get setup with Subversion on Windows and Visual Studio if you decide that Git isn’t for you.

If you are already familiar with Subversion, you should be aware that Git works a little differently. First of all, Git keeps a full copy of the repository on your local computer, so you can continue checking in frequently even when you cannot access the remote server. The workflow in Git involves you committing regularly to your local Git repository, and then when you are ready to share your changes with the rest of your team you ‘push’ them to a remote server. This allows Git users to commit far more frequently than Subversion users as they don’t have to worry about sending half finished changes to the rest of the team. Additionally, you can have multiple ‘remotes’ that you push your code to, allowing a ‘distributed’ architecture. Git also does things differently to Subversion when it comes to branching. While branching was nothing short of a nightmare in Subversion, Git has a great branching system that allows you to merge changes together with less conflicts for you to manually sort out. 

For more information on using Git, check out the Git website’s documentation section. I personally read the book ‘Pro Git’ on my Kindle when learning the system, and found it probably goes into more depth than you need in day to day use, but if you want to get a good understanding of how it all works, it’s a good place to start.

So, how do I get started using this on Windows?
There are two ways to get started. First of all, if you are a command line fiend, you can simply head over to the Git Website and download the Windows installer, this will give you access to the ‘git bash’ that lets you use all the git commands to manage your code.

Alternatively, if you prefer the option of a graphical user interface (as well as a fallback to the command line) you should check out Git Extensions. Git extensions is a great project that provides Windows Explorer and Visual Studio integration for Git. It’s not perfect, but it’s probably the best GUI for Git on Windows at the moment (there is a TortoiseGIT project as well if that’s your preference).

Download the installer, and run through it. I choose to install MsysGit and KDiff, but otherwise just leave everything as the defaults.
when the Git setup wizard comes up, it’s a good idea to tick the option for ‘Git bash here’ windows explorer integration.
Once this is done, you’ll notice you can then right click on folders and see the various Git Extensions shortcuts
or alternatively you can click the ‘Git Bash Here’ option and see the command line.
But first, you’ll have to head into the ‘Settings’ section of Git Extensions and setup a username and email address

Then you are all configured, if you open visual studio and create or open a project, you should see a ‘Git’ dropdown menu at the top of the screen. From here you can initialize a new repository
After this you are ready to do your first commit, you can do that from the ‘Commit’ option. You’ll then see the commit screen:
You may notice a large amount of junk/temp files that you don’t want to commit into your version control, you can remove these by creating a .gitignore file in your project directory. GitExtensions has a preconfigured file of these you can use. Go to working dir changes, edit ignored files and click ‘add default ignores’
You should then see your files list shrink somewhat. You can now ‘stage’ your files (which is a git terminology, it lets you choose which files you are going to commit at any point, though many people simply stage and commit all at once) , enter a commit message and then commit them.
Then you will have just made your first revision! These revisions are internally stored in the .git folder in your projects base, so you could copy and paste your project folder around, store it in Dropbox, zip it up and email it – all while keeping this all in tact.
As you can see, Git Extensions is quite useful for the general Git workflow, and is a good way to get introduced to the Git world. I personally prefer the command line most of the time, but having Git in my Visual Studio is very handy. For more information on how to use Git Extensions, check out this manual.
Finally, now that you know how to get Git running as a local repository, I recommend you take a look at arguably the best feature of Git – is an online repository hosting service that has gained huge popularity, and today drives people to take a look at Git, even if it is just to make use of their services. If you are happy for your project to be open source, Github gives you free hosting, if not, you can pay a small fee a month to get some repositories. Their servers are rock solid, and their web interface is wonderful. You should definately check it out.
Much of the code that I create for this blog is available on my Github profile.
I hope that this article has given you enough of an introduction to Git to encourage you to do some more reading on it. If you aren’t using any version control for all of your projects, no matter how small, please, start doing so – you’ll thank yourself later!

No comments: