Git 101 Basics - Computer Stuff They Didn't Teach You #4

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends I'm Scott Hanselman I hope that you subscribed to my channel as well as leave me comments about the kinds of things that you'd like to learn I've been doing computers and programming for about 30 years professionally and about 40 years if you don't count getting paid for it I don't know everything but I know a bunch of little stuff I'm kind of a Swiss Army knife if there's something that you want to learn or something I can help you learn please let me know and I'll make a little video about it this one here I thought would be fun if we talked about get now there's lots of great get tutorials and videos and entire classes I don't really like the way that they're expressed there's a lot of confusing diagrams and branches so I thought I would just do I get basics for regular people it does assume a few things and let's just do this let's go to picture and picture and talk about some of the assumptions so the first thing is now I'm using Windows you can certainly use a Mac if you want to I'm using the Windows terminal your terminal or your command prompt might look a little bit different like this that's fine I recommend you get the Windows terminal I also recommend that you go to my blog and you search for Windows Terminal pretty prompt and what a pretty prompt means is it's a prop that has information on it I want to point out that it doesn't just have the path but it's got some nice colors and it tells you information about get that I'll show you it shows your name the name of your computer this will take you just five or ten minutes to set up it'll add a lot to your get experience I would also recommend that you get visual studio code that's this application over here on the right so I've got the Windows terminal here on the left and I've got Visual Studio code right here I'm going to close them both and start over I'm going to open up the windows terminal I'm using PowerShell for what I'm doing here that's all explained in the post on a pretty prompt I'm gonna go to my D Drive and I have a folder called github have another folder called learning git and you'll notice when I go in there my pretty prompt immediately says hey mein what's mean you might see master I like to use mein you might also see trunk those are your starter branch it's like a family tree and the main branch is the kind of the grandparent of the entire thing and we'll see how branches work in just a little bit in this folder by type dir there's nothing I can also type start dot and I can open up Explorer Windows Explorer and see that there's nothing as well we'll come back to that I've got Visual Studio code so I'm gonna type code dot where dot is the current folder remember that we're in the folder called D github learning get when I say code dot it's gonna open up visual studio and they might give me a little welcome I'll close that and you'll see here nothing okay this is important because one of the things that get does and does very well is it tries to lie to you it lies to you to make your life better make your life easier now not all lies are bad in this instance what it's doing is it's hiding things why is this folder a git repository a place for my source control if I go up one folder you see how it doesn't say main if I go back down a folder it does say main so there must be something special about this empty folder well if I click in Windows Explorer on view and I click hidden items there is a hidden folder right here called dot yet traditionally if a folder has a dot in front of it it means it's hidden from you now if we open that up we can see that there's a bunch of stuff in here don't touch any of this you'll mess it up this is basically all of the stuff that git is hiding from you it's your database of changes it's your repository it's the secret sauce that makes it work never ever mess around in that folder just acknowledge that it's a friendly lie I'm gonna hide that and let it lie to me now I'm gonna go and I'm gonna use the windows keys and I'm gonna say windows left arrow and then have windows right arrow so I've got on the right here my empty folder and on the Left I'll clear my screen my empty folder so this is a way to think about my files where I have the graphical information on one side and I have the command line or the terminal version on the other side now I can use any editor I can use Visual Studio code I can use notepad means whatever makes me happy and I could say something like notepad test dot txt this is not Visual Studio code this is something super basic and I'll say test dot text and notepad says hey I don't know what that is I've never heard of that and I'll say yes I'll say this is a test this is my first test and I'll save it and you can see it's right there so there's a file you can see when it was made you can see that it's real and then it exists okay I'll close that and I'm just gonna press Enter right here and you'll notice something interesting just happened my prompt my terminal this custom pretty prompt that I asked you to setup turn to color it's not green anymore it's yellow and it says hey one item has been added no items have been changed and nothing's been removed remember how we said that this is a magical folder there's a hidden folder there that dot git folder noticed that something got added but if I make a change to that file another test nothing really happened there didn't do anything that's because we didn't tell get about it I can't go back to a previous version I can't rewind time because nothing has changed okay nothing interesting has happened there other than the fact that get noticed something there so that's interesting right now that is a there's a modified repository with one file but I haven't done anything I haven't told get about it so let's see we can tell git what's going on I can say git status now git is the command that I'm typing here and that was installed when I went to the git website and I installed get for my system I have Windows you might have Mac or Linux and that's okay and I'll say get status and it says hey I want a branch called main or master or trunk I don't have any commits I haven't told you about anything but I do have some files that no one tracked and I mean they're there but they're not being tracked I'm not watching them for you so there's really nothing added to commit but just to remind you again untracked files are present so hey you know use git add to track those get out ok get add and I want to hit tab get add I just hit the tab key on my keyboard and that meant I didn't have to type test dot text so cool what's happened here it's very subtle but our little prompt changed it went from saying hey something important happened to being all right something's modified notice that that's the same icon that we saw here that modified icon nothing's modified nothing's removed one is added but our our repository has been modified all right let's do it get status again cool we're on the branch no commits yet you still haven't really told me about anything but you're you're setting up things to be committed you put a file in there this is called staging you've put a file kind of ready to be staged committed but we have we've kind of set ourselves up for a commit but we haven't actually done it yet I could actually add another file notepad test to hey this is another file notice on the right hand side it just appeared okay well that's interesting look at this there's two of these things what's going on well here there's one file that we are ready to commit and then there's this other one remember that warning hey something's going on there's another file that's kind of sitting around if I say get status we say all right I'm ready to go with this one ready to tell everyone about it commit it put in the database but you know there really is an untracked file here just want to let you know there's two files in this folder one I know about and what I don't so I'm gonna add that one too when I say git add test2 there we go so now I've got two files cool ready to go so now let's commit them I'm gonna say git commit and you have to tell the system what what you think about that you have to give it a message to acknowledge and give information about that commit I want to batch these files up and I'm going to say git commit hey this is my first two files okay when I hit enter cool all right well two files are changed there's a thing called an insertion it's kind of about a number of lines don't worry too much about that but the fact is we added two files and look we're back to green again so cool that means that everything's cool I have two files here okay now what if I accidentally delete these files oops they're gone I hit enter like whoa whoa what's going on two files have disappeared that's not that's not good okay well I guess you meant to do that right you have some changes I guess you want to stage them and let me know that you deleted these two files so how can we do something about that you want to add those and tell people about that you committed these files you deleted them you meant to remove them or do you want to put them back well shoot that was an accident I'm gonna say git restore test that text watch the right-hand side and get restore test two dot txt they're back they're back I didn't mean to do that and I'm back at a good place get status all right cool that's nice I can say git log and I can see that there's a there's a ledger there's a log there's an audit log and I can go and see look Scott Hanselman that's me went and did something and I went and I added some files one time and that delete weird is gonna pretend that that didn't even really happen it's just it's fine we just put it back the way it was now let's switch to visual studio code I'm going to use Visual Studio code on the right rather than using Explorer because Visual Studio code will let me edit my files so I can see the folder here and I can make my changes at the same time and I can look at their perspective on what's happening with get this little icon here this little icon here says source control you'll see source control referred to as a family tree or a series of branches so that's what that icon means I'm gonna worry about this icon and this icon all right I want to also want to point out down here the name of my branch we'll go over that in a few minutes so you're seeing the changes on the left in our terminal and what's being reflected on the right hand side individual studio code are kind of lined up together so let's take a look at that let's make some changes Hey oops hey I forgot a line there we go cool so look on the right hand side Visual Studio code is saying M that means modified you made a change cool hit enter over here one file has been modified that makes sense here is a line in another file save cool look two files have been modified hit enter over here in my terminal in fact two files have been modified all right now let's say get add and I'm gonna say doc remember before we said code dot to me in the current directory or we said start dot to open up Explorer I'll say add dot and now I've staged I've told get that I modified two files and I'm going to now go and commit them get commit made a few changes added two lines all right cool two files changed that's great these are no longer modified over here nothing's going on here we'll come back to that in a second I'm gonna say git log cool all right so right now the head what we're doing here is we are pointing at the head and the head points to the most recent change here that says I think we can actually split this so we can see one file at the top and one file at the bottom so I'm gonna go and right-click and split down so here we go we've got tests at the top test two at the bottom I've got the files over here everything's visible on the screen at the same time I'll even make a little bit of room so you can see that in case you have my head in the way alright get blog we saw that okay what if I needed this this this hash is a unique identifier that is the the ID the social security number the unique number that represents this previous commit so I can go and check that out I could go back in time if I wanted to and I could get a specific thing I could say let's just take a chunk of that enough to be unique what if I said get check out okay and this is a little weird I just backed up in time look at this look on the right-hand side everything went back the way it was we just went back in time okay but when we did that we are now detached we can look around but we're really not in the right time we've gone back in time into an alternate universe okay so then I'm gonna go and say get checkout main and I'm gonna switch back so look at the right hand side watch those files I'm gonna do that again let's do that again so we're here we are on the current main this is the state of affairs on the right hand side I'm gonna say git log and I'll say look you know I want to go back to four minutes ago I have a unique ID that represents four minutes ago let's go back to then watch the right hand side I'm gonna hit enter and I want you to watch right here one they're gone they're changed we went back in time and we can mess around here if we wanted to but I want to go ahead and go back to main okay that's not usually a way that you want to work what you'd like to do is not just go back in time what you kind of want to do is split time in half you want to branch you know I'm working on this but I have an idea I'm gonna practice I'm gonna just step over here into this parallel universe and see if this is a good if this is a good universe for me to spend time in so I'm gonna say get checkout and what I want to do is I want to make a new branch of my tree okay so I'm not really checking something out as I'm making a new space there's a couple ways you can do it you can check out a new branch like this will say you know I'm just testing okay or I can say make a new branch call it testing and then I can check out over there but what we're gonna do right now is we're gonna say get checkout and we'll say I'm just testing all right so we are in a branch now called testing that's why it's so important to have a nice prompt you see how how friendly that is it's really hard to learn get if you can't visualize these things all right let me come back over here I want to point out that Visual Studio code has noticed that we're on testing I did a bad job with my drawing right there okay Visual Studio code has noticed that as well let me close this real quick I made a mess of myself there we go but we're in a world called testing and I can say git log and I could say that's cool you know the head well the head in testing and main are both the same so it doesn't really matter we're both looking at the same files so I can say you know all of this is wrong this is all wrong new testing here test that's wrong and let's say that this is also wrong I'm going to test something else here so I've made two major changes and you know I'm gonna even go so far as to say I want to remove test two because that was a bad idea so we'll say get remove or get our M test - oh that's interesting so it won't let me because I made changes to it well I'm gonna go ahead and say force this happens sometimes you want to be very careful if you say - F to get you're really saying I mean it I know what I'm doing so it's saying are you sure you want to delete that you made some changes I might now I know what I'm doing you better know what you're doing but I will say I do will pretend I know what I'm doing okay look that's deleted it's warning me it's not here anymore okay so let's be aware of that okay now let's go over here and let's look at our changes get status okay so in the world here in the parallel universe of testing we have removed a thing and we have changed a thing all right so I'm gonna say cool get add dot so I have changed a thing and I have deleted a thing get commit this is some testing I think it's better cool all right so in this world we have a file called test test - is gone I put my new testing in here it's all wrong and now we can say git log and this is interesting what's going on here well the head where we are right now this universe the present time is testing there's another thing from earlier that's on main that's where all my co-workers are doing their work and then there's the first thing I did so time went like this and then I broke time off and headed in this direction and I made a new parallel universe called testing now other people could continue in this direction and I might want to merge with their universe I want to merge with their universe I want to go and say get check out main now watch on the right watch here bunch of stuff is going to change what I hit enter one two three alright remember in this world and we'll say git log to prove it main is main I can't even see the testing exists test two is back it never died alright and I can see that expressed if I want to use other ways I can go and see tools like visual studio that allow you to do some of these things on their own where you can visualize changes but I like to go and see those things here but you notice if I go like this I say git log this is a graphical way or another way of seeing those changes you see on the right this log here this area is the same as this let's go back over to testing to prove that git checkout testing enter all right so we go over here we're gonna look at that log there's our history to see our history here we could say all branches you can see main and testing and how they relate to each other so there's lots of great tools that will let you see this pick the one that makes you happy and helps you understand what's going on but a lot of this is just time travel in different expressed ways alright okay cool so let's go back and say git checkout main there we go we're back here in this new world we've got test and test 2 we can close our get history here there's no changes here now we talked about how main was doing its thing somewhere around the center we went off here and we branched and we made a testing branch while main continued how do we bring this back to the main line how do I take that work that I was doing over in testing ok and I want to merge that brand in with the main line when I merged the branch in with with master or in my case with main okay so what are we gonna want to do here I'm here in Maine and I've got some changes elsewhere I got my changes over in testing so I'm gonna go and say git merge testing okay so I could go and do that but I might want to make sure that I'm clear about what's gonna happen and whether or not there's gonna be a problem there could be a conflict and I can work those conflicts out we can talk about those things or you can do what's called rebasing which is a little more advanced that we might want to cover in another future video if you find this useful let's go and say git merge and see what happens all right look at this we're gonna update I'm gonna fast-forward time I see that you made a couple of changes here and you made a couple of deletions here and you made three deletions there that file ended up disappearing okay so look now we're here on main let's go and take a look at that and our log has main and testing is the same thing so we took our changes from our little workspace our little area over there and we did a bunch of stuff what's cool about this is you could make a little branch get checkout be you know new feature wanna go and make a new feature feature see how we're on new feature and I'm going to take these tests this test here I'm just gonna be silly okay I'm gonna make a bunch of copies make a whole wacky world here with a bunch of new files so I've got like five files in here okay and you'll see that for four new files and I'm gonna say get add get commit - em a bunch of work okay now I'm gonna say get checkout main watch the right hand side watch right above me right here one they're gone I'm gonna say get checkout new feature they're back you could have huge amounts of work they're changing branches is a whole parallel universe with a mr. Spock has a beard and it's just chaos it's a whole other thing now when you're over there in the parallel universe making changes like test let's say that I said well this is all right not all wrong so I'm changing line one of test get add test get commit I got this right all right now we'll go back over to mainland to the world where it's still wrong and I'll go this is all kind of okay so two people are doing work ones in new feature in that parallel universe the other one happens to be on the same line in the same file okay get add oops get add dot get commit I think I got it got it alright so in my world I think I got it testing is another one if I go over to get check out new feature that's another one my branches are starting to get a little crazy what's going on there's a lot happening here if I want a view that or visualize that I can do that remember by looking at all branches see all the different things that are going on and you can see graphically in Visual Studio what's going on there so that's interesting but what's significant here is that two people have changed the same line on the same file so that'll be a conflict at some point get check out name get merge new feature conflict I couldn't merge this automatically automatic merge failed can you fix these conflicts and figure that out for me now there's tools that'll do this work for you but it's nice to know just like you might be using an uber and that's how you get around town or a bicycle it's nice to just kind of know how a car works it's nice to know how to drive a stick shift you may never need to but it's just nice little thing nice to know so look what it did here it made a file it took my test text file and it said well somebody said this is kind of okay somebody else said this is alright you see how they put these little left like it all points to the left and then it's when all points to the other direction which one is it is it all right or is it all kind of okay well what you could do is edit this let's make sure that we see what that that file looks like because I want to show you there's no magic here let's open it up in notepad just to make the point that's literally what's in the file is a bunch of characters and I could fix it and I could say that's that's what it's supposed to be right and then I commit that that's how I fix my conflict okay that's one thing I could do I'm not going to do that or in a tool like Visual Studio code it's like well I could except the one that just came in the incoming one from the new feature branch I could accept the current one the one I had before and say I'm in charge I could keep them both and work it out later or I could go and maybe start a session and share like my screen with somebody else and go and figure these things out when I compare the changes side-by-side I can see well one person said it's kind of okay and another person said it's kind of alright I need to decide that but I have to fix this conflict you can see that it will show up as a conflict down here this needs you to work it out so what are we gonna do okay let's accept the incoming change and we'll say it's alright I'm gonna hit save I'll go back over to here and I'm gonna say get status I don't say alright we brought these files in from new feature that's fine there was no issue there but we didn't we didn't mark this as a resolution we didn't say that our conflict is fixed so I'm gonna go and say get add cool get status all right now we've got a bunch of stuff I'm gonna say get commit brought in the new work from new feature all right cool and now I'm back on the main line new features out there doing its thing there's a parallel universe over there I haven't it's not the head main is the head testing we did 10 minutes ago that's chilling out there as well we can go in again visualize these things by looking at visual studio okay and then looking at it like this look at that look at this parallel universe came back together at the end there which is very very nice and then I can continue my work but again at any moment if I needed to go I could go back and I could cherry-pick I could say well you know oh I did some really good work I just want evil Spock with his goatee to bring him into our universe or I just want that one line there's all kinds of more advanced fun things that you can do but the root issue the fundamental thing that's interesting about get that I want you to understand is you can't lose your work if you pay attention you can almost always go back in time don't be afraid there's a lot of great tools out there that'll help you just commit often you notice how I committed often with a bunch of dumb silly messages I can squash those commits if I wanted to make one cool well phrased message in this case of course it was always me but if I had a team we could also push and pull from a central location like github maybe I'll do that in the next video so I hope this gives you a sense of get how I like to think about git and you find this useful this was all shot in in one no edit video but there you go get in about 33 minutes hopefully that was useful if it was please leave me a comment if it sucks go to another channel but hopefully you'll subscribe to this one thank you
Info
Channel: Scott Hanselman
Views: 160,811
Rating: 4.9396019 out of 5
Keywords: git, computers, learning, 101, source control, github
Id: WBg9mlpzEYU
Channel Id: undefined
Length: 33min 50sec (2030 seconds)
Published: Tue Jun 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.