Showing posts with label Git. Show all posts
Showing posts with label Git. Show all posts

Monday, October 19, 2015

New Video: Git Basics for Visual Studio Developers

Git can be a bit of a stumbling block for Visual Studio developers. That's because we're generally used to centralized source control systems like TFS. In addition, there seems to be an emphasis on using command line tools to interact with Git repositories. Because of these differences, we often put off learning Git because it seems like it will be a lot of effort.

But in reality, with just a few key commands and the tools built in to Visual Studio, we can get started with Git very quickly. And that's what I show in my latest video -- along with some GitHub hints as well.

I've been using Git for less than a year, and I'm completely sold. I love having the complete repository on my machine. That means I get full history, branching, and roll-back capabilities without being connected to a central server. I'm not an expert, but I'll help you get started.

Watch it on YouTube: Git Basics for Visual Studio Developers

Or here:


For more resources on getting started with Git, check no further than here: Getting Started with Git.

Happy Coding!

Tuesday, June 30, 2015

Git Miscellany: Stuff I Didn't Know But I Do Now

You don't need to be an expert in order to show someone else how to do something. There is a lot we can learn from experts, but there's also a lot we can learn from someone who is 2 steps ahead of us, like "Watch out for that step!"

As we found out yesterday in You Aren't Instantly Awesome, and That's Okay, I'm not afraid to share what I do know even when there's much, much more that I don't know.

Last week, I did an introduction to Git presentation for a user group in Las Vegas. I am by no means an expert, but I can share my experiences in getting started with the tool and what helped me get comfortable with it. And that's what I did: Getting Started with Git.

Of course some questions came up that I did not have the answer to. Well, now I do. That's what's really great about continuous learning. So let's take a look at those questions.

Commit GUIDs
In distributed and disconnected systems (especially), we end up using GUIDs (globally unique identifiers) to mark our items. This ensures that 2 people don't use the same identifier that then causes problems when we try to bring the records together.

Git has identifiers that look like GUIDs to mark the commits:


So the question came up: How are these identifiers generated?

After a little bit of searching, I found the answer: These aren't GUIDs; they are SHA-1 hashes of the commits.

The hash is used for data integrity of the Git repository. So the idea is that when we create a commit, that changeset is used to generate a hash value. This value can be checked to make sure that the repository has not been tampered with.

Since Git is distributed, that means that I have all the files for the repository on my machine. It's possible for me to go in and monkey with a changeset in the Git repository on the file system. But if I tried to push that or otherwise try to use that particular commit, I would end up getting an error instead. If you modify the files, then the hash of the modified files will no longer match the has that was generated with the original commit.

For a little more information, you can check out this article on Wikipedia: SHA-1 / Data Integrity.

The "-u" Parameter
When doing an initial push or pull with a remote repository, the sample commands show using the "-u" switch. Here's the instructions from GitHub for pushing a local repository to a newly-created GitHub repository:


The "push" command will push to "origin" (the name we gave to our remote repository location) from the "master" branch (our main branch on our local repository).

But what does the "-u" parameter do?

I found the answer to this in the Code School - Try Git tutorial that I mentioned in my first Git article. Specifically on Step 11: https://try.github.io/levels/1/challenges/11


This tell us that "-u" sets up a default for the push command. So the next time we need to push to the remote repository, we can just use "git push", and it will use "origin" and "master" automatically as the parameters.

The same is true when we use "git pull" and the "-u" parameter.

I highly recommend the Code School tutorial. When you go through it, be sure to check the "Advice" box next to the file system display. This has a lot of helpful hints.


These aren't necessary to completing the tutorials, but they are extremely helpful as we start to go a bit deeper. I actually missed these my first time through, and I found lots of good stuff when I went back to look.

Remote Repository Options
The last question is a bit more complex. I won't give a specific answer, but I'll show you where to find what you need.

How do I set up a remote repository on our company network?

There are a myriad of options for remote repositories. Two easy off-site hosts are GitHub and Visual Studio Online. But what if we want to set up our own remote repository on our server?

It turns out there are a lot of options for this. What you choose will depend your specific needs. And this is all spelled out in the Git "Book", specifically Chapter 4. Start by looking at Git on the Server - The Protocols.

