Source Code Versioning with Git in Visual Studio
Join the DZone community and get the full member experience.Join For Free
if there is something every developer ought to do often, it is source code versioning , it is not something new and not rocket science. be it a large scale software development or just in house development, versioning your code can be interesting and can save you a lot of headaches.
before i got immersed into source code versioning, what i was normally doing was to do a backup of the current project i was working on, at the end of each working day, while this is good its not really efficient. what plunged me into source code versioning was that, there was a day i had not backed up the project i was working on and i mistakenly deleted a code class, that i had worked on for more that four hours, i whined through out that day, because it was just four hours of development time wasted.
source code versioning can be achieved using a version control system(vcs). vcs is a system that keeps track of changes made to a file or set of files over time so that the files can be recalled later if need be. it is a very wise thing to use a vcs when building softwares because it allows files or even an entire project to be reverted back to a previous state, files can be compared to see what changes have been made over time, who last modified a file, when it was modified, and a lot more. using a vcs also generally means that if you screw things up or lose some files, you can easily recover them back.
there are various vcs available such as mercurial, bazaar, perforce, subversion, git and so on. git is a robust vcs, and i prefer it to other vcs because it makes use of local files and resources on your system for all its operations. everything in git is check-summed before it is stored and is then referred to by that checksum. this means that git will know and keep track of any change made to the file or folder it is monitoring, and files cannot get corrupted without git detecting.
now, lets get started by downloading and installing the windows version of git from http://code.google.com/p/msysgit . after the installation is completed, try and right click any folder in your system, you will see that two new menu entries have been added to the windows context menu - git gui here and git bash here , thus you have access to a cli and a gui for controlling git.
to work effectively with git, some basic git shell commands must be understood, and i will try to explain them below.
since the installation is completed successfully, the git on your system needs to be setup and configured. you need to set your username and email address and this should be done after git installation. so open your git bash, that can be done by right clicking any folder on your system and select git bash here and then type the command below.
$ git config --global user.name "ayobami adewole" $ git config --global user.email email@example.com
to start version controlling and tracking an existing project on your system with git, just locate the folder that contains the project solution, files and other items, right click the folder and select git bash here. then type the following command.
$ git init
$ git add *.cs $ git add info.txt
not all files and folder needs to be tracked, for example we might need to tell git not to track the bin sub folder in a visual studio project. to achieved this, the command below is entered in git bash in order to generate a .gitignore file.
$ touch .gitignore
*.suo /obj /bin
if you type the command
$ git status
to commit all the changes you have made you need to run the command
$ git commit -a -m 'i just added two new code classes'
there are times when you might mistakenly screw things up and need to revert files or even an entire project back to its earlier form. suppose i have an accountrepository.cs file that i want to unmodify, i can simply just type the command
$ git checkout -- accountrepository.cs
now, to some folks like me who are not really keen working with command line interface, there is an extension git source control provider which when installed into visual studio can make working with git interesting, because everything operation will be performed within the comfort of your visual studio, be it visual studio 2008, visual studio 2010 and even visual studio 11 beta. just download the extension from gitscc.codeplex.com , and install it into your version of visual studio.
i will be using visual studio 11 beta, but nothing to worry about, because its the same approach to all the supported versions of visual studio. create a new project, in this case i created a new windows form application and i named it gitexample.
when the project has succesfully loaded, right click the solution and select new repository, this will create a new git repository for the project and those files required by git, even a .gitignore file would be automatically created for you with some entries, which you can edit later. you should see a yellow plus symbol, which indicates that there are new files that have not yet been added to the repository.
in your visual studio, you should see a tabbed document with the title git pending changes-master, with this tab, you can commit changes, ammend last commit, stage files and even invoke the git bash directly.
in the git tab you will see files that have been changed, new files that the need to be staged and commited. to commit changes, in the git tab, under changed files, right click those files and select stage to stage the files, then type your comments in the comment textbox and then click commit to commit the changes
after a successful commit, the icons in front of all the files that git is tracking will change to a blue padlock, the moment you make changes to any of the file, the icon will change to an orange pass mark icon indicating that the file has been modified.
i hope you find this article helpful.
Git code style
Opinions expressed by DZone contributors are their own.