JeremyBytes - Git Basics for Visual Studio Developers

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is Jeremy Clarke of Jeremy bytes calm and today we're going to be looking at the basics of using git and I'm approaching this from the standpoint of Visual Studio developers the reason I'm doing this is because a lot of us are familiar with centralized source control systems like TFS but we're not quite familiar with the distributed systems like git now today we'll be looking at some of the basic commands and get including creating a new repository and using some of the built-in features that we have in Visual Studio 2013 now the tools in Visual Studio 2015 are a little bit different but I like to show 2013 since it's a little bit easier to work with so let's head over to our code and get started using git so we're going to start out with a project that I've already created called lambda expressions and this is a pretty simple application as we'll see in just a minute but before I open it up in Visual Studio let's go ahead and open up a command line so that we can get started using git now when I did the installation of git I use the defaults and part of the default installation is to add git to the path of our system that means we can open up any command prompt and start using it right from here so here I am in the folder for my solution and to create a new git repository I'm just going to type get in it now if we flip over to our file system what we'll see is that we have a new hidden folder dot git now the interesting thing about this folder is it includes the entire repository that means it will include all of the history for this particular repository all on my local machine now this is a hidden folder so if you don't have hidden folders showing you will not see this so now that we have our repository let's go ahead and start using it I'm going to start out with git status the files in red are untracked files so before we can put these into our repository we'll need to add them so that they are tracked but before we do that I want to point out that there's some files that we probably don't want to include in our repository one of those is the dot suo folder this actually contains the user settings for our solution and it's something we don't normally want in source control and there's some other files that we want to exclude as well well get gives us an easy way to do this with configuration there's a file called get ignore that we can use now I have one here on my desktop so I'm just going to drag it into my folder and now watch what happens when I say get status now we can see that the suo file is no longer showing that's because it is being excluded by get so let's just pop open the get ignore to see what's inside what we can see here is there are a lot of exclusions in this file now this is specifically for visual studio and it's set up for the things that we normally want to exclude from our visual studio repositories so this does include the things like the suo file as well as some other user files and it also includes our output folders so our debug release as well as some temporary folders including option bin now you probably don't want to build this file yourself now fortunately for us there is a github project for git ignore files and it includes a variety of environments including Visual Studio now me personally I generally need just the basics so I'll go ahead and keep a copy of get ignore on my local machine and then just copy it into my folders where I need it now I mention that we will need to add files before we can commit them to our repository and fortunately get gives us a lot of help here we can see that we have a note here to use git add to track the files so let's do just that so I'm going to say git add dot and what that will do is it will start tracking all of the folders now git does have its roots in the Linux environment so there are a lot of command line options that follow those conventions so when we use dot that's the equivalent of star dot star that we normally have on the command line so let's do another git status to see what we have now we can see that all of these files are listed in green that means that these are now being tracked and if you'll notice this is showing us all of the files in the full folder structure that we have here so all of these files in the project are now being tracked and they're ready for us to commit to our repository but before we do that let's open up visual studio to see what we have here now I don't have any special plugins forget installed on my machine so the functionality that we see here is what we get out of the box with Visual Studio 2013 now right off the bat let's look at the solution Explorer if you notice all of my files here have plus signs that means these are new to the repository but they haven't been committed yet and that really shouldn't be much of a surprise to us right now so let's flip back to our command line and do our first commit so for this I'll just say git commit and then normally we want to put a message here now if I just hit enter here it will open up an editor so that I can type that in but it's a lot easier just to do it as a parameter on the command line so I'll say - m4 message and we'll just say that this is the initial project and when we hit enter we'll see that all of those files are committed and now if I do get status we'll see that there's nothing new to commit so our first commit is actually complete and if we look over in Visual Studio will see that our plus icons have changed to lock icons so this is showing that the files that we have on our machine are current with what we have committed in our repository now the project we have here is really simple in fact all we have is a button that's not hooked up to anything right now and this is the same demo project that I use when I'm showing people how to use lambda expressions if you want some more information on that you can check out my other video on anatomy of a lambda expression we're just going to use this project so that we can get a number of commits so that we can get comfortable using it so all I'm going to do is start out by just adding an event handler to our button so we'll say click Me button click plus equals and then I'll hit tab twice to let Visual Studio go ahead and generate that event handler for us and then inside our event handler will just say message box show hello world now notice in our solution Explorer our main windows ml dot CS file the one we're currently working on now has a checkmark icon next to it that means we're currently in the process of editing this so let's go ahead and build our application and run it and then we'll see our functionality is just to pop up a holo world message box now to commit these changes I'm going to go back to the command line and we'll just say get status and we'll see that we have our one file main window dot Samuel dot CS so I could try to do a commit just like I did before so I can say git commit dash M added event handler but we'll see that that actually doesn't work that's because our file needs to be added before we can commit it just like we did initially now fortunately for us get is here to help us out look at the hint that it gives us so it says use git add and/or git commit dash a so the dash a switch will let us add and commit files in a single step so if I say git commit dash a and then we'll go ahead and add a message so we'll say dash M added event handler this will add and commit the files at the same time but something to note is that if I have two switches I can actually combine them so it's actually more common to see git commit dash a.m. that combines both the ad and the message parameters and so now if we hit enter we do see that we have a successful commit and if we look at Visual Studio will see that our lock icon is back on the file that we just modified so now let's go ahead and look at what our repository currently looks like so we'll use git log for that and this will show us the two commits that we've done the one for the initial project and the one when we added the event handler now I want to point out the IDs for these commits it looks like a gooood a globally unique identifier but it's actually a hash of the commit itself and the primary reason for this is data integrity all of the files including all of the commits exist on my machine that means that if I know what I'm doing on the file system I can go in and make a change to a previous commit now if I do make those changes the hash will no longer match and so it will be rejected by the get system so this helps us give us confidence even though we're using a distributed source control system we know that the contents have not been modified since they were initially committed so let's start taking a look at some of the other features that we have in Visual Studio so if I right-click on the file that I've modified and choose view history this actually shows those seem to commits the initial project and where we added the event handler and you'll notice we also have the first part of the hash so we're showing the first eight bytes of the hash for each of these commits now for a lot of the tools that we haven't get if we do need to reference a particular commit we can just use those first eight bytes rather than typing in the entire hash okay so let's make a few more changes and work a little bit more with visual studio so for this I'm going to take our event handler and I'm going to inline the code and what this is going to do is create an anonymous delegate rather than the name delegate that we have here so I'm just going to type in the delegate keyword and then paste the parameters and the method body from our separate event handler now this has the effect of in lining the code and creating what's known as an anonymous delegate which is just a delegate without a name and if we run our application we'll see that we get exactly the same results that we had before so now let's see how we can commit these changes from visual studio for this I'm going to use the team Explorer now if you don't have the team Explorer already open you can get it from the View menu and these are the items that we have available to us at the home tab for Team Explorer now to get back to this all we need to do is click on the home icon so wherever we are in team Explorer we can always get back to this location so I'm going to click on the changes tab and we can see that we have one item which is our main window zamel CS file and as you expect if we flip over to our command line and do a get status we see exactly that same thing now rather than doing the commit from the command line we'll do it right here from the changes tab in Visual Studio so in this case we'll say a change to anonymous delegate as our message and then we'll just click the commit button now you'll notice we have a notification section here and it gives us the first eight bytes of our hash and there's also a link to sync to share it with the changes on the server now we don't currently have a remote repository setup so we don't have a server to sync with but we'll be looking at how to use github in just a little bit now if we go back to our command line and do a get status we'll see we're completely up to date because we committed the changes through Visual Studio and if we look at our log we'll see all three commits the initial project where we added the event handler and where we change to the anonymous delegate now the nice thing about understanding the command line and Visual Studio is that I can flip back and forth and use whichever means is easiest for me at the time so for me personally I found that there's some operations that I prefer to do on the command line and there's some operations that I prefer to use through the visual studio interface now let's move on and look at branching a little bit now I'm going to say git branch and this will list out all of the current branches that we have in our repository now right now we can see we have one branch called master and this is the current branch now to create a new branch we'll just say get branch and we'll call this lambda expression and now we see we have two branches but master is still the active branch now if I want to change to the lambda expression branch all I have to do is say get check out and then type in the name of my branch and now we can see that lambda expression is our active branch now dealing with branches and visual studio is actually a little bit easier if we go to our branches tab we'll see that we have two branches listed lambda expression and master and notice lambda expression is the bold one that tells us that this is the active branch so let's go ahead and make some changes on this new branch so I'm going to remove the delegate keyword and I'm going to add the lambda operator the equals greater than sign in between the parameters and the method body now we have a lambda expression and if we run the application again we'll see we get exactly the same results that we had before so let's go ahead and commit this change as well now I am committing a lot of changes normally I don't commit after every line of code but I want to have several commits here so that we can look at them so for this will say changed to lambda expression and we'll go ahead and commit that and then lambda expressions have this thing called parameter type inference which means if the compiler can figure out the parameter types we don't have to type them in now since the compiler can figure this out these are still strongly typed so we see e is still of type routed event args we just don't have to type it in and if we run our application now we'll see we get exactly the same results that we had before so let's go ahead and make a note that we added parameter type inference another common thing to do with lambda expressions is to use single character parameter names just because it is such a compact syntax so we'll go ahead and just change sender to s and again if we run our application we get the same results that we had before and we'll go ahead and say changed to single letter parameter names and we'll make one more change here we actually don't need the curly braces if we only have one line in the body of our lambda expression and we can also get rid of some of this white space and if we run our application again we'll see we get exactly the same results that we had before so for this last commit we'll just say removed curly braces and white space now again if you want some more information on the demo that I just showed be sure to check out anatomy of a lambda expression which will go into some more details about lambdas specifically but again we're concentrating on what get gets us in Visual Studio so I'm going to go back to the solution Explorer I'm going to right click on the file we've been modifying and choose view history again now this time we see all of the commits that we've done so far and in addition we have markers for our branches so we can see where our master branch is and we can see where our lambda expression branch is and one really great thing that we can do from the history window is actually compare these changes so I can control click on multiple items right click and say compare and this will give us a side-by-side comparison that shows us what's been changed in these files and we're not limited to just comparing branches here I can go ahead and pair any of these commits that I like and in fact you can pick more than two files at a time if you'd like now the really great thing about having all of this repository information on our local machine is that we can switch back and forth between branches really really easily so I'm going to go back to the team Explorer and we'll go to our branches and if I want to go back to my master branch all I have to do is double click on it and notice that my file and visual studio updated to go along with that branch change now a really cool thing about git is that whenever we switch branches it actually updates the files and what's known as our working folder so if I were to go into the hello world and then take a look at the main windows MOC s what we would see is this is showing what's currently showing in Visual Studio which is the master branch that I have selected right now now what that also means is that if we switch branches from the command line it will be modifying the files in the background so we'll say git branch and we'll see we're currently on the master branch but let's go ahead and check out lambda expression now we can see the lambda expression branch is currently active and if we look in visual studio we'll see that it's updated as well to show that lambda expression is the active branch and then if we click back into our IDE we'll get a pop-up that says the files been modified outside of the editor because again that files being replaced on the file system so we'll go ahead and say yes and we'll see the most recent file in our editor so we can see we have the choice of changing branches on the command line or we can easily change branches by just double clicking between them in visual studio so far we've only been working with a local repository that means all of these changes all of these commits all of these branches exist on my local machine and my local machine only now I really like that because that means if I'm disconnected maybe I'm on an airplane or a hotel in the middle of nowhere I can continue to work on my projects and still maintain the history the ability to rollback and the ability to create and switch to new branches without having to have a connection to the central server now unless you are working all by yourself you probably do want a central server out there somewhere and there's lots of options to choose from including Visual Studio online which hosts get repositories now I'm going to show you some of the basics of using github this is one of the most popular repository hosts out there forget repositories so for this I'm just going to open a browser and go to github.com now one thing I really like about git and github is that they do hold your hand where you need it so when you're brand-new to the system if you just follow the on-screen prompts you're probably going to be okay now I already have a github login so all I needed to do is create a new repository so for that I'm just going to click on the new repository link and for this let's go ahead and give it the name a video get sample 2013 since we're using Visual Studio 2013 now there are a few more options that we can do here so we can put in a description and then we can choose whether this is a public or private repository now if you do want private repositories you do have to pay for that but public repositories are free to anyone who signs up we also have the option to initialize the repository with a readme and that's a very common file we'll look at it in just a bit but I won't add it here and then notice we have the option to add a get ignore file so this is really useful as you can see there are quite a few different environments that we can choose from here including Visual Studio which is way down at the bottom but since we already have an existing repository we will not add that here now if I was creating a repository from scratch and I started at github I'd probably add all of this and then bring it down to my local machine but since I already have items on my local machine we'll just kind of leave everything blank for now and then fill in the pieces as we go now the last option we have here is that we can include a license along with this and this has some of those standard licenses including the Apache and the MIT licenses but like I said we'll just go ahead and choose create repository with a pretty much empty repository so like I said github is really good at holding your hand when you're new to things so notice that what we have here we have a quick setup so that if we're using the get desktop tool we can set it up really easily or you can see how we can create a new repository on the command line or we can push an existing repository from the command line or we can import code from another repository now since we already have a repository let's go ahead and just use the command line here and so what I'll do is I'll just go ahead and make this a little smaller so that I can see those commands now what I'm going to do is use git remote to add a reference to this remote repository so when I say add I'm going to create a mapping that I can use more easily on my machine now if you look at the instructions we see we have git remote add origin and then we have the web address for this particular location so again it's HTTP github comm then my user ID Jeremy bytes and then the name of my repository video - get - sample - 2013 and then dot git now when I hit enter all that's doing is creating the mapping now you might be curious about this origin what we've just done is given an alias to this remote location so whenever I say origin in my local git repository it's going to refer to this remote location on github now using origin is just a convention we can actually name this whatever we want so let's push some files into our remote repository server this will say get push and I'll say origin master so what this is saying is I want to push to the origin location from the master branch of my local repository and when we hit enter it will prompt me for my user ID which is Jeremy bytes and it will prompt me for my password which I will not say outloud and then with that information it will go ahead and push the master branch to our origin our remote repository so now I'm going to go back to github and just refresh this page so this is just the root of our remote repository and now that we have some items up there we'll see that our displays changed now there's a few things to note here first if we look at our branches we'll see that only the master branch has been pushed the lambda expression branch has not and we'll take a look at pushing that in just a bit and we can also look at the commits that we have here and in this case it's just the three commits for the master branch not the rest of the commits that we have on our lambda expression branch and we'll take a closer look these in just a minute so let's go back to visual studio and take a look at how things have changed there so if we go and look at our branches now what we'll see is we have published branches versus unpublished branches now the published branches tell us that they are on our remote repository and the unpublished branches tell us that it's only on our local machine and it's not part of our remote repository now this is one of the screens that's changed pretty significantly in Visual Studio 2015 so it actually keeps the local branches and the remote branches a little bit better separated so we've seen how to push a branch from the command line we just use the get push origin master how do I push a branch from Visual Studio well it's pretty simple we'll just right click on the lambda expression branch and say publish branch and we'll see that we get a message that says it was successfully published and if we go back to github notice that we have a passage here that lists lambda expression as a recently pushed branch so even without refreshing the page we actually get some updates based on our remote activity and if we refresh the page we'll see that now we are showing two branches and let's take a look at those so here we can see both our master branch and our lambda expression branch and it's really interesting when we look at the comparison so notice this says the lambda expression branch is four commits ahead of master that means since we've branched off of master we've actually made four commits to this particular branch now before we look at merging anything back let's just go back to our main screen and here we're on the master branch and we can see that right here on the screen now one thing we see here is there is a suggestion to add a readme file and if you look at the repositories on github pretty much everybody has a readme file the nice thing about this is it shows up on the main page over of your repository so that you can provide some documentation or additional information so for this I'll just say this is the sample project forget basics for visual studio developers and we'll go ahead and add a URL where people can get some additional information now this is in a markdown format so if you're familiar with markdown you can style this however you like now I can actually commit this to the repository right here that means I can actually make changes to file and code on the website now for code files I would not recommend that but if we're talking about making subtext changes sometimes it's really easy to just jump into the file and make that spelling correction so for this we'll just say added readme and we'll go ahead and commit this and by default it will go ahead and commit that to the master branch so now we're in a bit of a situation we have a file that's on our server that's not on our local machine so if we go back to our file system we'll see that I don't have that readme IMD file on my local machine but it's actually really easy to get from the command line now to do this rather than pushing from our machine to the server we'll pull from the server so we'll say get pull origin master so we're actually pulling from the remote origin to our local master branch and if we hit enter we'll go ahead and see that those files come down and if we go back to our file system we'll see that now we have that readme MD file in our local machine so now let's go back to github and take a look at our branches now if we look at our changes will see that our lambda expression is still 4 commits ahead but now it's 1 commit behind and that's because we added that readme file to the master branch which is not on the lambda expression branch now really what I'd like to do is merge the changes from my lambda expression branch into the master branch and for that let's go back to visual studio now for this I'm going to go ahead and click on the merge link and we'll see that we have our choices of what branches we'd like to merge so I'd like to merge from the lambda expression branch to the master branch now this is one area where I appreciate the GUI tools that we have in Visual Studio from the command line we need to make sure we're on the rayker entrench and then merge into the branch that we want so it is a little tricky to get things in the right order but from visual studio we just choose our from and our two branches and then we just click on merge so now let's go back to our command line and just do a status now if we look at this it says that the master branch on our local machine is actually ahead of the master branch on origin our remote machine by five commits so this is telling us that we really need to do a push to get our local changes up to the server but rather than doing that from the command line again we'll go to visual studio and look at these tools so for this I'm going to go to the unsynched commits tab of our team Explorer and notice that we have sections for incoming commits and outgoing commits so this will show us things that are changed on the server that we don't have locally and it will also show our local changes that need to be pushed to the server so in this case I'm just going to click on the push link and that will go ahead and push those changes to the remote server and if we go back to github and refresh our page now we see lambda expression is now two commits behind master and if we look at the actual commits we'll understand why so in addition to all of the changes that we had on the lambda expression branch on the master branch we added the readme which we saw before but then the merge is also a commit so that's why the lambda expression branch is showing two commits behind master now we do have a lot of options here one thing that's nice is we can click on any of these hash values and it will show us all of the files that were changed in this commit so most of the commits that we had just had one file changed like this one here where we added our event handler but if we go to our initial project what we'll see is all of our files have changed because we're adding new things and so that includes all of the files from that initial project so by doing this we can actually look at what items are part of each commit now one last thing I want to do before I go is I actually want to remove that lambda expression branch that was just a temporary branch and I really don't want that published anymore well in Visual Studio I can right-click on the lambda expression branch and choose unpublished branch and then if we go back to github and look at our branches we see now it's gone we only have the master branch but this branch is still in our local repository and we can pull files from it if we'd like to now if we'd like to get rid of it entirely we can just right click on it and say delete and now that branch is completely gone so if we go back to our command line and look at our branches now we see we only have the master branch left so we've just barely scratched the surface with git but hopefully this gives you a good enough idea of how to get started and that it's not really hard to use and based on our preferences it's easy to use the command line and it's easy to use the built-in tools that we have in Visual Studio for more information and articles be sure to visit www.howtouseashoppingcart.com/kickstart
Info
Channel: Jeremy Clark
Views: 8,654
Rating: undefined out of 5
Keywords: Git (Software), Microsoft Visual Studio (Software), Software (Industry), Source Control
Id: H0OcllB2IwA
Channel Id: undefined
Length: 30min 46sec (1846 seconds)
Published: Mon Oct 19 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.