DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
The Latest "Software Integration: The Intersection of APIs, Microservices, and Cloud-Based Systems" Trend Report
Get the report
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Source Code Versioning with Git in Visual Studio

Source Code Versioning with Git in Visual Studio

Ayobami Adewole user avatar by
Ayobami Adewole
·
Mar. 24, 12 · Interview
Like (0)
Save
Tweet
Share
4.89K Views

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 myemail@myemail.com
    
you should substitute your name and your email address appropriately.

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
    
this command will create a new folder named .git and all necessarty files that git needs to track your project effectively in the project folder. but at this point no file is being tracked yet, you have to explicitly tell git to start tracking your files, that can be achieved with
 $ git add *.cs
 $ git add info.txt
    
these commands tell git to start tracking all our c# files in the folder and a text file named info.

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
    
after this file has been created, open this file with notepad and type in all the sub folders and filesĀ  that you do not want git to track and then save it, for example
    *.suo
    /obj
    /bin
    

if you type the command
    $ git status
     
you can see all the files that have been modified lately and have not yet been committed and probably new files that you just created and are not yet being tracked by git.

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'
     
this command will commit the changes, -a option is to skip the git staging area and -m option is the commit message.

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
     
this comand will revert back the content of the accountrepository.cs file to what it was before the last commit was made. the beauty of git is that any change that have been committed can be recovered back.

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.

solution explorer with a plus yellow symbol

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.

git tab


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

git tab

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.

solution explorer

i hope you find this article helpful.
Git code style

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Keep Your Application Secrets Secret
  • Event Driven 2.0
  • 10 Most Popular Frameworks for Building RESTful APIs
  • Top 10 Best Practices for Web Application Testing

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: