I'm still fairly new to Git. Last week I talked about how I got comfortable with Git (Git While the Gittin' is Good). Since then, I've pretty much decided to start using Git for all of my new projects. And that's when I discovered something really cool: Visual Studio integration for Git.
Now, I haven't been living in a hole. I remember a while back when it was announced that Visual Studio started to support Git for source control (in addition to Team Foundation Server source control). I also remember hearing about a lot of folks who jumped on it right away and found that it was a bit difficult to get started.
But it's not that way anymore. So, let's create a new Git repository and see what we get in Visual Studio.
[Note: Git articles are collected here: Learning Git. And a video is also available: Git Basics for Visual Studio Developers.]
Creating a New Repository
A couple weeks ago, I published a video series on C# Delegates. Someone left a comment asking me to show using delegates with unit tests. So, I sat down to do some quick tests to see what I could come up with for those samples.
I started with the "starter" code that I showed in the video (just the base project). Here are the files (along with a ".gitignore" file that I already copied into the folder):
So, let's set up a repository for this project. First, we create a repository with "git init":
And that creates the ".git" folder on the file system:
This is a hidden folder, so depending on your settings, you may not be able to see ".git". I always show hidden files and folders (and also file extensions).
So far, so good. Next, we can run "git status" to see how things look:
This tells us that we have some files that are not being tracked by the repository. Notice that the user file "FuncActionDelegates.v12.suo" is not included in this list. That's because ".suo" is part of the items that we have in our ".gitignore" file. This is one of the reasons that it's important to have a good ".gitignore" (and we saw last week that GitHub has some for us already).
So, to start tracking these files, we should run the "git add" command:
The "." parameter says that we want to add all of the files and folders. If we run another "status", we see the files are now tracked, but they still need to be committed:
Then we commit the files:
For the "git commit" command, we used two different switches: "a" and "m". The "-a" says that we want to commit all of the pending files. The "-m" says that we want to include a commit message, which is what "Initial Project" is. We can combine the two switches into a single "-am" for our commit command.
And if we check the status...
We see that everything is all set. All of our files are now committed to our new repository.
Unexpected Things in Visual Studio
After setting up the repository, I opened the solution in Visual Studio. That's where I found out (completely by accident) that Visual Studio has a lot of Git integration. Here's what my solution looked like:
I noticed the lock icons next to my files. I'm used to seeing that when projects are hooked up to TFS source control. But I didn't do anything special for this to work with Git. Visual Studio just found the ".git" folder and started using it. I'm using Visual Studio 2013 Ultimate with Update 4. I'm not sure when the Git support was rolled in. I haven't installed anything special for this, so I guess it came in one of the updates.
[Update: Git integration has always been in VS2013. Apparently, I'm thinking back to VS2012 for the manual bits. More info on MSDN: Use Visual Studio with Git.]
So let's see what happens when we start to modify files. I made changes to the "Person.cs" file to add a custom delegate (like is shown in the video), and I also created a new unit test project. The source control information updated in the Solution Explorer with each change.
Here's what the changes look like:
And just to show that this matches what Git has, we can run another status:
This shows the same thing as the Visual Studio window. Our solution file has changed and also the "Person.cs" file. In addition, we have some untracked files -- everything in the "FuncActionDelegates.Tests" folder.
Back in Visual Studio, we can take a look at the Team Explorer window (since this is where we normally deal with source control stuff):
If we click on the "Changes" option, we can see what needs to be committed:
This shows us the same thing. We need to update the solution file and the "Person.cs" file. And we need to add a bunch of files in our "Tests" folder. Notice at the top, there's a place where we can put in our commit message.
Let's do that:
When we click the "Commit" button, everything gets committed.
This shows the commit, and also gives us a link to "Sync" to a server (such as GitHub). I haven't hooked this project up to GitHub, so we won't worry about this today.
If we go back to the command line, we can use "git log" to see our history:
This shows our commit from Visual Studio with our message "Initial Delegate and Tests".
In the Solution Explorer, we can also right-click on files and select "View History..." If we do this on the "Person.cs" file, we see the initial state and our change.
Even better, we can highlight both of these files, right-click, and select "Compare".
This shows the differences between the 2 files.
And if we have a changed file (like we have here)...
Our right-click menu has a number of options related to source control:
We can "Undo" to revert to the last committed version and "View History" as we saw above. I really like the "Compare with Unmodified" option. This gives us a side-by-side comparison with the current file and the last committed version. And finally, we can "Commit" from here. This will take us to the Commit window that we saw earlier to give us a chance to put in a commit message.
There are lots of other options as well (which I haven't had time to play with yet), including branching and merging, configuring Git, and syncing to a server. Here's the window that lets us set up that remote repository:
And we can see that there are "Pull" and "Push" options available once we're hooked up.
So, lots of things to explore without needing to use the command line.
Honestly, I wasn't expecting this to be so well integrated. I guess I had the things I had heard about the initial growing pains stuck in my head. It was a happy surprise to see that Git is so well integrated into Visual Studio without having to do anything special.
Now I am glad that I learned the basics of the command line. By doing that, I became familiar with how Git works as a distributed source control system (as opposed to the centralized source control systems that I've primarily worked with in the past). This gave me a good understanding of what was going to happen when I chose various options in Visual Studio.
Time will tell if I end up using the command line more or the Visual Studio tools more. With some practice in each one, I'll figure out what works best for my workflow.
In the meantime, if you've been worried about Git integration in Visual Studio, don't be. The basics are there (I don't know about the more advanced stuff since I'm still new to Git). So check it out. It's very easy to get started.