How Version Control Works

Mark Wahlbeck
A free video tutorial from Mark Wahlbeck
CEO Devslopes | iOS | Android | Freelancing
4.3 instructor rating • 15 courses • 289,055 students

Learn more from the full course

Beginner Full Stack Web Development: HTML, CSS, React & Node

Learn web development with HTML, CSS, Bootstrap 4, ES6 React and Node

31:27:02 of on-demand video • Updated October 2020

  • Build websites with HTML & CSS
  • Build backend servers & APIs with Node and Express
  • Work with NoSQL databases like MongoDB
  • Build full-stack web apps with Facebook's React Framework
  • Build mobile-friendly websites with Bootstrap 4 & CSS
English [Auto] Havior one Mark Price dead slopes. Come and today we're going to talk about version control specifically about get. So let's get started. But I'm going to show you kind of how version control works. And I want to give you a tip from now on in here into the far future as your development career progresses. If you are not using version control on every single project you are doing it wrong. OK I don't want to ever see you creating a project that doesn't have version control in some form or fashion. OK that is the law. Put it in right now in your brain. Always always always use version control. OK. Not to be confused with birth control or things like that. This is actually really important stuff. What is version control. Well let's talk about code here. OK so you write code and it creates programs. So let's talk about a document. Let's say we've got this document here. OK. It's an ugly document. It's OK. This is your document. OK you've written some code on it and actually it doesn't you have to be code it could be anything can be a text talking or whatever but we're going to talk about code. So you've written some code here. OK. Now if you're only one person and if you're working by yourself that's fine. You keep making changes to this and no one cares right. You can just keep adding and changing it all that you want how ever what happens if maybe somewhere right here you broke something. OK. And then now your 5000 lines later OK 5000 lines later of code and you just realized you had a big bug. OK. Well now you have to dig through 5000 lines of new code to go back and find this code here. So this is a problem. You've just done a lot of work and now you're going to have to do a lot more work to find this bug and go back. OK. This is one problem. Version control can solve and I'll tell you how to solve it in a second. Here's the next problem. OK you've got a few people let's just say to people happy Sam here. OK. That's happy Sam. And this one is beady eyed puppy and he's never really happy. OK. So you've got two people here this is Pete this is Sam and they've got their own code files right. But they're working on the same project. OK. So let's say that Sam wants to make some changes to the codebase. OK so he he makes changes. OK. And then Pete makes his own changes. OK. Now here's the question this is the same project. They're building an app together. How does Sam get his changes over to Pete. He's made one two three changes and now he's mid-twentieth you change. Sounds like OK. Let's go ahead and load up Dropbox so you know Dropbox you maybe you've done this before. So Dropbox. And then he handed over to me. He's like I made some changes. So Pete now has a new code file. So he then opens up the code file. So he he opens up this code file here and then what he does is he grabs the changes he finds all the changes that were made and then he puts them here and his code. OK. And he mixes them in and he's like OK I put your changes in the code now. Now I need to give you the latest Fosset and it sends over the combination back to Sam. OK. Here's the new file with all of Sam and peace changes in it. And then that's going to go ahead and overwrite this. And then if you've got a third person here let's say the pointy nosed Sally joins the project on her B-D angry eyes she go up on the wrong side of the bed. So she now joins the project. OK so now you've got people transferring code K to all the different players on the team. Does that make sense. And we've got this big mess. Who has the latest version. Blah blah blah blah. That's that's how it used to be a long time ago a long time ago. Do you think that's effective. That's terrible. This is all bad Kate Plus it's you're manually taking these changes inserting them. Oh it's nasty. So this is not what we want to do. Problem one is saving your files if you have problems going back and be unable to revert and find those issues. And then you've got this whole issue here with multiple people on the same team thus version control was created. OK. And what version of control is the controlling of versions of documents. OK. So let's talk about scenario one with your code backup files if someone was used a version control. Let's go back to Scenario 1 so make a new person here. Keep it interesting. This is Jerry. And Jerry has code. And now let's say Jerry's using version control. So he makes change one day to his document and it's saved changed to is saved. And we're going to call these actual commits. That's the that's the phrase that you'll use. With version control especially in regards to get. So he's making comments right. So he's committing. So what he's doing is every time he's making a change he's now committing to version control and it's being committed Now let's say he comes to a version for and he finds a bug here it's like oh my gosh what's going on here. Well now what he can do is he can go check out. It's called checking out. You know like when you're checking out a girl. Whatever. Except in this case you're checking out code. Not really related. Anyway he's checking out where he wants to check out a previous commit because he wants to locate worst problem is what he can do is he can actually check out. Let's say right here number three and then he can run his code his code is restored back to that previous version and he can say OK the bug still exists here. So that is not it happened. And then he can check out another one another previous comment and he's like OK the bug is going on here. So then what he can do is he can analyze the differences between those two cochain does he doesn't have to go back to the five thousand lines of code to find or the problem is he can analyze it between the last two changes there where his problem was and let's say that it was a small problem. Well he may not want to fix it here. He can go back up here but he knows where it is now and then he can go fix that problem or he could just take it out of this altogether. And then you might have cascading problems down through here. But regardless the point is you can now easily solve this problem because he can check out previous versions of his code. Also this is important here besides just checking our previous code. This allows you to have multiple versions OK and multiple versions of your application. It's like saving backup right saving backup. That's what this is so this is safe. This is much better than the first scenario we're talking about. OK. Now let's talk about the other scenario where we have multiple people. OK. So this guy here. So happy. He loves the code. All right. And he's got his code here. And then let's go over here and this guy's name is Sam too. But this is sinister. Sam he plays online games and thinks is real cool but I have the handle games at his code. And then what you've got down here is you've got this guy. And this is Harry and Harry has his own CO-2. OK how do we solve this problem. You have any ideas where does version control come into play. Well let's take a look at this. So let's say Jim mix changes 1 2 and 3 and sinister Sam exchanges one two and three and let's say Harry. Well Harry sucks at coding so he only made one change and it wasn't really good. And anyway so you've got these three these three people here i'm just had known subsequences practice you get better over time and you're like thanks for de-motivating me. So they've all made their changes right. Remember how before we had to swap documents all that kind of stuff. Well now what we can do is Jim can now commit. OK so he's made these three commits these three commits these treatments. He can push them up to a server OK or some location whatever it doesn't matter it's it's arbitrary the locations arbitrary let's just call this the business call this the server for right now. OK. So what he can actually do is he can push these changes here. OK. And then what happened is sinister that he can actually pull down those changes. OK so he can recall this kush. So Jim can push sinister Samkon. Paul what happens is it's going to pull those changes and what it's going to do is it's going to automatically inject those changes where they need to go in Sam's code automatically he's going to inject them in there. And if there's no conflict meaning if no one edited the same line of code at the same time it's called a merge conflict we'll talk about that later on in the lessons. But if you automatically inject them right in there that's cool. And now that's Paul. And now with those things merge this is called a merge so centrist that sinister Sam has now merged. Those changes merged. Now what he can do is he can actually push the merge back to the surface of the servers. Now today with Jim's for stands coat and now Harry OK he can actually pull down all of their changes. So he's polling and it's going to inject them all in here boom boom boom boom. OK. And then once those are merged Harry can now push all the changes back up. And now the server has everyone's code changes. No one had to swap files and happened to automatically the pushing in the polling like I drew it out here. But really you could literally if off if everything worked out the way it was you could literally make the changes. Pull down someone else's change just merge them in and push them back and you could do that within a couple of minutes. OK. For less than two seconds even so we went from this massively crazy process of managing people's code to working automatically. Everyone's so happy even sooner Sam is starting to break out a smile. OK. Devious smile but because you put a bug in the code. But anyway. So version control very very powerful. OK. Never ever ever ever ever write any code any more on any project if you don't have control and of course the most popular version control system right now is called Get G-I. There are other systems such as mercurial perforce SVM things like that and some larger companies are still using those but get is blowing away the world right now it's fantastic. And that's what we're using in these lessons here and it's not that hard. Even a beginner can get up and running with it fairly quick. So that's version control that to get started to make sense started to dig your brain wrapper around the concept. In the end what I want to think about is just imagine having multiple versions of a file and a system that manages that for you so you never have to copy and paste again. And because the more you have to do copying and pasting the more error prone your code becomes and you never ever want any code. So that's it. Mark Price does slopes does come forward and remember to get your free life help. Go to Devis looks dot com and click on the chat room with thousands of students and they're helping each other out. Also click on the forums. This is where you can search for answers to your problems. You can also post your own issues. So head on over now.