Git and GitHub Crash Course For Beginners | Complete Tutorial [2024]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
git isn't hard to learn and if you're using an online tool like GitHub with Git well you've just made your life a whole lot easier all you need in order to become very competent with this git and GitHub tooling is a little bit of time give me about an hour and I will teach you everything you really need to know about git and GitHub some patience of motivation and I know you're motivated because you would't be watching this video if you weren't motivated to learn get and GitHub and be a good instructor and put those things together and you can become very very dangerous with these tools you might even become the most competent person on your devops team system administration team or software development team hi I'm Cameron McKenzie I'm the editor-in chief over at the serers side.com and I want to teach you everything that you really need to know about git and GitHub and I want to do it efficiently and quickly I don't want to waste your time with trivial stuff that's not going to make you Adept at this tooling promise you give me about an hour and I'll teach you all the things that you need to know including and before I tell you what I'm going to teach you in the next hour or so I may say some words that you might not know about or some ideas that might be confusing to you so as I list these things off please have faith that you're going to be an expert on these things in about an hour's time and those things would be creating commits dealing with the git index pushing code to GitHub pulling code from GitHub doing fetches doing merges doing merge conflict resolution doing rebases doing cloning maybe squashing a commit and I don't know maybe even if we have the time doing a little bit of cherry picking but in order to do all of those things there's a couple things you need to do first the first thing you need to do is register at GitHub so set up a GitHub account it's easy to do it's just a give your email address pick a GitHub username do a verification and you've got your account the other thing I'm going to ask you to do is install git now most of the people that watch my videos are really anxious to get started so I'm not going to go over the installation process of git right here but I got a bunch of videos that do exactly that you just download git click next at all of the prompts when you do the installation and git will get installed honest to God it couldn't be easier to do and once you've got that we'll get started with creating a git repository maybe maybe just explaining what git is and what GitHub is as well before we create that repo and that is what we're going to do next so I want to get started with Git and GitHub as quickly as possible but before we dive right into the tool I just want to actually talk about what git is and what GitHub is so git is an open-source tool that was built by Lin tral and it's managed by the Linux foundation and it builds itself as a distributed version control tool and I think that's actually a pretty good description CU it describes two of the three things that I think are really important about get first of all it's a Version Control tool which means you can write code and then you can version that code take a snapshot of that code create like a save point for that code in get we call that a commit and so you write some code and then you know you're happy with what you've done you do a get commit you write a little bit more code you do another G commit and it takes a snapshot a picture of the state of all the files in your project in your repository in the folder that has all of your files um at that point in time I often describe it uh to friends as like a like a safe point in a video game it's like you know when you're playing a video game you know you you you get so far you hit a safe point you go further you hit another safe Point go further you hit another save point and then you die well you don't start back at the beginning right you start at a previous safe point so in get every commit is like a save point it's like you know you do some work you do a commit all of a sudden you do some more work you break something you need to go back in time and and see what you where you were at this morning or yesterday evening when things worked um you can roll your environment back to that point in time very easily also people can see your progress you know by looking at the git commits it's a very very powerful feature the other big part of git is it's distributed right it's a distributed Version Control tool which means you can collaborate with others you can share your code with others you can take your code and you can push it to somebody else's git repository where they can look at your code maybe even fix your code or give you some insights on how to make it better and then when they're done you can pull the changes that they made back to your local repository um so you have this ability to share your code with others in a distributed environment that's why they call it a distributed Version Control tool now one thing that they leave out there and I think it's the most important feature of git is the ability to do branching and we're going to cover branching in this course as well but essentially what a branch is in git is it's an isolated environment that branches off the main line of development where you can do code changes you can update things you can break things and it's all isolated from the work that others are doing so you can take chances you can experiment you can try to do something that's brilliant and if you do something brilliant well you can merge your code back into the main branch or the master branch and show everybody how brilliant you are and if you mess things up you can just delete the branch and nobody is the wiser so it gives you that ability to to do isolated development and take chances that's called branching and I think that's one of the best features of git so that's git is a tool that you install download free open source does all of these amazing things so what is GitHub well one challenge with Git is actually sharing code with others it's um you can do it but sharing your git Roo with the git repo of somebody else on the the network can be challenging so what some brilliant people did was they took git and they put it on a server and and they connected it to the internet put it in the cloud and said to people you know we're going to make it easy for you to connect to our get repo um and share code with others will just sort of be like a a central repo for you so a bunch of people just put get that open source tool on a server in the cloud and made it easy for people to connect to it and share code uh so really GitHub is just get running on a server somewhere um and you know making some of the things in git just a little bit easier to do and by the way GitHub was sold to Microsoft for $9 billion a little while ago so taking open- Source software putting on a server somewhere and making it easy for people to use it's a very good business model now GitHub does more than just allow you to have a git repository in the cloud there's a bunch of things that git doesn't do out of the box git doesn't have any security built in why would they it's not part of the product it's it's not part of their goal but if you're an Enterprise you want to control who can see your code so GitHub provides authentication Services no one can see a private repo unless they log in and authenticate themselves um GitHub also provides features like workflow so if you change some code you push it to the server and you want that to go into the main branch you want that to go into deployment well you can set up workflows through things called pull requests that you know make sure that you know maybe someone on senior on the team sees your code before it goes into that mainor Master branch and gets pushed out into the world um so there's workflows associated with it um GitHub keeps metrics for you GitHub allows you to do things like forking of your code there's even like sort of like a follow aspect in GitHub so it almost has a social media aspect to it um GitHub will actually do hosting for you they'll do free hosting and allow you to build blogs using jeal so GitHub isn't just git in the cloud they provide a whole bunch of other services built around git but fundamentally they're just making git easier to use it is get in the cloud okay now with that Baseline and that differentiation between what get is and what GitHub is we now need to go into GitHub and create a repository which is exactly what we're going to do next so if you want to work with Git and GitHub the first thing you got to do is create a git repository you can create it in GitHub you can create it on your local machine but you need a git repository somewhere which begs the question what's a git repository well a git repository typically you have a git repository for every project that you're working on and the git Repository maintains that history of commits every time you take a snapshot of your code every time you do a commit that get repository will keep and maintain that history of commit so it's very important right because that's the essence of gits one of those primary functions the git repository will also keep track of branches that you might create locally so it can keep track of all those little experiments that you're doing with your code and the state of your code in each of those different branches that you've created um it will also keep track of all the remote repositories that you're connecting to so if you've got a git repository on your local machine it knows where GitHub is and it knows how to connect to your GitHub account and pull code from GitHub and push code back to GitHub and then that repo also has a bunch of configuration information about your repository as well about your environment so bottom line if you want to work with Git You need a git repository so how do you create a git repository well I always suggest if you're using GitHub go to your landing page in GitHub go to your home folder and you might see this beautiful green button here to create a new repository you can click that um over here you can also see there's that plus icon the drop- down list and there's a bunch of options to create a new repository there whichever one you want just choose it I'm going to click new repository right there and it's going to ask me a few questions not too many uh it's going to say what's the repository name and I'll call it my project right because I like to project positivity as much as I can it's going to be a public repository I always like to just add a single readme file just to put something in there it's just a text file that goes into your repository but just nice to put something in there and then as you can see there's other options like get ignores which we will actually ignore right now choose a license if you want you if you want to share your code with other people allow other people to see it you you could put an Apache license in there if you want um and then just click the beautiful green button and say create repository and boom that repository gets created so there you go patch yourself on the back you've just created a a brand new git repository in GitHub so what's next well one of the things you can actually do in GitHub is add files so watch this I'm going to click add file right here create a new file I'm going to call this file I don't know alpha. txt and I'll type in hello world because it's sort of required to type in hello world and I'll click commit changes It'll ask me for a commit message and I'll type in hey this is my first commit click commit changes and boom there you go you've got a file in your repository and you even created a commit gets now maintained a maintaining a history of all these commits and if you want you can always go in and add another file if you want just get crazy here right so there's bravo. txt right we'll say hello Bravo world I don't know put whatever you want in there and I'm just creating XT files right now I give it the name second commit but git can maintain anything you can take HTML files Java files python files JavaScript files HTML files CSS files any file you want a version get can manage for you um I like using t txt files just CU it's you know simple to use right um so now I've got two files I got a little bit of a history how do I know I've got a history well look at that history link in the right hand corner there I can click on History right now and you can actually see GitHub is already keeping track of uh my commit history you can see an initial commit you can see a first commit you can see a second commit so you know you get that idea that git will track your files it'll maintain a history of commits for you okay now if you're working with GitHub you know you're probably not going to your code on the server you're probably going to write all of that code locally on your file system and so how do you do that well um how do you get the code from GitHub down to your local machine well the first thing you need to do is go to your git repository so my git repository is called my project and every git repository is going to have something called its GitHub URL it's a a URL that uniquely identifies uniquely points to your repository with all your code in it so I'm on my project page in GitHub you can see that beautiful green button right there you can see this https URL I'm just going to click copy there I'm going to copy it again and copy it one more time always end up having a problem copying these things um and I've now got the git repository what I can now do is I can clone that Repository and I can take the content of that repository and bring it down to my local machine that's called cloning so by the way when you're working with Git You might have a GitHub repository if you want to get the content from that GitHub repository what you do is a clone and that takes the GitHub repo and it copies that repo down to your local machine that's a clone and then you can work locally you don't have to work on the server right you make your changes locally you do commits locally and then when you're happy with what you've done you can then push back to the server um now when you push back to the server your changes will go to the server but maybe somebody else made some changes up there as well and if they did um well what you uh end up doing is you end up pulling those changes down maybe even doing a Fetch and a merge but that's kind of the collaborative aspect of working with Git and GitHub a itory in GitHub you clone it you work locally and then you push your changes to the server if you want to get other people's changes you pull that down you do some more work do more commits and then you push that back to the server that's the the ying and yang of GitHub so I want to take this I want to bring that down to my local machine so again copying that GitHub URL and then coming down to my local machine I'm going to go and create a new folder called underscore repos in Windows and you can see that boom right there there's that underscore repos folder and inside that underscore repos folder I'm going to rightclick and I'm going to select open get bash here now you might be wondering where do that open git bash here command come in if you've installed git and that's the two things I asked you to do earlier register for GitHub get a GitHub account and install git if you've installed git you'll have that option when you rightclick on any open space any white space in a folder so I'm going to open up git bash open git bash here and I'm going to do get-- version so get is definitely installed and then from here I'm going to say get clone and I'm going to paste in that GitHub URL so I'm going to clone that remote GitHub repository so let's give this a try click enter get clone and boom notice over on the right hand side we have a new folder called my project so it's cloned all of that down um I'm going to close this window and I'm going to double click on my project here look at that Alpha and Bravo and read me all of those files are now on my local file system so I've cloned that repository now what do I do at this point in time well what do you want to do right I can go in and I can create a new file so I'm going to say hey I'll just right click on this folder say I want to create a new text document and I'll call it charlie. txt so boom charlie. txt has been created here now what I want to do is I want to open up git bash I actually type in something called get status it'll actually say hey you know what there's a new file on your file system and it's untracked untracked means you haven't told git to include this file in your repository you haven't told git that this is a file that you want get to pay attention to you can imagine that sometimes on your file system there's files that you don't want to put into a shared repository maybe it's just a you know a scrap page or temporary file that you're just writing notes on right and that's not go still go into your project so git's going to assume unless you tell git about a file uh it's going to assume that you don't want to include it in the git repository now this charlie. txt file is fantastic of course I want to keep it in the git repository so I have to tell git about it and telling git about it is called adding it to gits index of files that attracts and so I'll do this I'll say get add charlie. txt this doesn't commit it doesn't create a commit doesn't take a snapshot of the file but it does tell git this is a file that I want you to keep track of so next time I do a commit include that in the commit and actually if I do a get status Command right now you'll see that it says hey this isn't an untracked file anymore I know this is an important file I'm going to include this in the next commit that you do so that's called sometimes they call that staging a file um adding that file to the git index adding a file to the index of files that that you want get to track and by the way not only do you have to do this the first time you create a file but anytime you change a file too so if you you've changed some files git won't automatically include a file in its next commit unless you say Hey you know those changes I want you to include so before you commit a file you have to make sure it's added to the git index both when it's a new file and when you make changes to files so that adds the files to the index now I can do a commit cuz it is saying hey here's changes to be committed that new file charlie. txt I'm not afraid of commitment I don't care what my friend say I'm going to say get commit Das m-m means add a message right include a message in this commit so I'm going to put in quotes third commit right get commit DM what's the message associate with it it'll be third commit and boom all of a sudden actually I get rejected and uh you know I'd actually forgotten about this but this is a good lesson to learn you know whenever you work with Git and you install git whenever you do a commit you have to tell git two things you have to tell git what your name is and you have to tell get what your email address is uh and that gets attached to every commit that goes in your git repository now I know what you're thinking am I going to get signed up for some newsletter something like that if I give my email nobody sees your email okay so the email is just metadata you can put a fake email in here I don't even think it validates the email so you're going have an email that doesn't even exist in here um but every commit has to be associated with a name and an email address that way if you do something brilliant your boss can say hey who did This brilliant thing and then it's like oh Cameron did it it's all like okay well I'm going to go give Cameron an award right um or maybe you'll mess up and they'll fire you but either way every commit has to have your name and email address associated with it now the first time you install git which I just did uh it doesn't know who you are so you issue these two commands you say git config D- Global make this a global setting user. email and my email is uh learnfast mcn.com and then get config D- global user.name and my name is Cameron McKenzie Cameron mcz on Twitter okay now that's done if I click the up key it'll take me back to my commit I'm a little afraid of commits right cuz got rejected last time boy nothing worse than trying to commit and get rejected uh now I'm going to do a third commit and boom all of a sudden it works it says that's not a problem now by the way if I do get status now it says everything's clean everything's been committed nothing's new on the file system uh you know there's also another command that I like I like doing git log i' actually call it git log a dog because there's a few switches I include here I'm going to click contrl l just to clear the screen I call this git log a dog because it's uh I want all of the commit history I want it decorated a d i want it all to be on one line and then I don't know maybe you can graph it or something like that there's a long command get log a dog a d o g a dog um and then that will show you a bit of a history the graph isn't too interesting right now because we haven't done any branching um but it is pretty cool right you start seeing that commit history there's the initial commit the first commit the second commit and the third third commit and you actually even can see origin main origin head um that's actually telling me where GitHub is right because it was that second commit that we pulled down from GitHub so that origin main origin head that's actually saying you know over on GitHub they've only got the second commit okay whereas locally your local main branch has that third commit on it and it's true if I actually go over to GitHub right and I click Refresh on my repository I've only got Alpha and Bravo but over on my local machine I've got alpha bravo charlie so how do we Rectify this well as I mentioned earlier uh when you first copy a GitHub repositor local machine that's called a clone if you want to take your local files and push it back to the server that's a push now in order to push code uh we're going to have to make sure all of our authentication with GitHub works sometimes you may have to create a GitHub auth authentication token I've got some tutorials online that show you how to do that some organizations do SSH I've got tutorials online that show you how to do that hopefully on Windows it will take my browser authentication and authenticate me using ooth when I try and push my change to the server but that's exactly what I'm going to try and do right now so I'm going to say get push and you actually only have to say get push but you'll see some people saying origin origin is just the the name of the remote server that you initially clone from and you can actually change the name origin to GitHub where I say get push GitHub which might make more sense but convention is just origin but I'm going to do a get push origin here let's see what happens it's going to bark and it's going to say whoa whoa whoa do I know you and I'm like yeah you know me I'm learning get fast so I'll sign in with my browser it'll think about it it'll say hey do you want to uh authorize this strange program and I'm like yeah it's not that strange No Stranger than me so it says okay authentications fine come over here it says you know what uh I think we're going to push some files to the server resolving Deltas I love it when Deltas are resolved now I come to my server and hey look over on my local machine I've got alpha bravo charlie but I've only got Alpha Bravo on GitHub it didn't work something's broke I'm just joking you I got to refresh over here as soon as I do refresh boom you see charlie. txt up on the server so we've cloned and brought the repository down locally we've done changes and done commits locally then we pushed them back to the server now imagine somebody else on the team go go and pushes something to the server creates something on the server maybe we got aler bravo charlie maybe they create a file called dvo are we not men uh. txt so let's simulate that maybe up on GitHub I'm going to create another file up here I'm going to create a new file I'm going to call it d. txt are we not uh can I say are we not men are we not devs that's gender neutral um now I'll do this commit now what are we up to is this the third fourth commit commits all over the place here boom we've now got our fourth commit on the server where's dvo it's not there Panic just refresh there it is d. txt so now we've got that file up there on the server but the problem is we no longer have the file on our local machine right alpha bravo charlie locally alpha bravo charlie dvo on the server so how do you get files from the server down to your local machine you might think clone but you only do clone the first time right after that contrl L clears the screen after a clone every subsequent time you either do a get pull or a get fetch I'm going to do a get pull here and keep your eye on the prize over on the right hand side we do see that uncontrollable lurge right there d. txt that was one of their songs Freedom of Choice there we go first album I ever bought um and now we've got uh the files on our local machine up on the server um I'm going to create another file here U I'll do you know touch echo. txt touch will actually just create a new file if it doesn't already exist so you can see there's echo. txt on my local file system I can say get add dot get add dot instead of giving the name of the file explicitly get add dot says add any file on my local file system that is in this directory or subdirectory that has changed add that to the uh git index changed or is new so git add dot is a a little cheat there um and now I can do a commit get commit DM what is this the fifth commit I'm losing track here of all the different commits so get commit fifth commit okay now we've got the fifth commit here we don't have the fifth commit on the server I think we know how to resolve that right we've only got dvo up here but we've got echko over here this is easy right it's just get push the latest versions of get you shouldn't have to say get push origin so I'm just going to say get push and let's see if it works looks like it worked it resolved all of my deltas and boom there we go echo. txt now I did mention that um you know uh if somebody makes a change on the server in order to bring it down to your locom machine you do a poll or a fetch um a poll will actually take the files from the server and then actually copy them right to your actually your actual workspace working tree I think is what they they call it but bring it into your your folder so you can see it sometimes you want to get the changes and get updated about the changes on the server but maybe you don't want to actually have them go onto your local file system because maybe that'll mess things up maybe you're in the middle of something so if you want to your local repository to be updated about all of the changes that have happened in the server but not actually bring them into your local repo you can do a fetch that brings the changes down information about the changes down and then after that you can then do a get merge and merge the the fetch into your file system let me show you that so up on the server here I'm going to create a new file I'm going to call it fred. txt open this up oh no I didn't do that right it's over here add a file create a new file fred. txt hello Fred commit those changes what are we at the sixth commit revenge of the 6th there we go we've got our sixth commit there there's fred. txt now I just want to show you the concept of a a merge you know push and pull is fine um but sometimes people are interested in in the merge so right now I could just say get pull and it would pull fred. txt down alternatively I could do this I could say hey get fetch all the changes from the remote server it'll say okay we'll bring down the changes we'll let your local repository know about the changes but we won't uh merge them into your local file system right so you can see Fred is up there on the server but Fred is not down here on my local file system although information about the update is actually in my local git repository get get used to that term the local git repository is what on your file system and then the git repository on uh GitHub is often referred to as the remote repo okay so that file fred. txt is is down locally but it's not on my file system I can just do get merge I can just say get merge origin and boom all of a sudden we now see fred. txt down on my file system now by the way uh yeah I think that's all pretty good I mentioned the git commit I mentioned the git ad yeah I think that is a a pretty uh wild overview of how git uh works now one thing I was going to mention is you know sometimes you might make a change to a file so um I've got uh this file here hello Fred I love The Flintstones so I'll save that file at this point in time now I've got a a file in my file system that's changed even if I do get status you'll see that so modified normally what I would do in order to do a commit is I would say get add Dot and then I would say get commit if a file's Chang you can't do this with a brand new file but if a file's Chang you can say get add- a which means add all of the files that have changed to the next commit and then you could say or sorry not get add it's get commit Das a this does a commit it adds all of the files that have changed to the index um- M to give a message and uh I'm going to say the seventh commit edited red so we'll do that so if you want to be lazy and not have to type in the git ad all the time you can do git commit d a then- m- a means take all of the files that have changed and add them to the staging index and then do the commit just a little bit of a shortcut there if I do a git status tells us that uh we're on Main nothing's new let's see if I can do a git log of a dog here we go git log a dog so those are all of the changes there that looks interesting wonder what get ref log would show me get ref log is another way of seeing the log get ref log actually focuses on what's on your local machine uh as opposed to what's might be seen by remote server so if you got orphan commits or something You' see that with get ref log um but get log a dog is good enough for now um but what I want to do is I want to make sure that that change that update is pushed to the server so you can see fred. txt in the server just says hello Fred whereas on ours it says I love The Flintstones so let's polish this off let's do a get push move those changes to the server click refresh over here and boom all of a sudden the changes that we made to our local machine are now up on the server and so there you go that is the the whole sort of collaborative thing with get and GitHub get clone get pull get push get get clone get push get pull get fetch get merge get push get whatever we got to do um but those are the the basic collaborative commands and then we also saw some of the commit commands in there you know the get ad the get commit even played around with Git status and get log a dog too just to give you some insights into how the system works so there you go that is a pretty Whirlwind tour of how git and GitHub works together the next thing I want to talk to you about is just doing some important git operations on your local file system like maybe being detached from GitHub a little bit and just looking how you can do things like resets and reverts and uh do commits and manage your commit history um all important things that you need to know if you want to be a power user with Git and GitHub and we'll do that next when we started off this git and GitHub tutorial we first created a GitHub repository added some files to it and then cloned those files so that they were on our local machine I want to actually do the opposite here I actually want to create a local repository on my computer add some files to it and then finally take that local project and push it to a new repository on the GitHub server so sort of the opposite of what we just did and in so doing we're going to explore a variety of new commands the first of which is get init so the get init command will initialize a new repository we're going to create a new folder that folder is going to represent the root of the project that we're building we're then going to say get in it and when we issue the git init command a hidden folder called dogit gets added to the root root of that folder to the root of that project and all the configuration information about your git repository and your commit history and your branches and all of that other information how it connects to the GitHub server that's all located in that dogit folder now we're not going to worry too much about that folder because that's some of the plumbing there we don't usually get our hands dirty with that we stick with the porcelain commands um but before we get into the git porcelin commands we first need to add a file to that folder or no in these commands I'm going to use touch alpha. txt that just creates a a new file named alpha. txt just a fun command by the way the touch command will actually change the timestamp of a file if it exists if it doesn't it creates a new one so um just a an easy way to represent the idea that I'm going to create a new file but if I say touch alpha. txt that'll add a new file to the root of my project now as we know just adding a file to a folder that's managed by git doesn't necessarily tell git to track that file we have to actually add that file to gits staging index the list of files that git is going to track the list of files that git is going to include in the next commit in the next snapshot that it takes so we issue the get add alpha. txt command and that will add alpha. txt to the git index that doesn't add it to a commit yet we haven't done a commit yet we're just saying I want this file included in the next git commit so what do we do next well not do a git commit git commit dmir commit that creates a a new commit in that dogit folder in that projects git commit history in that commit it's a snapshot of alpha. txt at this point in time and you also notice that as that commit happens the git index gets cleared out after a commit the git index index of is clear of files so what do we want to do next well we're going to create Bravo touch bravo. txt that adds Bravo to the file system we want get to track it so we say get add bravo. txt the git index and git says I can do that so we add it to the index and then we say we we want to do another commit so we want to create a commit history here so we say get commit DMC commit and of course a new commit get created and this one includes bravo. txt and you can see our file system is actually the aggregation the sum the addition of all of the commits up to this point in time you also notice that each commit gets a unique commit ID get will generate that for you it's almost like a blockchain you could almost say but each commit it keeps track of with an ID if you ever need to reference a given commit in your history that ID is how you would reference it what do we do after Bravo well you probably guessed it uh we're going to talk to Charlie here hopefully he's not surfing there we go touch charlie. txt that adds the file to the file system doesn't add it to the get index there you got to say get add charlie. txt Charlie gets added to the index then boom all of a sudden we say get commit dasm we do a third commit and look at this get history just building and building and building it's a beautiful thing now with these examples we're just going in and adding files but normally and we're adding one file at a time normally you'll be editing files and you'll be editing multiple files at a time don't get the impression that you got to do a commit after changing just one file you can change 10 files and then do a commit I'm doing one file change at a time just to kind of simplify things but you know maybe you could go in and edit alpha. txt right and uh one way to edit it is the command line is just to say something like Echo high alpha. txt that just appends the letters Hi High to the end of the file I mean you could open the file up type in high click save would do the same thing I'm just doing it the command line just to be just to be cool um but let's say you edit alpha. txt I put a star there next to Alpha txt and the fossil indicate that it was changed then maybe after that you're going to add d. txt are we not devs uh dvo gets added now we want both Alpha and dvo to be added to the index there actually a shortcut we can do get ad dot you'll see the dot there that says take every file that's new or changed on in the local folder or subfolder and add it to the git index and if we do that well boom all of a sudden we can say get commit D m4th commit and then that fourth snapshot keeps track of both of those files it includes the changes alpha. txt and the fact that d. txt is a brand new file now that's all fine and dandy you can see the four files on the file system there I want to introduce you to another command here because I I keep talking about the get commit history but what good is it well imagine you were unhappy with what you did right now um and maybe the code doesn't work anymore and you need to go back in time maybe the last time things worked was with that bravo. txt commit what you can actually do is you can actually type in get reset -- hard then the ID of the commit that you want to revert back to not well not revert reset back to there's actually a revert command that's slightly different you want to reset back to bravo. txt you say get reset hard 2B f149 and when you do that that look what happens to the files in the file system boom Charlie and the change to Alpha and d. txt those all disappear they no longer exist anymore and in fact we've reverted back and the change to alpha. txt is gone as well we've actually reset our local file system back to the way it was previously at that point time and so that's one of the cool things that we can do with Git not only can we maintain this git commit history but we can actually roll back our workstation roll back our files roll back to a previous commit in our commit history um and then start over again and then from here we can start changing files and doing new commits and rewriting that history so that's one of the cool things that we can do with Git using that git in it get ad and get reset command to really manipulate our change history now let's actually do it at the command line and maybe even talk about a couple of other commands like git revert and git cherry pick maybe even connect this project to that remote GitHub repository as well and that's what we're going to do next okay enough with the theory let's actually create a new git repository on our local machine and prep it for pushing our project up to GitHub now I'm going to create a new folder under that underscore folders folder and this is going to be called No Fear because it's going to be all about commits and I have no fear of committing and in this folder I want to create a new git repository so you can imagine maybe this is a Java project maybe this is a python project HTML CSS project whatever you want to track with Git if you want to track backck it with Git you've got to well you've got to initialize a git repository super easy to do you just type in g in it and keep your eye on the prize once I do that boom that dogit folder gets created it is a hidden folder so you might not see it on your operating system if you don't have it configured for hidden folders to display if you go in there you can see some of the plumbing it's information about how we manage a git commit history how you actually commit to a GitHub server although we haven't configured fed that quite yet as I said that's all Plumbing we want to stick to the porcelain so get out of there you don't want to spend too much time in there um but what we do want to do is actually create a bit of a git commit history so I'm going to add a new file here I always like adding alpha. txt in there if I do that and ask for the status G's going to say hey I'm not tracking that file so we all know we can do get add Alpha .txt that adds the file to the git index so if I ask for the status git says yeah we're going to add that and include it in the next commit then I'm like okay let's do a get commit get commit DM first commit and again this is a new repository we did a bunch of work with a repository we created in GitHub that's separate from this repository this is a separate project separate projects get their own git repository so so even though I made commits in another project this is the first commit in this one here now I want to create a history of commits I'm going to do this again three times so that I've got Alpha Beta Charlie and d. txt files and I'm going to create a commit each time so feel free to follow along but I'm going to do this quickly just to create that interesting git commit history I'll do touch beta. txt I can do a get add do command that's just a shortcut I do a get commit DM say second commit okay touch charlie. txt get add dot get commit dasm that's the third commit okay you know where this is going right touch dev. txt get ad dot not TIG ad dot get add Dot and then get commit dasm and what is that that's our fourth commit okay that gives us a nice git commit history by the way you can always do a git ref log command and that shows you your history of local commits might include commits that um people might not see if you were actually to to push up to GitHub so there's git log and get ref log get ref log kind of exposes everything in your local file system now here's what I want to do I want to reset my environment back to that second commit I want to say hey you know what I don't like this Charlie file I like this dvo file it's breaking things I want to go back in time to that second commit where everything worked and it's easy to do you just say get let's reset back to that second commit that that second commit has an idea of EF 0000 DB now on your system it'll have a different commit because every commit is given a new unique hash code a new unique ID but if I say get reset Das Dash hard because this isn't easy to do and give that commit ID e00 DB that's actually going to reset my environment back to the way it was at the that second commit by the way hard actually means uh reset get to Point back to that previous commit um clear out the index and also reset the local file system and so keep your eye on the file system they're going to click enter keep your eye on the prize and boom all of a sudden Charlie and dvo are gone I've now reset back to a previous point in time I've said you know what the work that I did on these files isn't working for me I need to go back to a time where it worked and all of a sudden I've done that now a lot of people will say well what happened to those commits maybe there was some work in there that you wanted to keep well if I do a get ref log again you'll notice that that third and fourth commit are still here now if I push this repository right now up to GitHub it wouldn't push the fourth and third commits up so other people wouldn't see it but anything you do locally is going to stick around in that get repository git does not get rid of things easily um so that fourth and third commit are still there so in fact I could say you know what I didn't mean to reset back to that third that second commit I meant to reset back to the third commit df13 so we can do that get reset hard to dff F1 A3 keep your eye on the prize boom Charlie D txt comes back again because we're now saying no no no it was the third commit I wanted to reset to who knows you may have just total regrets and say I don't want to do that at all I want to go back to that fourth commit and you can do it get reset dash dash hard the fourth commit was five D5 dc3000 and boom there we are back to where we started from but you can see how get when you create a rich git commit history git gives you that ability to sort of move through time go back to a previous state in your repository maybe start off from there when things worked or maybe if you've regret that decision go forward in time again it's incredibly powerful and that's what we talk about what we mean when we talk about how git is a aversion control tool it allows us to keep that commit history and maybe even go back and revisit previous commits now that's reset there's a command that people often confuse with reset called revert I'm going to do a get ref log right here now what a revert does is a revert will surgically remove the things that happened in a particular commit so with reset we said I want to reset back to that second commit it just reset everything back to the way it was when that second commit happened if you revert the second commit what will happen is get will remove everything associated with that second commit but leave the stuff that happened in the third and fourth commit alone so watch this I going do a get revert and I'm going to say get revert that second commit e f0000 00 DB it's actually going to ask me for a git commit message uh cuz when you actually do a revert it actually creates a new commit for you this actually takes me into the vi editor or Vim editor which can be confusing for many people by the way if you can configure git to be configured with notepad++ when you install it I recommend that uh from here to get out I just do colon and then Q I could update the commit but that's good enough for me colon Q gets me out of there and look what happened in the file system beta. txt is gone I just surgically removed what happened in that commit with the revert and contrast that against the reset which re which removes everything that happened subsequent to that commit so revert surgically removes the things that happened in that commit a reset takes you back to that commit removing all the files that happened after that commit happened so that's kind of interesting now by the way uh the revert will surgically remove things from a commit there's actually a command called cherry pick and cherry pick will bring in all of the things that happened on a commit so a cherry pick is actually the opposite of a revert Cherry pick's often used to cross branches which we're going to talk about next but you can also use it here so you saw that we reverted EF DB let's do a g cherry pick and cherry pick e f00 DB is it cherry pick I always forget the spelling of it is it cherry pick that's right p i c k cherry pick and boom now we reverted the revert no no we cherry-picked that commit that second commit which was reverted and we brought it back in into our environment so there you go that is creating a git commit history that's the git init command to create your git repository that shows you how the git reset command works it shows you how the git revert command works and the difference between git revert and git reset and it also shows you that git Cherry pit command a little introduction but we're going to see more of that later now by the way I've got this local project on my file system I want to push this local project to the remote GitHub server how do you do that well first of all I'm going to create a new repository in git so I'm going to come over to git I'm going to say I'm going to create a new repository here I'm going to call it no more fear and I'm just giving it a slightly different name from the project just so you don't have the if I called it no fear you might think oh does it need the same name as the folder it doesn't I'm just going to create this repository I'm not going to put a read me or anything in it I'm just going to create this repository so it's blank it doesn't doesn't have anything in it if you got something in it you might run into some troubles and the repository says look if you actually want to add something to this repository here's how you do it so as you can see it it does say you know do the g in it and then add some files to it we've done all of that then it says do the git Branch DM Main and that's just going to rename that Master Branch to name to main um so that your local repository in the remote server uh consistent and the same so I say get Branch dasm Main and you'll notice the name Master boom switches to main um and then we want to actually say hey um let's tell our local repository about that remote Branch so we'll say get let's add a remote Branch to this repository not a remote Branch but a remote rote repository remote reference actually I think is the term um and the reference is going to be https github.com learnet fastor fear. I'm going to just look at that I could have copied it in um but you're basically saying to your local repository I want you to be able to push and pull to this remote repository on GitHub I want this project to be associated with that remote GitHub repository so we add that get remote add that all looks good oh what did we do get remote ad origin oh I didn't give a nam so okay there we go get remote ad you know and I'm actually just going to call it GitHub right just you don't you don't have to use the name origin I'm going to say get remote ad GitHub now GitHub is added as a remote server all I have to do now is say get push - GitHub and push that main branch to GitHub so we're actually setting an an upstream Branch for the server basically saying take this local branch and push it up to the server we click enter and boom all of a sudden we now have that local repository pushed up to the server if I come up to the server here I do a refresh you notice that we have a a new Branch name made and we've got all those files Alpha Beta Charlie and dvo and even if I look at the six commits the commit history we'll see all of those commits here the first commit the second commit the third commit and we'll also see the commits that were happened when we did the revert or the cherry pick as well so there you go that's how easy it is to work with your local get commit history create a new local repository reset revert cherry pick and even can take your local repository and push your local project up to a remote GitHub server it's all super easy now the next thing we're going to take a look at is some of my favorite stuff about git which is working with branches that's what we're going to do next so the next thing on our agenda is branching with Git branching gives developers the ability to break away from the main branch or the master branch and create an isolated development environment for themselves where they can create files update files change things delete things take chances and experiment and you know maybe come up with something brilliant and if they fail they can just delete the branch and nobody is the wiser now branches are a mouly complicated concept so Darcy declute her book scrum Master certification guide is behind me right there she put together uh some visuals for me here to help us visualize how branches work now just imagine you do that get init command you create a brand new repository you start off with the main branch or the master branch and well initially there's nothing there so I don't know what do you do you add a file you can do the get touch uh you can do the touch alpha. txt and get add then do a get commit like we did on the previous examples and then after you do the kit git commit you've got your first get commit and you've got a file alpha. txt in that main branch right that all makes sense now let's just say somebody wanted to experiment a little bit they didn't want to mess up the main branch um but at the same time they they kind of want to take the files on the main branch and and play around with them a little bit you know uh uh break things uh be courageous so what a developer can do is a developer can create a new branch and then switch onto that new Branch so here's one command get switch dcde that says create a new branch called Dev and then switch onto that Branch so the user can issue that command that creates a new branch called Dev now initially Maine and Dev are exactly the same when that new branch is created both Dev and Maine at this point in time are pointing at the same commit right you just said hey there's a commit there I like it I want to experiment with a little bit create a new Branch off that commit Point okay and so that new branch is created but when the branch is created the main and Dev are pointing at the same thing but then you know you can add a new file to Dev so touch bravo. python you can see bravo. py now gets created in this Dev Branch it's not on the the main branch and you can do a get commit make it your second commit now the dev branch has two files in it the main branch only has one and in fact you could go ahead and then maybe even create another file create charlie. python add it to the git index then do your get commit your third commit and now dev's got three files in it Charlie and bravo. Python alpha. txt and the main branch only has alpha. txt so now the develop ER could delete those files and delete the development branch and nobody's the wiser um while the developer is actually doing this maybe somebody's actually updating the main branch as well maybe there's some fix that's gone into the main branch so um maybe even the developer themselves is doing that so you could always switch back to that main branch do a git switch main that now takes you from the dev Branch onto the main branch and now the files on your your workstation if you did that switch onto Main you'd only see alpha. txt cuz the Marain Branch only has alpha. txt it's that other Dev Branch it's got Bravo and Charlie python now just imagine this maybe the developer on the main branch is going to add d. txt add it to the staging index do a get commit and now all of a sudden we're really in trouble because the main branch has d.o. txt and Alpha and then dev has Charlie and Bravo in Alpha but it's all out of sync so now what do you do um well you know this isn't necessarily a bad thing maybe the developer working in Dev is thinking you know I've done something brilliant here and I know the people working on Maine in parallel have been updating things and fixing things that's great too but I'm going to take my changes and merge them into main so in this case a developer would switch onto the main branch so get switch main where we currently are and then they would issue this command here they would say get merge Dev so you're on the main branch and then you say what I want to do is I want to merge all the files from Dev from the dev Branch into Main and when you issue that command watch these files move the files that are in Dev move over to Main and then a new commit happens that merge commit which indicates that you know what we've actually had some files move over here when we've merged the state of that uh Dev Branch with the main branch and so after that merge the main branch has dvo Charlie braa and Alpha in it now what's in Dev we merge Dev into Maine but we didn't merge Maine into Dev so right now Dev the the the last status of Dev is that third commit if we were to switch back to Dev it would take us to that third commit where we'd only see Charlie Bravo and Alpha right um we haven't actually merged the files in main which in this case would be dvo into that that Dev Branch so a lot of people miss that they a lot of people think that when you do the merge both branches get synced but no you're on the master Branch you merge the files from Dev into master that only brings the files from Dev into the master Branch doesn't do anything with the files in the master branch and putting them into dep so there you go that kind of helps you visual ize how branching Works in git why don't we do exactly this on the file system with our git bash tools doing this git branching and git merging we'll do that next okay that's enough with the theory about git branching what I like to do is I actually like to do that git branching example on the local file system so you can see how you can merge branches together locally on the file system but then I actually want to do it up on GitHub and show you something that GitHub includes in the process called a git pull request a GitHub pull request I think you'll find that interesting but starting off I'm actually just going to create a new folder here on my file system and I'm going to call it cool Branch because I think Americans love that on their salads and stuff cool Branch topping I think it's called and I'm just going to open up a git folder a git bash shell here so right click open git bash shell and I'll do the requisite git init command and you can see I get that git repository created over here now I want to add a file to this well I'm in the master Branch right now I want to add a file to this I'll say get I'll actually say uh I'll do it over here create a a new file called alpha. txt file created do the get add do the get commit dasm first commit and boom all of a sudden we've got a nice get commit history I do get Ru log and we can see there we go we've got one commit and Alphas included in it now let's say somebody on the team or maybe even you wants to do some experimentation you want to actually go to another Branch create a new isolated area where you can do development well what you can do is you can say hey I want to uh switch onto a new Branch I'm going to call that branch devb and since it's not already created you have to do- C and then say the name of the branch that'll switch you on to a new branch called Dev when I do that you can actually see the word in the get bash shell changes from Master to Dev and I'm now on the dev Branch now what do we want to do here well I going to create a new file create a new file I'll call it bravo. txt do the standard get add. get commit DM second commit okay now all of a sudden we're creating a rich commit history um but look at this right now I'm on dev imagine I did this get switch master what do you think would happen if I switched on to the master Branch right now boom Bravo goes away right because Bravo is only on the dev Branch it's not on the master Branch right so I can actually be on the BR Master Branch here and create a new file I'm going to create a new file and in this case I'm going to call it d. txt just to be consistent with the PowerPoint presentation I did before I haven't created Charlie yet but I can say get add dot I can say get commit DM and I'll call it the dvo commit so now on the master Branch I've got Alpha and dvo but now I'm going to switch get switch head over to that Dev Branch boom dvo goes away and Bravo comes back in fact if I did that get log d get log a dog all on line decorate graph you actually see it becomes a bit more interesting right now like you can actually see that after that first commit uh on the one on the left side kind of stop side we've got you know branching off with Master goes differently with dvo commit and then you actually see Dev goes to another spot with that second commit and I'm actually going to add another file over here to that Dev Branch I'll create a new text file called charlie. txt do the get requisite get add Dot and get commit DM I'll call it the Charlie commit I don't want to call it the third commit or something like that because we're kind of getting out of order here from the phonetic alphabet but if I click that up button again you can the up button in terminal shows you previous commands um I didn't mean to do the commit I wanted to find that lck you can see now we've actually got uh two commits on dev the Charlie commit and the second commit and then dvo over there with that uh the Masters over there with that dvo commit so you can see that our branches are diverging right like we've got some work on dev we've got other work on Master dev has Bravo and Charlie if I do a get switch and move to master get switch Master you can see master has Alpha and dvo but it does not have Bravo and Charlie so let's say you want to get the files from the dev Branch into the master Branch how do you do it well you just do a merge you just say get merge merge Dev into Master that's what you're saying get merge Dev into the current branch which is Master I'll click enter now keep your eye on the prize okay I got to put in a get commit message I'll take the default which means means just typing in colon and then Q to get out of there and boom all of a sudden on the right hand side we now have alpha bravo charlie and dvo all the files from the death branch have been merged into that Master Branch but don't be fooled if I actually do a get switch onto the dev Branch the files from Master have not been merged into Dev right they are not at the same point we merged Dev into Master we never merged Master into Dev which we could do if we want we can say get merge master and now that uh Dev branch is the same as master and the master branch is the same as Dev and you know for that matter we could actually go and take a look at the git log and you actually kind of see here now Dev and master are all merged into the same common place we' still got that branching of the history where it kind of diverges and goes out um but we've got to a place where Dev and master are exactly the same and so there you go that's how easy it is to well create branches create Divergent branches and then merge the files from those branches together all at the command line now when we do it with GitHub it's a little different merging in github's bit different from git we'll actually use a pull request so when we want to pull the changes from Dev in into the main or Master Branch we'll do a pull request and if the pull request succeeds we'll actually then do a merge which is exactly what we're going to do next more often than not you're going to be creating branches and merging branches on your local file system either through git bash get goey maybe an IDE like uh Clips or Visual Studio code however you can create branches on GitHub and you know if you're actually going to be merging code into the main branch or the master branch and you're working on a team there's actually probably going to be somebody who oversees all merges into that Manor Master Branch because quite often anything that goes into that main or Master Branch might be involved in continuous deployment you know you may have a product build that goes out and gets deployed right into production when a merge to master happens so we actually have the ability to incorporate that in a workflow in GitHub which is called a pull request where you have some code you want to merge into to the main or Master Branch you actually make a request for that to happen and then somebody oversees it make sure that it all works and then merges it it that's exactly what I'd like to show you right here so I'm going to create a brand new repository I'm going to call it olives because I believe that well we should always offer Olive branches to people to be public and I'm going just going to put that requisite read me file in I'm just going to create that repository and I'm going to add one single file to it I'm just going to add our good old alpha. txt file in there so we've now got one file and one commit in our repository maybe we made a commit when we created the repository as well but you get the point so we've got one branch called Main and that one branch called main has one file in it called alpha. txt plus the readme so what I'm going to do is I'm going to go over and open up the same repository in a different window so this is the same repository just two browsers here but in the second window I'm going to type in Dev to create a Dev branch and notice I've got this link here do you want to create a a branch Dev from Main I'll say yes and at this point in time Dev and Maine are exactly the same they're pointing to the same commit so they're completely in sync but as soon as I come over here and create a new file like bravo. txt I got to take that in there bravo. txt boom all of a sudden I now have a Dev branch that is out of sync with the main branch if I come over here and do a Refresh on Main you're going to notice that I've only got Alpha on Maine but I got Alpha and Bravo on dev and in fact I'm even getting a message that sing to me hey you know Dev is actually uh one commit ahead of Maine might want to catch up on that but I don't have time for that I'm doing a lot of development over on this Dev Branch so I'm going to add another file I'm going to add charlie. txt over here do the commit and now that good old Dev branch has two files in it that that Master Branch doesn't and I'll do a refresh over there just to prove that to you right I we get messages saying hey dev's got some pushes to it that you don't have in the main branch now the main branch can fight back right the main branch can go yeah well you've got Bravo and Charlie I've got dvo are we not devs commit changes over here now it's a complete mess right we've got uh Dev on the right hand side with Bravo and Charlie we've got got main on the left hand side with Alpha and dvo boy oh boy how are we going to bring all of these in syn well here's how you do it and in fact there's a big green button um there incentivizing you to do this um what I can do is I can do a pull request right so I can be on that Master branch on that main branch I can see that there is uh that dev has some recent changes to it so what I'll do is I'll click this compare and do a pull request and essentially I'm going to do a request that well that the changes in Dev get added to main now this might happen you could imagine you you've got one developer working in Dev and then you've got maybe the team lead who oversees the code that goes into the main branch so a Dev would go in to the main branch and say Hey I want to pull request on dev we're doing it all individually as the the the one sole uh benevolent dictator for the whole repository but you get the idea so we come in here and you know you can give a a title to the pull request you could say hey merge my Dev files into main right whatever you want um the developer creates the pull request so you now create the pull request and that pull request is then going to be seen by the team lead the next time they come in or other people that might have control over that repository now right now you can actually see here since I've got full control over the repository I'm being asked to merge in the files that were associated with Dev associated with that pull request and so over here I can say hey yeah I can create a merge commit maybe do a squash and merge or rebase and merge let's keep it simple right now we'll just do what we did on the file system before which is a a merge so we'll merge the pull request we want to confirm the merge yes we do and now all of a sudden in that Master Branch we've actually got all of the files that were in the dev Branch as well so let's go back to this olives Branch the main branch here and you can see the main branch has alpha bravo charlie and dvo we've now merged all the files from Dev into the main branch but at the same time let's go and take a look at that Dev Branch we never merged Maine into death so Dev doesn't have that d. txt file right we merged Dev into main okay so now main has all the files from Dev but we never merged Maine back into Dev so Dev doesn't have that d. txt file but who knows maybe you're just going to delete that Dev file once everything's merged in or that Dev uh Branch after everything's merged in you quite often do that with topic branches there isn't always that need to to bring them both in s syc but there you go that is how you work with branches in git both at the command line and online completely using GitHub with pull requests as well now that's merges um a second ago we saw the option to well we saw the option to squash and we also saw the option to rebase rebase that's what I'd like to talk about next now we have become experts at git branching GitHub branching merging branches together even doing GitHub pull requests and merging branches that way but you know there's a few things people don't like about branches people don't like having this disjointed history where things Branch off that upsets some people uh there's another problem with this sort of merging approach and is the fact that we end up with this Superfluous merge commit right no work happened in that merge commit no no files were changed no files were added no files were deleted it's just like a marker commit right and it's it's just kind of bookkeeping right doesn't represent anything that's done so there's an alternative to a merge which achieves the exact same result okay the exact same result of bringing files say from Dev into main like a merge does it's not a merge it's called a rebase and so it achieves the same result but it plays around with your history a little bit to give you a linear history and eliminate that Superfluous merge commit so here's how it works I would go on to the main branch and previously I would say get merge Dev and that would take the files from Dev and merge them onto Main instead I would say get rebase Dev and when I say get rebase Dev well things start to change so first of all what ends up happening is we end up going to that very first commit where we created that Dev Branch now when we actually created the dev Branch off Main off that first commit when we first create the dev Branch Main and Dev actually are pointing at the same commit they're both pointing at that first commit so not until we do that second commit exclusively on the dev Branch where they get out of syn when we do a a rebase and we we're on the the main branch and we say let's rebase Dev onto Maine what we do is we take the commits on that Dev Branch we just put them onto that on top of that shared main Dev commit that we started out with so all of a sudden that second commit is no longer part of that exclusive branch of Dev now it actually just gets put on top of that original first commit um and now Maine and Dev both for the first commit and the second commit are the same we then get the files from Dev pushed into Maine for that second commit so if anybody ever went back to that second commit both Dev and Maine are exactly the same right there and then that third commit the same thing happens we rebase that third commit on to the main branch so for the first commit second commit and third commit well main Endeavor exactly the same we don't have any diverent Dev branches and you can see actually the files in Maine and the files in Dev for first second and third commit are all the same as G is rebasing you can see that Maine is getting all the files that it would have got with a merge now here's the thing that third commit remains part of Dev so Dev is now pointing at that third commit so Dev still has Charlie Bravo in Alpha which is exactly what it had when we did the merge but the rebase then takes that fourth commit and it puts that fourth commit on top of the third commit which was put on top of the second commit which was put on top of the first commit which was a commit that was shared both by Maine and Dev you can now see dev has been rebased onto Maine so Main and Dev are the same all the way through to that third commit and in fact if you looked at the files and Dev you would see that the files and Dev or alpha bravo and Charlie everything up to that third commit now with the main branch we've got that fourth commit and that fourth commit is now stacked on top of that third commit so it almost looks in history like the first second and third commit happened and the fourth commit happened after that and it doesn't just look like that that's what the history is telling you has happened we've now rebased Dev onto Maine and then taking the commits that happened in Maine and then put them back on top of Dev we're now given a linear commit history and that Superfluous merge commit is no longer there so we've solved the problem of the branching and Divergent history and we've got got rid of the Superfluous commits all by doing a git rebase instead of a git merge and one other thing that's worth mentioning that third commit is where Dev points to Dev is now pointing at that third commit okay it's time to do a git rebase I'm actually going to do a git rebase on my local file system and then I'm actually going to do uh get rebase on the server that's going to include uh a push a pull creation of a new repository and a pull request and a rebase as well so that's going to be wrapping everything up together in a bow but I'm going to create a new folder right here and uh I'm going to call it my Reaser okay and I'm going to go into that Reaser project and open git bash here do a get in it and then give me just one second I'm going to create a git history of two branches with alpha bravo where it needs to be merged or rebased together so just give me one [Music] minute [Music] [Music] okay here we are as you can see I've got my rich git commit history here I've got a Dev Branch I've got a master branch and you can see the Divergence and I want to merge Dev into Master but I don't want that branched history and I don't want an extra commit representing that merge commit so the option is to do a rebase so I'm on the master Branch I want that second and third commit added to the main branch the master branch and those second and third commits were from Dev if I do an LS command you can see I've only got Alpha and dvo locally not Bravo and Charlie so what I can do is I can do a get rebase and rebase Dev onto this Branch I'm going to move this file here so you can see the files on the file system right there so we going to rebase Dev it now says hey we successfully rebased and updated the references so now you can actually see that we've got all of the files in Dev I actually open up a new git bash shell here just so that I can do the get log all oneline decorate graph get log a dog actually get log a git log AO okay I messed up the order there but you get it uh click enter and now look at that there's no longer this disjointed commit history we've now got a linear commit history first second third fourth commits that fourth commit was put on top of the second and third commit um and we have a beautiful git commit history here with no Superfluous merge commit that didn't do anything other than just track uh an operation that didn't affect files so there you go don't be afraid of the rebates it's just a way to to keep your commit history clean now let's see if we can pull all of this together and do a a crazy git GitHub rebase pull request Branch all that other stuff uh to really hit this thing out of the ballpark and uh mark down a grand slam for our git and GitHub tutorial that's what we're going to do next okay we're getting towards the end here I want to actually pull together everything that we've learned so far in this git and GitHub to tutorial I want to create a new repository I want to clone it I want to create a branch I want to add fils I want to push to the server and then I want to do a GitHub pull request and then maybe do a rebase of the code instead of a merge like we did before going to introduce a couple of new commands as we do this but this is going to kind of represent what you would do on the first day of a job if someone says hey here's the GitHub repository go do some work and show me how good good you really are so starting off I'm going to create a brand new repository up here on the server I'm going to call it the Grand Slam repository cuz we're going to hit this out of the park here I'm going to add the readme that creates the repo and now I've got a GitHub URL that I can use to clone so click that beautiful green button that says code copy that URL go into your folder on your file system where you have your repos click open get bash here I always say get bash shell but bash stands for Born Again shell so it's like saying the get bash Shell Shell anyways uh and do a clone get clone and paste in the name of that repository now my paste is not working so I got to come back and paste it harder there we go there's the paste let me see get clone paste that in come on be good and there we go we have now cloned that repository I'm going to close that window go into that grand slam folder and there's the read me that I created so everything's looking good so far now from here I'm going to open the git bash shell and we don't want to do any work on Main so the first thing I'm going to do is create a new Branch I'm going to say get Branch dasc for create and create a Dev Branch so that now creates a Dev Branch earlier we did a get switch so you can do a git switch which switches You from One branch to another and you can use- C and the name of a branch and if that Branch doesn't exist it'll created it for you so we did that before this is another command get Branch get Branch DCd creates a new branch called Dev but you can see we're not on the dev Branch we're still on Main um but if I do get Branch d-list you see it list the branches and Dev is there so now I can do a g switch and do a get switch and switch on to Dev now you may have seen someone do checkout so you can also do get checkout Dev as well they're kind of deprecating checkout checkout is some baggage cuz in other version control systems that checking stuff out kind of implied locking files and not allowing others to have access to it we don't have any concepts like locking files in git it's purely distributed um so get check out Dev took me onto the dev Branch but uh and you'll see a lot of older tutorials using git checkout you want to stick with Git switch now I'm on this Dev branch and what do I want to do well maybe I'll just add some files to this Dev Branch so I'll create a new file a new text document I don't know I'll call it index. HTML create an HTML file here for the first time yes do the git ad dot do the G commit DM and say uh creating a website and I don't know should we add another file a new file what do we got the about. HTML you can add that in I don't know I'll add a couple of commits in for the the same one so maybe I don't know contact. HTML simulating a website right okay there we go we got a couple of files here do a get add dot get commit DM more files and now we've got a rich git git commit history on our local machine now all of these are in Dev if I do a get switch and go to the main branch you know so all those files disappear um but if I do the git Switch and Go onto the dev Branch they all appear once again now here's something I'd like to do we haven't done this before in fact probably going to cause a little bit of a problem what I want to do is I want to um push this Dev Branch up to GitHub right if we actually go to GitHub we can see that we Clon the repo but there was only a main branch there if I click refresh that main branch is the only thing that exists we don't have a Dev Branch up on on GitHub so let's say we want to actually push this Branch up you would think it's going to be as simple as get push origin right but it doesn't like that it says the Upstream branch of your current Branch does not match the name of your current Branch to push to the Upstream branch on the remote use get push origin head colon main oh boy this is just going to be some big headache so let's try this let's do get push origin and we're actually on the dev Branch so get push origin Dev get push origin head Dev and now we're actually going to be pushing that Dev Branch up to the server not uh onto the that actual main branch so let's go over and see if that actually worked do a refresh we've got the main branch nothing is in it but boom there is the dev Branch we move on to that and about contact and index so there you go that's how easy it was to take our files in a new branch and push them up to the server right because you probably don't want to be merging into master or main even locally right you don't want to have that happen you want to write code in your own topic Branch push your branch up to the server and then ask somebody on the development team to take a look at your code and if it's really good maybe merge it in so you want to do a pull request you want to say hey I want you to pull in my code take a look at it and if you like it merge it into the main branch or the master Branch or or do a rebase right you could even do a rebase so let's go and take a look at the state of the server right now so I'm up here on the server I'm going to move around a little bit so there I am in the server it says hey you know there's two branches so I'm going to look at these two branches here it looks like there's a main branch and there's a a Dev Branch let's take a look at this main branch okay the the main branch doesn't have much in it I look at the dev Branch there's a couple of things in there okay well what I want to do is I want to merge the files in Dev into main before we clicked on this green button compare and pull but that's not always going to be there uh what will always be there is this option to create a pull request and so over here I'm going to say I want to create a new pull request and what do I want to do well I want to take all of the files that are in my Dev branch and move them into main so I want to move all the files from Dev into main so GitHub takes a look at it says well okay we're able to merge those we'll create a pull request and send this request to the appropriate people so we'll create a pull request you can add some details there create that pull request but essentially you are now saying as the user you're saying to the the the team lead hey pulled this in so the idea is the team lead comes in the next day um they go into their gith Hub they start looking at some of their repositories they go into the Grand Slam repository and they say oh look at that there's a poll request okay now I'm doing all this as the same user but you could imagine right you know the team lead's going to get a notification that says there's a PLL request they're going to go and take a look at it so they look at this PLL request and they say okay there's some files in there they changed they added some files couple of interesting things in that commit history you can go in here take a look at what happened um then if everything looks good in terms of all of these commits and creating a website more files gez what happened down there it looks like created the about and the contact files looks great this is a great developer I'm so glad that I hired them okay so now that team lead can come in and say okay everything looks good I'm going to merge those pull requests but you know we've got got one of those team leads that doesn't like Superfluous commits and they don't like the branched history so maybe this team lead is going to say you know what rather than just merging I'm going to do the rebase and merge so now the dev says I want to rebase and merge clicks rebase and merge takes a look says confirm rebase and merge and now all of a sudden those files have gone into that main branch into that Master branch but it was all done using a rebase not using a merge so if we now look at Main you can see Main's got about contact and the index.html file all of the files from that other branch have now been not merged but rebased into the main branch and there you go that's kind of how git and gith hub is supposed to work you have a repository you clone it create topic branches where you do isolated work you do your commits you push back to the server you do a pull request and then hopefully someone senior on the team looks at your changes likes them and merges them back into the main or the master branch and there you go that is a a good overview of how things work when you're working with Git and GitHub okay you can't talk about merging files or rebasing files without also talking about the possibility that you'll get a merge conflict a merge conflict happens when a file is in two separate branches and people are working on it at the same time and you try and merge those branches together and gets like I don't know how to fix that like two people wrote code at the same time in the same file in two different branches you want me to figure it out it's like not going to do it so let me show you how to create a merge conflict and also to to fix one so I'm going to create a a new folder and I'll call this piece because we want piece um and in this folder I'll create a new repository so I'll open up my git bash shell here say get in it and touch alpha. HTML G add dog commit DM first commit and there we go we've now got one file in our repository and it's named alpha. txt now let's say I go over to the branch called Dev so get switch DCd to go onto the dev branch and I go in here and I look at this Alpha file and I say hey you know what I'm actually going to uh I'm going to edit this file and I'm going to say welcome to my awesome website okay no problem right get add get commit DM awesome edit Okay so we've got that commit right um now let's go back to main get switch main get switch Master now I'm on the master Branch now from the master Branch we going to go and open up alpha. HTML and of course on the master Branch we haven't made any edits to alpha. HTML right the they were made on the other Branch or maybe some someone come in here and say you know um hey this is a great and Fabulous website and now they've changed this file in this Branch so on the dev Branch it says this is an awesome website in the main branch it says this is a great and fabulous website so I come in here I say get add dogit commit DM fabulous okay and uh now maybe I want to merge everything together I'm going to do a get log d-1 line-- decorate D- graph Dash Das all okay so you can see we've now got uh two commits it's branched out I'm on the master Branch let's merge Dev in so get merge Dev let's bring Dev in and let's see what happens merge conflict in alpha. HTML automatic merge failed fix the conflicts and then commit the results and you can see it now says Master colon merging like even the the git bash shell is telling us that things don't look good so what do we do we have to go in and fix it so I got to go in and find that file I'm going to edit it and you can see gits telling me hey this is what was in Dev and that's what's in the current Branch where which is the master branch so I don't know how to fix it fix it you know like two people working the same file at the same time right I don't know what is it fabulous is it awesome is it great I don't know just fix the darn thing right and don't have people working in the same file in different branches at the same time like get your team lead to figure things out so I mean you just got to come in here I mean this is code this is a real Mass right cuz you got to worry about compilation and stuff this is a great and fabulous and AES awesome website right so contrl s to save the file now get add to add the file get commit DM fixed it up now that merge is complete and if I do the git log on line You'll now see that those two branches have been merged into one with the new updated file based on the fact that yeah I had to fix things up because people were working on the same file and separate branches so that's what a merge conflict is it's when people work on the same file and separate branches and G has no idea how to merge them together how do you fix it we just got to go into the F resolve it figure it out get those two people together put their heads together and fix the darn thing best way to fix uh a merge conflict is to not have them in the first place well there you go that's your merge conflict you know you can't talk about the get rebase command without talking about squashing commit and taking your git commit history and compressing it a little bit tidying things up making it look handsome you can see I've got a number of files in this repository here and I've also got a number of commits and let's say I wanted to squash all of these commits down so I'm only got a history of a couple of commits here here's how you do you can say get rebase not yeah get rebase dasi do it interactively and I'm going to put in C8 d 4 DF that's the first commit so I'm going to rebase back down to C8 D4 DF and all of a sudden this window comes up and I've got the ability to pick which commits I want to leave alone and which I want to Squish Squash I'm going to leave that second commit but I'm going to pick that for a squash and then I'm going to pick that for a squash as well I'm going to squash the life out of that one oh Fred is not spared so now I squash all of those commits I got to commit this file so I got to click Escape control W to write that file and then colon Q to get rid of it um now colon Q exclamation mark to really commit out of there and now all of a sudden you can see that when I do that git ref log command or even the git log on line command boom all of a sudden we've smashed that git Comm commit history down rather than having seven commits well now we've only got two that's how you squash your G commit history you know here's a a little trick that you know I forgot to mention it's uh it's the G St stash um sometimes you're going to be working in your repository and you'll create a file so what uh cdef so uh I'll touch Fred hope Fred doesn't get uh upset with me touching him like that um I've added a file now I'm going to do a get add command and now if I do a get status you can see that that file is ready to be part of the next commit but let's just say I'm like ah you know what I need to do a commit but I'm not happy with that file I don't want to commit that file but at the same time I don't want to delete that file because there's good work in it right so one of the things you can do is you can stash your changes in git I can say you know what get stash everything that's currently in the index and they'll say okay I'm going to stash it I you notice it takes out Fred and if I do a get status there's nothing to commit I can now do a commit and that file there is not going to be part of the next commit it's kind of been removed from my workstation but it's actually in the stash and at any point in time I can go back and say Hey you know those files that I put into the stash pop that back onto my workspace and so I say get stash poop and look what's going to happen over there boom Fred comes back if I do a get status you can see Fred is ready to be staged if I do a get commit DM and say hey Fred that now gets added to my git commit history and there you go that's how the git stash git stash pop works there's also a git stash apply which you can use alternatively to get stash pop but you get the idea if you got changes that uh you don't want to delete but you don't want to make them part of the commit just do a git stash and there you go that is a a giant overview of git and GitHub and if you were able to pay attention and some of that stuff that we covered in this git and GitHub tutorial makes sense to you then you know you're are becoming very competent and very dangerous with these get and GitHub tools now of course there's more to get and there's more to GitHub but honestly I think what we covered here makes you very effective in the world of git and GitHub I mean you can get into GitHub Branch protection rules and you can get into GitHub actions I mean there's more stuff there and I don't know what other command you we could cover in git I mean there's smudging and stuff but we covered a whole heck a lot of the git commands that you use on an everyday basis and even some that you don't use that often so really I think this was incredibly thorough and you know all you need to do is just get some time behind the wheel and start practicing with Git and you are going to become very confident with this tool and that's so important because this is the foundation to all devops now if you did enjoy that tutorial why don't you head over to the serers side.com we got lots of great tutorials over there on git GitHub gitlab bitbucket scrum agile you name it um also why don't you subscribe on the YouTube channel cuz I'm going to be constantly posting more updates I will say a couple of other things you know if you're interested in me you can always follow me on Twitter @ Cameron M cnz and I've also got a couple of books out I've got the Pickering of Springfield which is all about uh where the uh fictional town of Springfield from The Simpsons originated got a couple of books on hibernate and jpa and I also been working with a A young freelancer named Darcy declute who just published the uh scrum Master certification guide so if you're into agile if you're into scrum and you're interested in grabbing some accreditations I definitely would recommend picking up that book there's links in the description and of course uh I have my newsletter so if you want to keep up with the latest in devops in software development even into the AI space as well sign up for that in the meantime enjoy working with Git and I don't know happy GitHub
Info
Channel: Cameron McKenzie
Views: 30,325
Rating: undefined out of 5
Keywords: cameronmcnz, theserverside, java, jakarta, jakartaee, git, github, gitlab, dvcs, versioncontrol, tortoisegit, gitgui, atlassian, bitbucket, linux, azure, devops, devopstools, gitops, gitopstools, azuredevops
Id: l2yrJtwoC_E
Channel Id: undefined
Length: 113min 19sec (6799 seconds)
Published: Sun Jan 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.