Git Tutorial for Beginners - Git & GitHub Fundamentals In Depth

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello everybody and welcome back so in today's video what I'm gonna be doing is teaching github fundamentals so essentially everything you need to know to be effectively using github and all of the common commands that you'll use 99% of the time I will note that getting github are extremely complicated tools when you get to the advanced level there's a lot of things that you can do and there is like thousands of commands and variations of commands that you can use within git and github of course I'm not gonna be showing you all those these are kind of things if you need to use them you're gonna look up and use them once in a blue moon the stuff I'm gonna be showing you here is what I can almost guarantee you you'll use literally 99% of the time so with that being said what I'm gonna do in this video is really focused on actually trying to teach you how get and github work I don't want you to just memorize the commands I want you to actually understand what those commands are doing so that if something goes wrong or you need to figure out what command to use you actually have some idea of what's happening on a lower level so with that being said I'm gonna hop over to the kind of blackboard here where I'm just gonna do some drawings and illustrations talk about first the difference between git and github then we'll talk about the workflow of github we'll talk about branches and then we'll actually get into all of the different commands and that will probably be near the second half of this video so I'm here on the blackboard and the first thing that I want to discuss is the difference between git and github so a lot of people seem to think the getting github are one in the same that they're the exact same thing that is not true these are actually different things they obviously integrate very well with each other and they're very similar and what they actually do but they are two separate things and I think it's important to understand the difference and github is really just a website I like to think of it as kind of like a nice add-on or extension to get and what github does is it gives us a nice UI and gives us some features that git doesn't have and it lets us host remote repositories and work collaboratively with a lot of other people now what git does is this is actually what handles all the version control stuff so when you're doing something like merging code together or when you're doing something like making a commit or making a change or pulling or push you're using get get is the version control version control software this is what we're really gonna be learning how git works github is kind of just like a layer on top of git that gives us a bunch of commands it gives us a bunch of UI features it has a fancy website and it has a bunch of other things that get just doesn't have so these are two separate things please do not confuse the fact that they are the same and I want to make it clear that we can use git without even having to touch github because github is not required it's not a dependency of get it is just something that we use on top of git and there's lots of other tools that go on top of git as well so with that being said let's actually get now into kind of the workflow when we use a github repository and let's actually discuss what I mean when I say the word repository so let's go here so to kind of explain what I mean by repository and so through the differences of what we call a local and a remote repository which i think is the first important thing to understand I want to consider an example where we have a team of four developers let's say we have four people and they're working on some kind of game well what these people probably want to do is they're all gonna have their own machines they're all gonna be coding and they want to kind of sync up with each other right when one person finishes a feature or they make some changes they want to send it to the other person when the other person has some changes they want to send it to them and they want some kind of system that keeps track of those changes and that means that you know everything goes fine so essentially we're not sending zip folders to each other we actually have some kind of automated system like yet that is controlling this version control stuff for us so let's say we have our four developers what I'm gonna do is I'm gonna draw four boxes down here in the corner of my screen and these will be my developers now here we're gonna draw a big circle and this is what I'm gonna call my remote repository so a repository is really just a store it is a place where we have a bunch of code and it can be a remote or it can be local a local repository is something that sits on your machine so it sits on your end in front of you other people don't see it it's your version of the code that is a local repository so each of these four developers have their own local repository on their machine so let's say we started out in development and just started there's a little bit of code that already exists the first thing that all these developers would do is they'd look at the remote repository which is going to be kind of our source of truth this is going to be the place that really has all of the up-to-date code this is where all of the changes go to this is where it starts from this is where we have the most recent version the version that's working that's what we think of when we think of a remote repository this is the place that all four of these developers kind of trust that has the most up-to-date version and they'll be making changes to this place and they'll be asking for new changes and that is what the remote repository does so essentially these four developers have four local repositories on their computer they are some kind of version based off of this remote repository they'll make changes on their local version and then they will push those changes to the remote repository and when someone else makes some changes they will pull those changes down so this is the basic workflow right we have this remote repository when we start out all of these developers kind of take a copy of this repository so they'll say okay let's copy it onto our local machine so let's say at the very beginning at time zero all of these developers have the same copy of this remote repository now let's say you know developer one starts working on a new feature well his code base immediately locally is gonna look different than developer two and it's gonna look different than the remote repository right that's just how it works so let's say developer 1 makes some changes on his local machine he test them out they look good and what he does is he says okay let me push these changes to the remote repository I want my other teammates to see them I want to check it in I want to make sure everything's good so he pushes those changes to the remote repository now the next day he says to developer - hey I made some changes pull those down from the remote repository what he does is he asks the remote repository hey give me any new changes and then it compares the remote repository with his local repository and changes anything that changed so it gives him all of these new changes maybe a new file was added maybe some files change lines whatever that's what get does for us it pretty much handles the interaction between changes and making changes right like that's all its really doing it's when changes happen handles that for us and the nice thing with git is that since it is a very complex tool it can do things like automatically merge code together like let's say you and I have worked on the same file but we changed different parts of those files well git can actually figure out what's happened in those two files and then automatically merge those changes together such that there's no conflict now sometimes there is but that's beside the point this is kind of the basic workflow of git and that's what I wanted to cover here all right so the next thing I want to talk about is get history and branches now this is really important and if you can understand this part here you're pretty much gonna be good to understand like 99% of github or the stuff that you're actually gonna have to use all github really does for us is track a history of what we call commits now a commit is simply a change that you've made now that's not necessarily like you change one line of code I wouldn't call that a commits what a commit is is essentially you make a bunch of changes on your local machine and then you commit those changes so you say hey I want to save all these changes I want to bundle them up into their own little package and I want to store the fact that I changed all of this stuff at this point in time that's what a commit is essentially like a checkpoint a way to return back to a previous point if something goes wrong so not every single time we save the file are you making a commit you have to actually manually make a commit so let's say I add on like a new feature or I have a bug fix I would commit that so that that way say I messed something up in the future in the codebase I can always come back to that commit and say hey at this point in time this was working this was functioning that's the point of a commit and it stores a bunch of data with it who made the commit what time the commit happened at who changed what files you know when they were changed within each file it stores a ton of different information and that information is really valuable when we start talking about branches and kind of a timeline in github so we have all of these commits and commits are just changes what you do is on your local machine so in your local repository here you know you make a bunch of changes you do whatever you're doing and then you commit those changes locally so inside your local version of the repository you commit those Changez once you've made all of your commits so you can make multiple commits it doesn't make as many as you want you know just checkpoints you're just saving whenever you want to essentially you will take those commits and you will push them up to the remote repository so this is your local repository and this would be the remote repository now the remote repository will have a look at all of these commits and it will attempt to bring all of those commits and push them into the branch of the remote repository so I haven't talked about branches yet so I'm trying not to say that word but essentially just imagine it takes these commits and it somehow kind of merges them with the existing code base and adds them into that that's what happened it happens when you push commits up now let's say that you're on a local repository and you know it's been a few days since you've updated your repository the remote repository has changed people have made some changes well what do you need to do you need to get those changes so what you do is you do what's known as a pull so you pull all of the changes down from the remote repository so let's just say the remotes up here pretend this one's gone you pull all these down and then what it does is it gives you all of the new commits that have happened since you last pulled down these changes now you have all these commits coming from the remote repository and you merge them into your local repository so you take them and you combine them with the work you currently have and that is kind of how this system works with a bunch of commits now what github does for us is it tracks all of these commits so let me attempt to erase all this let's get rid of that as well so in github we have these things called branches and the first branch I want to discuss is what's known as the master branch now we have all these commits these are all our checkpoints all our kind of saves and we push commits up and we pull commits down now the remote repository and even your local repository as well keeps track of all of these commits and everything that happens to the repository so if at any point in time something goes wrong I can always roll back to a previous commit or I can just look at all of the history and what's happened in this code base which is very valuable so all of these dots that I'm gonna put here are commits and imagine this being a time line we're back here is in the past previous commits and right here so at the very end of the time line is what the current code base looks like and it's the most recent commit so what stores all of these commits kind of on this timeline maybe they all happen from different people maybe they're all the same person whatever there's all these commits these are all the changes that happen now this is on what we call the master branch so the main branch of our repository is really just what we think of when we think of the code base we have a branch and a branch is just one version of this codebase essentially and it's hard to describe until I show you what another branch looks like but just imagine that the core code base that you're looking at is pretty much always going to be the master branch now this master branch is what we want to make sure is always working think of if we're working on a large project I don't want to modify this master branch with some code that's broken or not working right because if another developer pulls it down now all of a sudden their local repositories messed up because you've made a change to the master branch they pull down the master branch and now they're all you know they're all messed up on their end because you made a bad change the master branch so instead of always making changes to the master branch so instead of always putting these bubbles so these commits right on to here sometimes what we use is what's known as another branch and what a branch essentially is is a copy of a repository at a certain point in time that has different changes so let's do this so another branch could look something like this so maybe this branch I'm gonna call feature are like a new feature so when you ever you have a new branch you give it a name you can call it whatever you want and the point of this branch is to essentially be able to work on a new feature push it up to the remote repository without actually directly putting it into the master branch so if I make a new branch maybe I branched off starting at this point right here so I branched off at this point in time at this commit now this branch has whatever the repository had up until this point right so everything behind it it has all of those changes and you've branched off and now these dots here are your changes on this branch so you are just making different changes on this branch so that you are not affecting the master branch until you're sure that this branch is fully functioning because imagine say you and for other developers are working on a new feature you're not done yet you haven't finished up you need to work on it together well you might make a new branch off the master branch so that you don't affect the master branch of code the stuff that everyone else sees and then as soon as this branch you're satisfied that it's actually working that everything is good what you do is you merge it with the master branch so you take all of these commits here and you say okay I want to merge those into the master branch and then get does its magic which I won't really discuss and it takes all of these commit messages and aligns them onto this master branch in the order that they should go and makes all of the appropriate changes that is essentially what happens when you have what's known as a branch and again you branch off because what you want to do is make sure that you're not modifying the master branch until you're sure what you're doing is working and functioning and of course on this branch here on this feature branch you can still update this branch from the master branch what you can do is you can say hey give me all the most recent updates from the master branch throw them on here so that this one is staying up to date with the master branch but it has some new changes that aren't yet in the master branch that is what another branch is kind of meant to do and you can even branch off from branches so when you get into really complicated projects maybe you have like another feature you're working on on top of a feature well you could branch off from another branch merge it on to this branch so merge like that and then merge all of this on to the master branch I know okay so this was a long explanation of branches of git of github but I think we've gone through a lot of theory most of the theory we kind of need to understand and now it's gonna be really easy to get into the commands and start actually doing some examples so with that being said let's head over now to come to the terminal and actually github and let's set up a repository let's go through some examples and let's learn what these different commands do all right so I'm on github here and I actually just wanted to show you an example of a large repository and what it kind of looks like in terms of branches and commits before we get into the commands so this is actually the repository I work on at work so I work at Microsoft this is an open source repository this is the extension for vs code so python four vs code and I just wanted to show you that if you're looking on github this is github the website not yet you can see that right here this repository has 4,500 commits and 22 branches so if I click on commits I gotta accept this let's let's see if this go through here okay so I'm in commits now and you can see that this is what I'm talking about I say branch master and here you can see all of the commits that's that have happened that have been pushed into this branch so it shows you on what day all of these commits happened and then you can click on a commit let me just click on one here and let's just have a look at what this is you can see the things that have changed in all of the different files and what actually happened in this commit so that's why it's really valuable to have these different branches and to have these commits because you can look back at the history of anything that's happened in their repository now if you go to branches you can see there's a bunch of branches here that people have been working on and these are branches that are off of the master branch right so let me click on my branches and you can see that I haven't pushed any branches of the repository but if I go to maybe active branches let's see how many active branches we have we have all these branches that are currently active that people are working on so that is what I mean by branches and commits and now I want to get us started with our first github repository so the first thing that we need to do is actually download and install the tool called git now this is gonna be the same on Mac and Windows all of the commands that I show you but you need to go and download install git I'm gonna assume most of you have this so I won't go through the steps but I will leave a link in the description that has the link to download and install git once you have that what we're gonna do is we're gonna start by making a local repository on our machine and experiment with how all of those things work and then we'll talk about remote repositories so let me actually change directories to my desktop if I could make it there so I'm on my command line CD stands for change directory this is a common tool you're gonna see me use some command line tools here I'll explain what they do but these should be the same on Mac and Windows and Linux as well so don't worry about that but get into your terminal so open a terminal open a command prompt and then go to your desktop or go to some location where you're okay to make a new folder and m'kay dirt and the name of your folder which in this case I'm gonna call test underscore repo so this is gonna be my test repository this is what I want to just kind of experiment with so I'm gonna make a new folder on my desktop that's what mkdir stands for make directory called test repo next I'm gonna see B into my test repo like that and now what I'm going to do is initialize a new git repository so there's many different ways to kind of start up and you get repository the most common way if you're working on a brand new project and you're starting it on your local machine is that type gets an it so let's say that you're about to start working on something you know you haven't worked on it yet and you're just booting up the repository it doesn't already exist what you do is you type get an it and that will make whatever folder you're inside of a github repository so keep in mind you can do this anywhere but as soon as you initialize a folder as a github repository anything inside of that now is going to be tracked by git and what that means is that you can add files you can use all your git commands within this folder but only if you've initialized a git repository so here let me just type git commit and you'll see that um this works everything's fine but if I actually CD back so I CD back to my desktop and I type the same thing so let's go get commit that it says fatal not a git repository so just make sure that you're actually in a git repository before you start doing all of these git commands okay so I've made a git repository and what I'm gonna do now is I'm gonna open this up in subline text so I'm going to file open folder test repos right there awesome and now I'm just gonna make a new fold make a new file server so file new file let's just name this readme MD and let's save that inside of test repo and let's just make a this is a markdown file let me just do pound hi so that's just like a header I've just made in a markdown file great so I have that now and I've made a change to this repository so let's imagine you know I added my code I'm in the repository I've made a new change the first thing I need to do if I want to commit or kind of like store this change so I want to make that checkpoint is they need to add all of the files that I want to actually commit so this is where we talk about something called a staging area so inside of gets what we have is a staging area this is where we're temporarily adding or removing files that we want to be in our next commit so let's say I added that final write and I believe I called that test or something what I would do if I want to add this to my next commit I'm not committing yet but I want to add it in and it's a temporary area called the staging area as I type get add and then the name of the file which in this case is readme MD so let's do that we see that nothing goes wrong everything happens fine and now if we want to check what we actually just did we type git status now you can see that it says we're on the master branch which is by default just our core branch we don't need to make that or anything it says no commits yet and it says changes to be committed it says we made a new file called readme MD so that is because I added this readme file to the staging area that I typed get status to look at what was happening in the staging area okay so I had a small error there but anyways do we now have this file inside of the staging area so we've added this in and now what this means is that it is ready to be committed so I can go ahead and add more files into here in the staging area and when I'm ready when I've made the changes that I've want I can go ahead and commit now usually you commit once you've made some kind of progress once you have something that's kind of working or maybe you're like in the middle of something and you want to store it at that point that's when you make a commit you're not really doing it like every 5-10 minutes you might do it like every few files you change or every like you know kind of milestone you hit when you're programming if you want to put it that way but anyways once we have that staged what you can do is type git commit - M which stands for message and then inside of quotation marks you put what's known as the commit message now the commit message to be something that's under fifty characters long and accurately describes what you changed in this commit so in this case what I did was I added readme dot MD that's what I did in this commit I made it a new I made a new breed me file so I'm gonna type git commit - M added readme MD now look what happens it says master root commit this is actually the commit ID it says Rini thought md1 file changed one insertions and create mode something readme MD so now we have just committed that file and we've made a change now if I actually type git status to look at our staging area we see on branch master nothing to commit working tree is clean and that is as easy as it is to make a change to a git repository you add your files you make a commit and there you go you've now stored that and you've saved that commit now what we can do is look at some other ways to add files and to make commits so let me go here now and once actually make a change to this file so let me just add another one that says hello let me save that file it's important that I save it and let's go back here and now let's type git status and now when I type git status we can see changes not staged for commit so this is telling me what I've made the changes that I made that have not yet been staged that I haven't added and it says okay we modified readme MD and then it says okay use git add file to update what will be committed use git checkout to discard changes in the working directory so we don't want to do that what we actually want to do is we want you add this file we want to stage it and we want to make another commit so what I can do is I can type git add in the name of that file or what I can do is actually type git add period and what this stands for is add all files so add everything that I've in the repository that's what this stands for just add it to the commit so when you do dot that literally means just add everything so I'm gonna do get add dot that's gonna automatically add this file now i can type git status and i can see that it says boom this is green we've put this it's changes ready to be committed and now we can go ahead and do another commit so get commit - a.m. and we'll say sorry not an as she's gonna be m made changes you know this is not very descriptive usually you want to be more descriptive but that's okay we're just gonna say made changes and now boom we have our new commit made changes one file change three insertions one deletion awesome next thing I'm going to show you is branches so let's say that at this point in time what I want to do is actually want to make a new branch and I want to start working on something and not modifying my master branch keep in mind all of this is happening locally so I have my master branch of code we're just going to imagine that this is for one user just ourself because this is a local repository and I really don't want to mess up the master branch I don't want to make any changes on there I want to make them on another branch and then maybe later on I'll merge that with the master branch well what I can do is make a new branch so I can say git checkout - B I believe is actually the command and then the name of the new branch that I actually want to check out - so git checkout is how you changed between branches when you do - B I believe that stands for make a new branch and now you can put the name of your branch so what I'm gonna do here is just call this branch new so get checkout - beet knew what this did now is to switch to a new branch new so now what I've done is I've gone from my master branch and I've branched off so I've copied everything in my master branch onto this branch new and now any changes I make on my new branch are not gonna happen on the master branch so let's actually split these screens and let's look at this so let's go here and say new branch insider readme MD and let's now on our new branch new make a new commit so let's say get aa dot let's add those changes let's say git commit - M and let's make this first commit on new branch boom let's make that commit and now let's change back to the master branch and see if the master branch changed because remember I just made these changes on the new branch so to change branches what you do is you type git checkout and then the name of the branch so if I want to go to the master branch I type master since I'm already on the new branch I can't change to it right but let's do this get checkout master and now look at this did you see the file it automatically removed that change because I had made it on the other branch so now we have two branches one branch which is a head of the master branch because it has new changes and the master branch which has our original changes right so let's actually add a new file here let's just do this test dot py and let's just say like imports hi game or something like that okay now let's make a commit so let's go get Ad dot so let's add all these files let's type git status we have our new file test stop hi let's do git commit - M added test dot PI boom inserted that awesome now let's change over to our new branch so let's say git checkouts new now I'm on this new branch and give us a second you notice that inside of this folder so let's get rid of that we have the change that we made previously on that branch and we don't have the new file that was on the master branch so let's say that what I actually want to do is I want to update this branch with what's on the master branch so a change happened on the master branch which is a new file was created and I actually want to get that change and bring that to my new branch how do I actually go ahead and do that well when we're working in this kind of system what I can do is actually merge the master branch with my branch so I can actually type git merge and then type master and what this will do is take all of the changes from the master branch and merge them with my branch so let's do this git merge master merge made by the recursive strategy and now look we're on our new branch here and we have this test file we have our readme file that still has this change here and now we're looking pretty similar to the master branch except we have this line of code now let's change branches again let's say git checkout master and watch you're gonna see that this new branch on line five here will disappear and there you go it's gone so we have these two files now but that line is gone this is kind of the power of git and that is how the two branches work you check out make a new one with the - be in the git checkout command and then you can make commits you can change and you can merge these things together now that hopefully you have a basic understanding of some git commands let's look at setting up a remote repository and get all of this working now in the cloud rather than just on our local machine so what I'm gonna do is go over to github and I'm gonna make a new repository so you need to make an account signing on github I'm going to assume you guys can figure that out now I'm gonna go to the plus sign and I'm gonna click new repository so let's do this and what I'm just trying to set up here is a remote repository something that other people can look at that's not local on my machine I'm gonna call this one a test repository I'm gonna call this test underscore because test is already taken I'm gonna make this a public repository I'm not gonna give any description and I'm not going to initialize this with a readme file you can if you want but for this step I would recommend you don't because I'm gonna show you how we can link our local repository with this remote repository and push what we currently have up so let's go ahead and click create new repository let's give it a chance here and here it goes it says quick setup if you've done this kind of thing before we haven't so what we're actually gonna do is follow the commands that are right here essentially what this is saying is we made a new repository but we haven't initialized it yet so we haven't actually set anything up what we need to do is we need to actually push some changes to this repository first to have it actually be set up remotely so what it's saying to do here is essentially what we've already done it says make a new repository on our machine add a new file make a commit and then we need to follow these two steps so what I'm gonna do is copy this line of code you guys are need to gonna need to copy this as well so git remote add origin and then this line right here so make sure you copy this and at least make sure you have this URL to your remote repository because you're gonna need that so let's go to our command line now let's actually put this on the side and let's type in that line by just right-clicking so if you copy something and you're in terminal you can right click on your mouse and it should paste it in and then go ahead and hit enter what this has just done is added what's known as a remote now a remote is essentially a URL to another repository so what we've done is we've said hey I want to add this remote repository as kind of a link as something that we know of here in our local repository so that we can actually push changes to it and pull changes from so that's what we've done when we say git remote add origin I said I'm adding a new remote that's called origin that has this URL so now when we use the word origin we'll be referencing this URL that's all we're doing we're literally mapping the word origin to this URL when we type git remote app you can add as many remotes as you want some people will commonly call this also like upstream so you can have an Origin which is typically your remote repository because you can work on more than one remote repository and then maybe like the company's remote repository will be your upstream repository or something like that anyways so we have that let me just clear the screen here and now I'm going to show you how we push changes to this repository so it shows us here that the command is get push - you origin master so the first thing I want to do is make sure that I'm on the right branch so I want to check out usually you wouldn't have more branches before you do this but I want to check out to the master branch so get check out master see we're already on the master branch but it's important we're there and then what we're gonna type is git push - you forage in master what this is saying is let's push all of our commits to the origin remote repository and let's push them to the branch master so we're picking where and what branch we want to push these two because we can push to another branch as well which I'll show you in a second - you says save these settings what that means is that next time we do this all we have to type is git push and it will automatically remember that it was origin and master that we pushed you so let's hit enter and let's see what happens here enumerated objects counting objects and boom there we go branch master set up on track remote branch master from origin so now if I go ahead and refresh this here let's have a look and says readme dot MD test up high and you can see that we look at a readme and we have these changes now on github so if you had any problems pushing there I probably know why I think I approach this at the beginning of the video but I kind of forgot so I'm gonna bring it in here what we need to do before we can actually work with the remote repository and the reason it worked for me is because I've already done this is set up a username and an email so don't worry if that push didn't work just follow along with what I'm about to show you this may not work either this gets a little bit tricky because you do need to authenticate yourself locally on your machine before you can just push and pull from any remote repository right so what I'm gonna do here is I'm gonna start by saying git config - - global user dot name and let me make this fullscreen actually so you guys can't see it and what I'm gonna do is put in quotation marks the name that I want to be represented by in this remote repository so on your local repository it doesn't matter but on the remote repository other people will see this potentially so you want to have some name that identifies you so I'm gonna put Tim as my user dot name so there we go now I've set up my global name so now this is stored on github locally and anytime I make a change to a remote repository I push or I pull or whatever I'm gonna be identified by the name of Tim now the next thing I want to do is use email so I want to say git config - - global user dot email and in here I'm gonna put the email that I use on github now this should hopefully authenticate you you might not you might have to actually use I might have to it might ask you to sign into github there's a bunch of different things that could happen here I don't remember how you authenticate locally to be able to push to be honest so you might have to look up how to do that there is something called a personal access token which you can use as well if you're on Windows at least but anyways let's put in our email so I am just gonna put a random email because I'm already authenticated so this is fine and I'm gonna put Tim at gmail.com actually I'm not gonna save that cuz I have another email that I don't want to share with you but anyways just put your email and hit enter and once you've done that you will have configured your name and your email on git so now you can try to push again and see if that works it may ask you for your password for github it might ask you to login if that doesn't happen unfortunately you're probably gonna have to go to the internet and figure out how to authenticate but let's say that now you guys are authenticated and you were able to push to this remote repository if you were able to do that what we need to do is move on to the next step which is understanding how to pull from the remote repository so I'm in my repository now I've pushed my changes up and I'm actually gonna make a change on this remote repository so let's go here and let me actually go to the edit button and I'm just gonna make a change that's hopefully gonna simulate what someone else might change on a remote repository so let's just move it so that the readme only has hello now and you can see that I can commit my changes right from github I'll just say updated readme commit changes all right so I've committed that now let's go back to test and we see that we've made a change and that just happened now let's go back to our command line and now let's figure out how we can actually get this change from the remote repository well the way to do that is to use what's known as a pull so I'm gonna say git pull origin master what I'm stating is what I want to pull so what I want to do where I want to pull from and what branch I want to take in this case I want to pull the from the origin which is the remote that we set up and I want to pull the master branch so let's go and hit enter and it says mm and boom go ahead and we have those changes so that happened everything worked and now if I go back to my sub line text you can see that Hello is the most recent change because that's what we modify now let's do this let's make a change here I'm gonna add hi and I'm gonna actually go ahead to this repository here and I'm gonna make a change on here as well I'm gonna put hello and I'm going to delete actually let's see here I'm gonna add hi I'm gonna add hi again on line four I'm trying to find a way that I can make this conflict so let's do this okay so I'm gonna commit this change here on this repository and now what I'm gonna do is we're gonna add my change here so I'm going to say get add cuz I made a change I say get commit - M change and now what I want to do is want to pull from this remote repository actually what I want to do is push to this remote repository so I've made some changes here I want to push them up to this repository but notice and oops let's go back here that I made a change to this remote repository that I don't currently have on my local machine so I change this just to be high and I don't have that on this local machine so watch what happens when I try to push this to the remote repository so get push and you see that says failed to push some refs too and then that origin updates were rejected because the remote contains work that you do not have so what what I need to do and what this is saying to me is hey there's changes on the remote repository that you don't have yet you need to pull those down before I'm gonna let you push your changes up that's what it's saying so if you get a message like this what you need to do is just get pull first so I'm gonna say git pull origin master and now look what it said and said ok boom we're gonna go ahead and we are going to pull all of these changes down now look what it says here automatic merge failed fix conflicts and then commit the result so what this is telling me is that hey we pulled some changes down but when we pulled those changes we had a conflict in readme dot MD the reason we had a conflict is because we changed two of the same lines and git doesn't know what to keep so on this remote repository I change the same line that I changed on this repository and this local machine right so now if we actually go ahead and look in here we can see that this is showing me there's a merge conflict so when you go ahead and you actually do a like you try to pull it down and there's a merge so we can't automatically sorry there's a conflict it can't automatically merge the code what you need to do is manually tell git what this code should look like so here what this is showing you is that hey there was a merge and there's merge conflict you need to fix this so what you have to do is delete all these arrows delete all this stuff and just make this file look how you want it to actually look so I'm gonna change it to look like this I'm gonna save it and I'm gonna resolve this merge conflict it can't automatically merge it so I need to manually come in and make the fixes which I've done and now what I will do is I will go ahead and I will actually commit this change so git commit - M change okay so I realize that what I've forgotten to do is just add the new change that I wanted to make so pretty much what happens is when you have a merge conflict what you need to do is fix the file so resolve the conflict you need to add those changes and then you need to commit those changes and then you should be good so we're gonna say gits ah dots we're gonna say get commit - em change boom we made the change that's known as our merge commit so we committed the merge conflict that we fixed and now I can go ahead and say get push so if I do this now you can see that I'm totally fine to push up to the remote repository because I pulled I got the most recent changes I fix the merge conflict and then I pushed back up to the remote repository and I made the change that I wanted to make that is kind of what happens and actually happens quite often is you get a merge conflict and hopefully you have some kind of software tool in this case I have sub line text that was able to help me a little bit in fixing that but all you have to do is go into the file that had the conflict sometimes will be multiple files fix the conflict add that file back in commit the change and then you can push again and pull again and everything should be working and functioning now the last thing I'll show you is pushing another branch and then after that I think we've pretty much covered everything we need to do so I'm gonna say git checkout and we're gonna go to the new branch and now what I'm gonna do is just push this branch up to the remote repository so I'm literally just gonna say git push and now I'm going to say origin for where I want to push it to and new so I need to specify that I want to push this here as the branch new to the new remote repository so I'm gonna go ahead and do that and now we will push this branch up to the remote repository so if we come here you can see that it says hey you've pushed a new branch called new and it says do you want to compare and make a pull request why it's saying this is because it knows that what we commonly use branches for is actually merging them with the master branch once they're ready and once they're good so let me refresh this here you notice it says two branches if I click we can see the new branch updated 17 minutes ago and let's click on it and go new pull request now it says you can't automatically merge because there's conflicting changes well let me go here and make this a girafft pull request so I'm just gonna submit it here just do this just to show you something and I'm gonna go to files change and you can see what it's showing me here this is what I added and this is what I removed from the previous file so that is how this works and this is kind of the basis of merging things into master is you make what's known as a pull request by having a new branch and then you compare that to the master branch and you merge them kind of squash them together so with that I think I've covered pretty much everything I can for github fundamentals I do apologize if this video is slightly confusing I'm trying my best here to explain everything sometimes it's hard to think back to the point where I've never used github before cuz I've been using it for a very long time but hopefully that at least gave you a good enough introduction that you kind of understand what's happening in github now maybe some of the commands you need to go look up again but now you have like a good foundation you kind of understand what a commit is what pushing is what pulling is what a merge conflict is how to make new branches all of the stuff that I cover so anyways if you guys enjoyed this video please do leave a like subscribe to the channel and of course I will see you in another YouTube video
Info
Channel: Tech With Tim
Views: 232,491
Rating: 4.9654098 out of 5
Keywords: tech with tim, git for beginners, git tutorial, git fundamentals, github tutorial, git, github, github fundamentals, git tutorial for beginners, git beginner tutorial, github basics, github branch, git for devops, github tutorial for beginners, git basics, what is git, how to use git, learn git, what is github, git push, how to use github, learn github, github tutorials, introduction to git, git explained, git commit, git introduction
Id: DVRQoVRzMIY
Channel Id: undefined
Length: 43min 6sec (2586 seconds)
Published: Thu Jun 18 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.