Showing posts with label GitHub. Show all posts
Showing posts with label GitHub. 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, 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!

Wednesday, December 17, 2014

Git While the Gittin' is Good

Okay, so I'm a bit late to the party, but I'm finally getting started with Git. This is something that I've been meaning to do for a really long time, but I haven't set time aside for it. The few times I've taken a brief look at it, it seemed pretty different from the source control I'm used to, and I knew that I'd have to dedicate some time to get my brain to adjust.

Although I've only been using it for a short time, I'm much more comfortable with the concepts than I was before. I think I'll be using it regularly from now on. Here's what I did to get started.

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

Initial Learning
First step was to learn something about Git. My initial search led me to Code School - Try Git. This gives a step-by-step introduction to basic commands. I liked this because it started at the very beginning of how to create a Git repository.

Overall there are about a dozen commands that are shown. This is an interactive experience. Each step has 3 different windows.

The description / instructions:

The command line:

The file system:

What's nice about this is that the command window is "live". So you can type whatever commands you like. In the example above, the instructions are to type "git add octocat.txt" (and you can also click the text to automatically add it to the command window). But in the command window, I typed "git status". This shows the status of the repository (with the file that needs to be added) as well as a hint message that I didn't type what I needed to complete that step.

This gives a bit of freedom to check status and list branches if you want to get some more information rather than simply mimicking the commands.

There are 25 steps altogether, including basics like adding, modifying, removing, and committing files, creating branches, and associating a local repository with a GitHub repo.

Installation
After going through this, I needed to install Git on my machine so that I could start trying it out myself. In addition, I created an account on GitHub (finally).

To download, I just went to http://git-scm.com/downloads. There were a few choices to make during the installation process. I'm not quite sure if I made the right decisions (such as the defaults for line breaks). I'll figure out if I made the right choices as I get some more experience.

One of the options is to integrate it with Windows Explorer. With that in place, you get some new options in the right-click menu in file explorer. These are things like creating new repositories, opening Git command windows and even a Git GUI application. I've tried to stick with the command line -- at least until I'm comfortable with the commands.

Git will keep track of changes, branches, and history all on the local machine -- there's no need for a repository on a server. Now obviously, there are advantages and disadvantages to this. The biggest advantage is that you can commit changes, create branches, and revert to previous versions all while being off line.

I really like the idea of this because I don't live in the "always connected" world. I like to have applications that work even when I don't have a network connection. And Git definitely offers that.

GitHub
The downside to having everything on the local machine is that it's not automatically backed up on a server somewhere. And that's where something like GitHub comes in. GitHub hosts your repositories (among other things) so they are somewhere other than just on your local machine. Plus, it makes it really easy to sync repositories with other machines and share your code with the public.

To create an account, just go to http://github.com. In addition to walking you through the process of creating an account, it also has some pretty good guidance for creating repositories -- suggesting files to add, putting in ".gitignore" files, and so forth.

And having a prebuilt ".gitignore" file is really cool. The ".gitignore" file is how you tell Git about the items that you do *not* want to be tracked. For example, in the Visual Studio world, we don't usually want anything in the "bin" or "obj" folders in our repository. GitHub has a whole bunch of files supporting different languages and environments. You can take a look at the Visual Studio file here: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore.

This is a fairly extensive file, and people keep adding to it. In addition to the basics, it also ignores test results, ReSharper files, and many other tools files. The nice thing about this is that it is a file in a GitHub repository, so the community is involved in keeping this up-to-date (57 contributors so far).

Git Practice
One of the reasons that I wanted to explore Git and GitHub was because I had seen presenters use it in their demos -- using branches to jump to different versions of the code to show progress. Recently, a few other folks I know have been looking into this. Joe Guadagno is one of those folks. He posted a demo that has branches for each of the steps: https://github.com/jguadagno/AzurePhotos.

It's interesting to look at the branches:

This way, you can jump to a specific step, and you can also use comparison tools to see what the differences are between files.

My Repository
So, I decided to try to put together something similar. I started with my simplest demo -- showing the syntax of a lambda expression. You can see the demo in action on YouTube: Anatomy of a Lambda Expression.

This gave me a chance to create a local Git repository, add the ".gitignore", and add the initial project files. Then I did step-by-step changes to the code and committed the changes after each step. Then I figured out that I really wanted to have branches (like Joe's repo) rather than just individual commits, so I deleted the ".git" folder and started all over again.

Yep, that's right, I deleted the repo and started all over. It was really easy to do. And I was walking through established demo steps, so I didn't have any history that I needed to hang on to.

This time, I created branches as I walked through the steps. This demo is pretty simple, but it's pretty important to show each step. I ended up with 7 branches in all -- many of them only having a difference of 1 line of code.

After I got things the way I wanted them, I pushed it to GitHub. You can see the repo here: https://github.com/jeremybytes/lambda-syntax.

Including all the branches:

What I Learned
I'm not sure if I'll be posting my demos like this to GitHub. But this exercise was very helpful for me. I have a much better handle on how to use Git. Here are the commands that I used in this process:
  • init
  • add
  • commit
  • status
  • log
  • branch
  • checkout
  • merge
  • push
  • remote
It wasn't a fast process, but it wasn't that difficult, either. There were times when I had to go back and look up commands or review the steps from the "Try Git" lessons. And I also had to Google some error messages. But after spending a couple hours with it, I'm pretty comfortable. I know that I still have a lot more to learn, but with the basics under my belt, I'm much more likely to use it regularly and move forward to learn the other features.

I have a system that works pretty well for me from a presentation perspective. So although I may not put everything I have on GitHub, I'll start putting my code into local Git repositories. And I can already think of a few projects that I can put online to share with other folks (like Conway's Game of Life, the rewrite of the legacy project, and the practice code analyzer).

I'm very glad I finally got around to this. Git is definitely a useful tool, and I see myself using it regularly moving forward. If you're not already using it, be sure to take a look at it to see how it can fit into your workflow.

Happy Coding!