What I like about this (even though I haven't had to do this myself) is that it gives Pros and Cons to using a particular protocol. Here's a snippet:


Then from there, the rest of Chapter 4 is all about the setup process. The complexity of the process depends on your needs -- from just having a network file share all the way up to a full-blown Git server.

Keep Learning; Don't Be Afraid to Share
There are certain topics that I am very comfortable with and have a lot of experience in. It's easy for me to share what I know with others because I've probably had the same questions that they do.

There are also topics that I'm still new to. It's a bit harder to share because I know that questions will come up that I can't answer, and I know that there will probably be someone in the room with much more experience than I do. (In fact, one of the attendees at the user group meeting had been using Git as their source control system at their company; he was nice enough to help out with some of the questions.)

But I'm not afraid to say "I don't know." And it usually turns into a blog article. Just like this one.

Even if you don't know everything, it's helpful when you say "Watch out for that step!" to the people behind you.

Happy Coding!

Friday, January 16, 2015

GitHub: Hand Holding Where You Need It

I'm still fairly new to Git and GitHub (been using it about a month now as you can see from my GitHub profile). And I'll have to say that I get more impressed the more I  use them.

Yesterday, I showed some really ugly code and mentioned that folks were welcome to offer suggestions through comments or by making a pull request:
Feel free to share your ideas in the comments for this article. Or make a pull request on the GitHub repo (I'm not quite sure how to deal with those since I'm still new to Git and GitHub, but I'll figure it out when it comes up).
Fortunately, my friend Matt Johnson (expert in all things date/time related) took me up on the this and made a pull request.

Help With The Pull Request
I got an email notification about the pull request, and I was happily surprised that it came with instructions:


This is what I needed: the Git command to merge the Pull Request and a bunch of links to review it.

And when I viewed it on GitHub, I got similar help. This is right in the pull request:


And when I clicked on "command line", I got step-by-step instructions:


This is exactly what's needed by someone who is still learning the tools (like me).

I took the easy route. I just clicked the "Merge pull request" button, confirmed the merge, then went to Visual Studio and hit the "Sync" button to get the changes on my local machine.

If you're curious about the changes that Matt made, don't worry. I'll be covering those soon. He made some great improvements to the code, and we'll walk through them in the next article. If you want to look at the changes in the meantime, this is all on the "sunset" branch.

[Update: Here's the review of the changes.]

Wrap Up
The more I use Git and GitHub, the more impressed I am with the features and implementation. Yesterday, I was genuinely baffled about what I would do if someone made a pull request. Today, I merged this into my project with no issues at all.

Now I'm *really* kicking myself for not doing this sooner. But I can't change the past, so I'll just keep moving forward.

Happy Coding!

Tuesday, January 13, 2015

Git Stash - Saving Stuff for Later

I ran across another great feature in Git today (it's always cool to discover stuff that's new to you).

I was experimenting with my sample code that shows how to use Task and await (working on how to show cancellation tokens in action). I got to the point where I had some changes that I wanted to keep, but I wasn't quite ready to commit them yet. On top of that, I really wanted to apply these changes to a new branch (and not to "master" yet).

Jeremy's 'stache (2009)
TFS has the concept of shelving changes -- putting a change set into source control without applying it to the branch. I wondered if Git had a similar feature. After a quick search online, I found that it does: stash.

Note: Git articles are collected here: Learning Git.

Stashing Changes
The "stash" command is pretty easy to use. It's basically a way to push a change set onto a stack. The changes can then be popped off when we want to retrieve them. We can have multiple stashed items, but we'll just use one change set for our example here.

Uncommitted Changes
First, let's run "git status" to see our current state:

git status

This shows that we are currently on the "master" branch, and we have 4 modified files waiting to to be committed.

But instead of committing the files, we'll "stash" them.

Stashing Changes
Now it's time to save our changes for later.

git stash & git status

First, we call "git stash". This saves the changes in our working directory into the stash list. (And "WIP" stands for "work in progress" -- this shows up quite a bit.)

Next, we run "git status" and see that there are now no pending changes for "master". Our working directory has been restored to the previously committed version of "master".

This has the same effect on our working directory as if we had discarded pending changes. But instead of discarding the changes outright, we've saved them off so we can access them later.

To see the list of stashed items, we just use "git stash list".

git stash list

This shows that we have one stashed item.

Retrieving Stashed Items
To restore these changes, we just use "git stash apply".

git stash apply

This shows that we now have uncommitted changes on our "master" branch -- the same changes that we saved off in our stash.

Deleting a Stash
Applying a change set from our stash does not change the stashed items at all. If we display the stashed items, we'll see that they are all still there.

git stash list

We can get rid of an item by using the "git stash drop" command.

git stash drop & git stash list

Now when we list our items, we see that the stash is empty.

Note that "drop" just removes the first item -- popping it off the stack. If we had multiple items in our stash, only the first one would be affected.

For information on dealing with multiple items, check out the documentation: Git Tools - Stashing.

Creating a Branch from Uncommitted Items
So we've seen how "stash" works in general. But all we've done so far is save off a change set and then re-apply it to our "master" branch. Our changes are still uncommitted. We're back in the same state we were in when we started this exercise.

What I really want to do is create a new branch based on these uncommitted changes. And it turns out that "stash" has that ability as well.

Stashing Changes
We'll start by stashing our changes again with "git stash".

git stash & git stash list

After running this command, we use "git stash list" to see that our changes were saved off successfully.

Creating a New Branch
Before creating a new branch, let's use "git branch" to see our existing branches.

git branch

This shows that we have 9 branches, and that we're currently on "master".

To create a new branch from our stashed items, we use "git stash branch" and then give it the name of the new branch.

git stash branch 09-CancellationBasics

This shows that we are now on a branch called "09-CancellationBasics", and we have uncommitted changes.

One other thing to note about this process, the stashed items are automatically dropped (as we can see in the last line of the output).

And just to verify, let's re-list our branches.

git branch

This confirms the new branch has been created, and it is the current branch.

From here, we can move forward with committing changes and doing whatever else we want. This seems like a great way to move uncommitted changes to a new branch.

Visual Studio (Lack of) Support
Previously, we saw there there is quite a bit of support for Git in Visual Studio: Git Integration in Visual Studio 2013 and Getting Used to Git in Visual Studio: Branches.

Unfortunately, "stash" is not supported out-of-the-box in Visual Studio 2013. I had a feeling that not all Git features were supported -- it's difficult to get one-to-one parity between command line and visual tools. So, I'm really glad that I've been spending time with Git on the command line. This has made it much easier for me to pick up this functionality.

Wrap Up
I'm still learning Git as I go. I've often found that just-in-time learning works well for me -- learning about features when I have a use for them. And I was very happy to see the functionality that is available with "stash". I often do experiments that I don't want to save, but I don't want to completely discard them either. This ability to save changes for later (and also easily move them to a new branch) will come in handy in the future.

Happy Coding!

Tuesday, December 30, 2014

Getting Used to Git in Visual Studio: Branches

I started working with Git not too long ago (here's how I got started). Soon after, I discovered the built-in support for Git in Visual Studio 2013. I've been working with Git and Visual Studio quite a bit over the last several days, so it's time for an update to show the things I really like.

I started by primarily using the command line. But as I've become more comfortable with the features in Visual Studio, I find myself using the VS GUI more and more. This is based on the project that I've been building for Exploring Task, Await and Asynchronous Methods (the GitHub project is at https://github.com/jeremybytes/using-task).

I've been using branches to document the various stages of the code, so I've had a chance to play with the branching and syncing functionality in Visual Studio. Let's take a look.

[Note: Git articles are collected here: Learning Git. And a video is also available: Git Basics for Visual Studio Developers.]

Creating Branches
The Team Explorer window has everything we need. To get back to the "Home" screen, just click the little house icon on the top of the window. This gives us a quick way to get back to the main menu for our source control:


From here, we can select "Branches" to see all of our current branches:


In addition to having a list of the branches, the bolded entry lets us know which branch we are currently on. In this case, we are on "master".

To create a new branch, we just click the "New Branch" item (it's sort of a cross between a link and a drop-down). It gives us the following options:


Now we can type in our new branch name, select which branch we want to base this on (in this case "master"), and we can also checkout the branch after it's created.

Here's what we get:


We can see a couple of things here. First, our new branch is bolded; this is the current branch. In addition, it is listed in the "Unpublished Branches" section. This means that this branch is on our local system, but it has not been published to our server.

Publishing Branches
Publishing our branch is very simple. We simply right-click on it and choose "Publish". This will push it to the GitHub repository that we have set up. And we can see that here:


Now the first time that we "Publish" a branch from our local repository, we'll need to enter the information for our server-based repository. For this project, we're already configured to go against the "using-task" repo on GitHub.

Changing Branches
It's very easy to switch between branches. Here's our "MainWindow.xaml.cs" file, and as we can see, this is on the "07-TestBranch" that we just created:


From here, we can double-click on a different branch, or we can select from the "Branch" drop-down at the top of the "Branches" window. If we choose the "03-Await" branch, we see that our source code updates automatically:


And if we have uncommitted changes, we'll get a warning that we need to deal with them before changing to a different branch -- by either committing the changes or discarding them.

I have found that this makes it very easy to jump among the branches. For my articles that use this solution, I wrote (and published) all of the code ahead of time. This let me work out what my code samples would look like for the article.

Then when it came time to write the article, I could switch to the various branches to take screen shots, run the application, and show the changes for each step along the way.

Now in production applications, I generally have very short-lived branches -- usually just long enough to create a new feature and then merge the changes back into the main branch. But if I did run into a situation where I needed to manage multiple branches, this would be a very convenient way to jump among them.

Comparing Branches
It's really easy to see the changes between branches as well. For example, we can right-click on "MainWindow.xaml.cs" in the Solution Explorer and choose "View History...". This gives us all of the commits that affect this file:


Notice that in addition to the commits, we can also see the various branches in our repository. So we can make selections based on individual commits or based on the state of a particular branch.

We can also easily do comparisons. If we select both the "07-TestBranch" and the "03-Await" items, right click and choose "Compare...", we get the difference window that we saw in a previous article:


This makes it really easy to see what was added or removed.

Syncing with GitHub
So let's say that we've made some changes to our "07-TestBranch". It's really easy to commit those changes locally and then sync them with GitHub.

After changing the file, we go to the "Changes" section in Team Explorer:


This shows that we have one file that has been changed. So we can add our message and click "Commit".

After we do this, we'll get a message that the changes have been committed, but we still need to sync them with the server. For this, we'll go to the "Unsynced Commits" section. (Remember, we can click the "Home" icon at any time to get back to the menu):


This shows that we have some local commits that need to be pushed up to the server. The great thing here is that we just need to click on "Push", and it handles everything else for us.

Merging and Clean Up
So let's say that we're done with this branch. We just want to merge this back to "master" and remove the branch from our repository.

Merging is very easy. We just go back to the "Branches" section and select "Merge". This gives us drop-downs to fill in:


In this case we want to merge from our "07-TestBranch" back to "master". This is especially nice since merging from the command-line is a bit more involved (first by switching to the branch you want to merge to, then entering the "merge" command with the name of the branch to merge from).

I'm not sure what happens if there are any merge conflicts. Quite honestly, I try to avoid those as much as possible, so I hope that I never have to find out.

It turns out that I changed my mind about publishing the branch. I really don't want this branch in the local repository or on the server repository. To unpublish a branch from the server, we just have to right-click on the branch and select "Unpublish branch".


The message at the top tells us this was successful, and our branch is now listed under "Unpublished Branches". And if we check GitHub, we'll see that the branch is now gone:


To delete the branch, we just need to switch to another branch first (we'll use "master"). Then we can right-click on the branch and select "Delete".

Note: the "Delete" option is disabled (grayed-out) for the active branch. So, you must switch to another branch before the delete option will be available.

And with that, our branch is gone:


I think I'm beginning to really like this.

Wrap Up
I started out learning Git with the command line, and I'm really glad that I did that. It gave me a good idea of how Git works, and how distributed source control behaves differently from centralized source control. And as I've used it over the last few weeks, I'm liking it more and more.

And I'm really impressed with how well Visual Studio works with Git. The built-in tools are top-notch. The thing that I like most so far is that it makes it much easier to interact with the remote repository (GitHub in this case). I see myself using the Visual Studio tools as my primary method of interacting with Git repositories. But I'll still use the command line from time to time to make sure that I don't forget how things work.

Happy Coding!

Sunday, December 21, 2014

Git Integration in Visual Studio 2013

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".

Viewing History
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.

Other Options
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.

Wrap Up
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.

Happy Coding!