Git & GitHub Crash Course For Beginners

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this crash course we're going to take a look at git now by no means is this a complete in-depth course where we're going to go over every command in every situation this is meant to be a no nonsense guide for beginners so either new or intermediate programmers or developers that either don't know what git is or have used it briefly but still don't really grasp the full concept alright so we're going to start out with a couple slides and then we'll jump in and we'll look at some of the most important commands for you to know all right so let's talk about what git is it's a version control system for tracking changes in computer files all right so it was initially created in 2005 by Linus Torvalds who was also the creator of Linux there's no specific language or framework or anything that you need to use git you can use it for static HTML web sites no js' apps Python Java c-sharp anything at all it just stores files so git is a distributed version control system or decentralized version control system and what that means is that many developers can work on a single project without having to be on the same network okay so there's also version control systems that developers actually need to be on the same network to use that's not what git is distributed or decentralized okay get coordinates work between multiple developers and attracts every single version and every single change that's made on the system or on the in the project you can also revert back to specific versions of any file at any time as long as it was committed to the repository usually when you use git you'll have a repository on your local machine that you work on you make changes and then you upload it to or you push it to a remote repository okay so something like github or bitbucket and then you can push you can push your local repository to the remote one you don't need an internet connection to work on a repository locally and you can make changes but if you want to push it the remote repository you will need an internet connection all right so let's quickly look at some of the concepts of git so it essentially keeps track of your code history with snapshots of your files alright so you make these snapshots by making a commitment amande and you can always go back to see any snapshot of previously written code and like I said you can always revert back if you need to so your code is extremely safe when you're working with git you can put your code in a staging area before actually doing the commit and writing to the snapshot this is done with a simple add command ok and I'll go over all the important commands once you make a commit to a remote repository other developers can then pull that information onto their machines ok so you can create you can also create branches but we'll get into that in a little bit all right I'm trying to just really stick to the basics here ok so let's look at the basic commands that you'll need to learn so get in it will initialize a git repository so you go into a folder via your command line or terminal and you do get an it and it will create a dot git folder in that in that project ok and it's hidden by default you almost never need to go into this folder at least I never have alright and then once you initialize that folder as a repository you can then start to run other commands all right so get add we'll add the file or files that you specify to the staging area or the index and they'll then be ready for commit you can run this command as many times as you need to before committing and it'll just keep the files in that staging area so if you want to see what you have in the staging area and ready for commit you can just use git status and that will display paths that have display the differences between the working tree and then the staging area or the index all right so when you're ready you can go ahead and commit your files so you run git commit and I'll take everything that's in the index of the state aging area and put it into the local repository there's also some important flags that you need to know are options that you need to know with get commit but I'll get into those a little later all right so the next three commands that I'm going to show you have to do with remote repositories so if you're working with github or bitbucket something like that okay so get push will take you a local repository that you've created and then push it to a remote repository such as github you will have to add for instance github or another remote service you'll have to add that and you have to add your credentials so it'll ask for your password and so on you can also create SSH keys with github so that you don't have to add passwords or anything like that so if you want to pull the latest changes from a remote repository you can just do get pull all right so if someone makes a change and you want the latest version you can use pulp use the poll come in and then get cloned will copy a remote repository into your current folder so if you find a program or a project or module that you like on github you can simply clone it and that'll download a TM machine so installing it is pretty easy no matter what system you're using if you're using Linux it's a simple package command for Debian use apt-get for fedora use yum and if you're on a Mac you can go to this URL right here this get SCM and you can download it I believe you can also use homebrewer or some package manager or something like that and then for Windows there's a few different programs you can use but I'd always suggest this get - fcm comm you can download the Installer and you can set it up really easily so this also comes with a tool called git bash which is a command line tool and it gives you a more Linux the environment on Windows I almost always use that tool over the standard Windows command line now that there is graphical tools that you can use where you don't need to use commands but I would strongly recommend then they use the commands because if you only use the GUI GUI you're not really learning anything you're not really learning how git works under the hood and if you end up somewhere where you don't have that tool you're going to be lost and it's not like it's really hard if it was if it was really difficult then maybe I'd say I'll use that but it's not hard at all alright at least for the basics it can get complicated to really really know how it works internally but for things that you're going to need it for most likely you're not going to need to know everything ok that's pretty simple all right so that's going to be it for the slides let's go ahead and jump in and I will show you how it works all right so we're going to go ahead and get started and install git now I'm in Windows if you're on Windows or Mac you want to go to get - SCM comm and if you're on a Mac you can click on this link right down here and that'll take you to the download page for our Mac for OSX but we're going to go ahead and click download on Windows and that's going to start the download and again if you're on Linux you can use your app get or your yum package manager whatever it is you use ok so let's go ahead and open this up and click yes so it's just a winged standard Windows installer we're just going to click through Program Files is where it'll be stored you choose the default for those now this option here I always choose the last one which is you get an optional UNIX tools from the Windows command prompt so it just gives us some extra commands to use so I always choose that option and then the rest of these line ending conversions I'll just leave the default terminal emulator we'll just use that default and we'll keep an able filesystem caching credential manager that's fine and that's that's good ok we don't want to use this experimental tool so click install and it will install it on your system okay so once that's done we're going to uncheck fully release notes and what's to watch get - okay click finish and then this is the git bash tool that they give you so I would definitely suggest using this over the standard Windows command line so now if we say get - - version you'll see the version you have yours may be different and then you can also use this from within the standard coming in line so if we go CMD and we open up the Windows command line and - get - - version it should also work okay so you can use git from either one now what I'm going to do is create a project so let's see on my desktop I'm going to create a new folder and just going to call this my app what we put in here doesn't really matter so when you have git bash you can also go ahead and right-click and do git bash here and that will open up a window there as well all right so I'll just use this one and you can hold ctrl and scroll and you can make this bigger or smaller which is nice okay so what we want to do first let's open this up as well kind of have both of these open at the same time okay so what I want to do is just create a couple files that that our project would have so let's say touch index dot HTML or you could use your file manager and let's create an app dot jas okay so we have these two files that this is our application and what we want to do is let's open up and open up atom which is the text editor that I'm using okay and we'll move this over here and I'm just going to add that folder as a project so desktop my app and let's close this stuff up and let's just open up index.html and we'll just put you know standard HTML tags in here put a title and body I'll just say this is my app okay we'll save that and now what I want to do is I want to initialize this folder as a git repository so to do that we just want to say get in it okay for initialize now once we do that it's going to create a dot git folder in your thoughtful in your directory now we can't see it here because I'm on a new Windows installation and I don't have hidden files enabled so what you want to do is if you're not seeing it here is go to view options and then change folder search options go to view and then show hidden files I'm also going to uncheck hide extensions for known file types because I want the extension to be shown all the time so let's say apply okay and now you'll see there's a dot git folder now this stuff in here don't worry about that at all that's just the gif files I don't even know what half this stuff means so we're not going to look into that there's no reason to so now we have this initialized as a git repository so we can start to use git commands now one thing you should do before you before you start anything is you want to add your name and email address to get so to do that use the config command so get config - - global and then we're going to do user dot name user dot name and then in quotes just put your name okay and then we're going to do the same thing for the email so we'll just change name right here to email and then just put in your email okay so now that that's set up let's go ahead and let's create our let's add the index.html file to our git repository so to do that we can say git add and then index dot HTML all right so it doesn't tell us anything when we do that but if we want to check to see what's in the staging area we can do git status it's the command we're going to use quite a bit and what this is telling us is that the index file has changes to be committed and that we've added it to the staging area and then app J s is a file that's untracked okay so it's telling us to add it so if you want to remove for instance index.html from the staging area you can do get RM so let's go ahead and do that so if we say get RM and we want to add the - - cached option and then index dot HTML okay and now if we do get status you'll see that both files are now on tracked now there's different ways you can add files for instance we could say get add and we could use a star or asterisk and say dot HTML and what that will do is it will add any any HTML files to the staging area so now if we say get status you'll see that it added the index file it also would have added any other HTML files that we had alright so I'm going to go ahead and take the index out again with the RM command and then I'm going to show you how to add everything ok so if you want to add every file you can say get get pad dot and now if we say get status you'll see that both the index and the app file are in the staging area ok so we haven't committed them yet and what I want to do is edit one of these files so we'll go over to index.html and I'm just going to put in an exclamation here and then save and then if we go and we say get status you can see we now have this changes notch Stage four commit index.html so this means that we have changed we've added some kind of change in the file while it was in the staging area so it's telling us to go ahead and add it so get add all again and then get status and now you can see that it's back in the staging area all right so now we're going to do a commit so for that we say git commit now there's an option that I would always suggest you use but I'm going to show you without it first so if we say git commit it's going to go ahead and open up this vim editor which can be a little confusing because if you start to type it's not going to do anything what you need to do is click I to go into insert mode and now I can type okay so I'm just going to say initial commit actually I'll just go uncomment it down here these some number signs are basically comments okay so anything that has the number sign in front of it is treated as a comment so I'll just going to remove it from this initial commit and that gives us a comment of initial commit okay now to get out of this we want to do escape which takes us out of insert mode and then we want to type in : WQ and enter and now you'll see that those files have been committed and it tells us how many files have changed and gives us the file names okay so that's how you do a commit so if we were now to say get status it says nothing to commit because we've already committed all of our changes if we were to go edit a file now and do get status it'll show us again which files are in staging and which files are unchanged all right so let's go ahead and edit the app.js file I'm just going to do console dot log and we'll just say hello save it so now we do lots of slow slow machine get status so that's been modified app J s now I'm going to go ahead and add it with get add all alright now I want to do a commit but I want to show you how we can skip that whole edit stage so we can say get commit and then add dash M and then do a comment here so we'll say changed changed fjs and enter okay so now it's made that commit and it's skipped that whole editing stage we just included the comment within that command okay using dash M all right so now what I want to do when we clear this out I want to show you how to use git ignore so this is a file that we can include the files or folders that we don't want to include in our repository at all so even if we do get add dot where it adds everything it's not going to add that so let's create it we'll say touch dot to get ignore now if you try to create this file in Windows in the file manager you probably won't be able to because it starts with a dot so it's going to look at it as an empty name so either do our touch touch command or you can create it inside of an editor I believe all right so what we want to do is open up get ignore and we're also going to create a file that we wouldn't want to include so let's say let's say touch and create a log dot text usually you don't want to include log files so let's add something to this we'll just say there are logs and save that and what's going to get ignore and we'll say log dot txt and save that's all you have to do is add the file name now if we go over here and we say get add all and then get status you can see the only file that's in the staging that's change does get ignored normally log text would be there because we just changed it but since we added it to get ignore it's not going to get added okay you can also add entire directories so let's say we have will say der one man this is slow and let's do jar - and inside these I'm just going to create let's say new file and app 1jf let's see inside there - we'll create a file called app 2jf and we'll just add something to those okay we just put that I'll copy it and we'll also put that in app one okay so if we go to get ignore and let's say that we don't want dirt to included at all so we could just put in slash dirt - okay and save that and let's go over here and go get add all and then get the status and notice that only there one is in there even though there are two has a file that we change it's not in there because we added the whole folder to get ignore all right you could even add you could say star dot txt and it'll it'll withdraw all text files okay and there's other expressions you can do as well I'm not going to go through all of them it's all in the documentation but just know that if you don't want something included you want to use git ignore or dot get a car alright so let's close these out and the last thing I want to look at before we look into remote repositories is branches all right so let's say that you're a developer working on a project with a bunch of people and your assign the task of adding a login okay so you don't want to start making changes and you know push to the repository and have the main code based codebase edit edited and changed without finishing the praat are the functionality okay so what you would do is you could create a branch call login or whatever and then work in that branch and you can still commit it and all that but it's not going to be committed to the main branch okay which is called master you can see right here when we say get status it tells us we're on the master branch now to create a branch we can say get branch and let's just call this my branch I actually will just call it login all right we'll pretend we're adding some login functionality all right now just doing that doesn't change us to the branch if I say get status it still tells us that I didn't commit these changes so let me just commit these before we go ahead and switch branches so we'll say git commit dash M and I'll just say another change okay so you'll see that got committed to master because we're not in that Brett de log in branch so to switch we can say git checkout and then the name of the branch okay so now we're in the log in branch so I'm going to clear that out and then I'm going to say get a not get let's go ahead and create a new file while we're in this branch called log in dot html' okay so over here let's going to log in and we'll just say whatever doesn't matter log in and save and it's also edit index.html so I'm just going to add down here I'm just going to say log in form and save okay and then let's say get add all and then let's commit it commit - M and we'll say log in form okay so now let's go ahead and switch back now when I do the switch watch the log in HTML so get checkout master and now the log in HTML file is gone and if we look at index.html that part of it that we edited is also gone so the reason for that is because that is that's in the login branch now if we want to merge that if we finish the functionality and we're ready to merge then we can say while we're in the master get merge git merge login all right so it's going to open this editor up so let's do I and let's see please enter a commit message to explain why this merge is necessary so we'll say added login and then we'll do escape : WQ enter okay so now even though we're in the master branch we can now see the log in HTML okay now there's a lot more to branching and merging if you want to get more complicated but this I want to keep this simple for beginners this is a good chance if you're working on your own projects that you won't even use branches alright so let's go ahead and now work with a remote repository so I'm going to go to github alright so I'm going to go ahead and just sign in if you don't have an account just create one it's obviously free okay so what we want to do is go up to create new new repository and I'm just going to call this my Apps example you can call it whatever you like put a description say sample app for tutorial okay so you have the option of public or private so public anyone can see it you can choose who can commit private you can choose who sees it and commits and I believe private cost money I'm not sure I've never actually tried to create a private repository on my account and you can also choose to initialize it with a me file if you want I'm just going to leave that unchecked you can also choose to add a get ignore which we already have or a license so let's go ahead and Creek clicks create repository alright and then it will take you to that page which has nothing in it so it's just giving us this message here and it tells you exactly what to do okay so you want to initialize your directory which we've already done it's telling us to add a readme but you don't have to but you probably should especially if it's a public repository because you want to add some information on your application and you want to use dot MD two for it to be able to display nicely on on the page K and MD is marked down now telling us how to make a commit but we don't actually have to do this stuff what we want to do is add this repository as a remote repository if we go over now we say get remote that will list the remote repositories which we have none so let's go and copy this and we'll paste that in here run it and then let's say get remote and it'll say Origin so down here what we want to do is get push and then - you origin/master so let's go ahead and do that just copy this okay and we'll paste that in and that should push it to that repository oh yeah okay so we do have to login to github it has to validate your credentials gender sitcom and password okay so now it's going it's going to push to the repository to the master branch and let's reload this and I'll see we have our files here all right and then down here it says help people interested in this repository understand your project by creating a readme so let's go ahead and do that we'll say touch readme dot MD alright and we can open that up which I click the wrong editor it's going to open up vs code okay so let's open up the readme and I'm not going to go through the syntax of markdown it's pretty easy if you want to just look at the documentation but we're going to put a heading by using a number sign here and we'll just say my app this is my app and we'll save it let's go ahead and add it and then we'll commit say commit and we want to add - M comment will say added readme okay so right now it's on our local repository we haven't actually pushed it yet if I reload you'll see it's not there so now that we've added that as a remote repository all we have to do is today get push and it'll push it right to github okay so now if I reload you'll see we have the readme file and it's actually displayed here now if someone wanted to get this they could just clone it okay if we go to clone or download right here they could choose to download it as a zip or they could clone it using this going to go ahead and copy that link and then let's let's create another folder and I'll just call it my app - and let's open up get - here and then what we want to do is say git clone and we'll just paste that link in and it's going to pull everything in now I actually okay so what it does is it actually creates the the folder for you my app sample so it pulls the entire folder in but if we open that up you can see we have the entire application here okay and you can clone anybody's anybody's project as long as it's public now if you had multiple developers working on it and someone else made a change and we want to go over here back to our original folder you could just do get pull and that would pull everything down you can see everything is up to date okay I think that's as far as I want to go into this so we covered all the basics all the basic commands you need to start using git I would suggest if you're not using it at least now start to create your repositories for your project create a github account if you don't have one create the repositories on github and then push from your local machine - to the github repository alright and you don't even have to use branches and stuff you know you're just starting out just getting used to it all you really need to do is add in commits and push okay so hopefully this just helped some of you guys I'm sure some of you already know all this stuff and hopefully you just used it as a refresher or whatever alright so that's it if you enjoyed it please subscribe leave like leave a comment and I'll see you next time
Info
Channel: Traversy Media
Views: 2,491,206
Rating: 4.9443855 out of 5
Keywords: git, github, git tutorial, github tutorial
Id: SWYqp7iY_Tc
Channel Id: undefined
Length: 32min 42sec (1962 seconds)
Published: Sun Feb 05 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.