Learn Git and GitLab Tutorial For Beginners | Full Course [2024]

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
git isn't hard to learn and if you've paired git with gitlab well you've just made your life a whole lot easier all you need to do to become very capable with these tools is a little bit of time just give me about an hour or so a little bit of motivation and I know you're motivated because you wouldn't be watching this video if you weren't and maybe a good instructor and if you put all of those things together well you can become very capable with these tools very very quickly hi I'm Cameron mck I'm the editor-in chief at the serers side.com and I've put together this get and gitlab journey for you so you can gain the skills that you need to contribute in a meaningful way to your devop system administration or software development team that's using get and git live if you stick around for just about an hour or so I'm going to teach you a variety of things and I'm going to tell you what I'm going to teach you right now and I might mention some things that you don't quite know about yet don't let that intimidate you because I'm not only going to explain it to you but I'm going to make you an expert in these things things like while creating repositories on gitlab cloning repositories managing commit histories doing git resets git reverts doing a little bit of cherry picking as well I'm going to show you how to use the collaborative tools that git and gitlab provides doing pushes doing pulls doing fetches I'm going to show you how to do isolated development in get using branches I'm going to show you how to create branches how to merge branches how to avoid merge conflicts and how to resolve merge conflicts when they occur I'm going to show you how to do rebases and show you the difference between a get rebase and a get merge and I might I just might even show you how to do an interactive rebase and squash now that's not a promise but I have a feeling we just might get there but before we do we need to make we've got a couple of things set up already so I'm going to ask you to do just two things one you need a gitlab account make sure you've registered for gitlab and you've got your own gitlab account and secondly install git it's super easy to install I've got a number of tutorials on how to install git but you just have to download it click the installation click next on all of the prompts and boom your git installation is done if you've got that done if you've got your git lab account ready and you've got git installed on your local machine well we're ready to talk about what git is what git lab is what a git repository is and how to create those things and get started and that is what we're going to do next now people that watch my videos about G and git lab usually want to get started right away and I kind of feel the same way but I also feel like I can't really start a discussion about get and G without first just quickly talking about what git and git lab is so what is git git describes itself as a distributed Version Control tool and I think that's a good description uh it leaves one thing out and I'll talk about that in a second but being a Version Control tool means that git can take snapshots of your project snapshots of your files over time so you write a little bit of code you're happy with it you create a commit you write a little bit more code you're happy with it you create commit and you end up creating this history of commit which is like a timeline showing the development of your code now the other cool thing about commits is that uh you can always go back to a commit so if you start writing code in the morning and you messed things up by noon well if you did a commit when you started the day you can always go back to that commit and start over again it gives you the ability essentially to travel through time I always compared to a video game right like you're playing a video game and you know you you start the game you get to a save point you play some more you get to a save point you play some more you get to a and then you die you don't start at the beginning of the game again you go back to the previous safe point and that's what commits are like in git is the ability to go back to a previous commit when things were working properly a way to to save yourself when you die now the other thing that git does is it provides a collection of collaborative tools that allow you to share your code with others so I can create a get repository on my local machine and I might have somebody else that I'm working with and I can allow them to clone my repository to get all of the code all of the commits onto their machine from their machine they can update the code and then they can push their changes back to me and then I can get their updates I can get their fixes and then I can push my changes back to them and I can collaboratively do this back and forth so the ability to manage a commit history and to collaborate and share your code with others those are two of what I think are the three most important features of git the third one is the branching features git allows developers to create branches which are isolated spaces that are separate from the main trunk of development where they can experiment take chances break things and if they mess up they just delete the branch and nobody knows that they were doing that they do something amazing they can merge that code into the main branch and then use the collaborative features to share it with other people um and that that that facility to allow developers to to take chances and experiment and be Fearless um just can't be understated enough um so those are the Fe key features that git provides it's open source it's managed by the Linux Foundation um it was actually invented by Linus turval the guy that gave us the Linux operating system so it's got a great and fantastic Heritage now it begs the question what's gitlab well gitlab is that free and open source git tool put onto a server in the cloud that people can connect to and be charged to use it taking open- Source free software hosting in the cloud and charging people to use it is a fantastic business model if you ever get into it um you're likely do well now I'm kind of joking but I'm kind of not because that fundamentally is what gitlab does it puts git onto a server in the cloud and then other people can connect to it this distributed nature of git is amazing but it's also difficult to configure so being collaborative having a central place where your repository is and having the features that gitlab provides like you know failover and scalability that is pretty valuable to an Enterprise but fundamentally that is what gitlab is and it's not dis similar to GI Hub and bitbucket it is git put on a server in the cloud and you can connect to it and interact with it just like you would any other git uh server or git repository now I will also say in defense of gitlab um they also wrap a lot of features around uh their git offering so you know I mean everything from security git doesn't have security built in Branch protection rules is something else they have workflows so with Git locally anybody can commit to the main branch or the master Branch um but you know quite often on a production system whatever goes into the main or Master Branch gets packaged and pushed into production so you want workflows controlling who can push when to the main and master Branch so those are other things that gitlab provides not to mention the whole whole Suite of tools that helps you do continuous integration and continuous delivery of your code um that's some pretty impressive value add as well but fundamentally for for what we're doing we're going to see gitlab essentially as git hosted in the cloud and that is fundamentally the difference between git and gitlab git is the open source product gitlab is the vendor who has built an entire ecosystem of tools around that uh individual git piece of software now that's the difference between git and gitlab the next thing we need to do is actually get into git get into gitlab and create our first repository and that's exactly what we're going to do next now after you've installed git and you've registered for your git lab account the next thing you need to do to get started with Git is create a git repository and you do that in git lab by creating a project when you create a project a get repository is created at the same time now a get repository is a pretty fundamental part of git every time you have a new project you create a new git repository to store all of the commits for that project and a git repository is a bit more than just a git commit history so first of all the git repository does manage and maintain your git commit history it also manages all configuration information about your in environment it keeps track of all of the branches and the state of the code in all of the different branches and a get repository will also keep track of where all of the remote repositories that it can connect to and share code with exist so uh that git repository is a pretty central part of working with Git whether you're working with Git locally or working with gitlab so what I want to do right now is actually go into gitlab and create a new project so I'm going to click on this create project button I just going to create a blank project I'm going to give it a name I'm going to call it my project because I like to project positivity as much as possible it's going to be a public repository always like throwing a readme file in it and then I'm just going to click create project and that will create my git repository on the gitlab server okay and there you go we've got our repository on the server now if you want you can go up here and you can add files I'm going to add a new file so I'm going to click new file here I'm going to add a new file on the server I'm going to call it alpha. txt I'll do hello world because that kind of seems like the requisite thing that you're supposed to do we can do a commit message you know say something like my first commit and then finally click commit and now I've actually committ mitted uh this file into this git repository on gitlab notice the branch name is main when we work with Git locally it usually creates a a default branch called master we're moving away from that so all of the git laab GitHub bitbucket they now create a branch called main instead of master um that may cause us a few problems a little later when we try and bring the two together but now it's just worth noting we've got the main branch and that's probably the way we're going to do it going forward so there you go we've now got got a file on our project it's called alpha. txt you can even see there's a commit ID for the commit every single commit that we have in git has a unique identifier associated with it now we can create files up here on git lab but you know generally speaking you write files and you create files in your local file system right you have a web project an HTML project a python project a Java project JavaScript project you're using an IDE or an Editor to to write and test files locally so we've got things working up here but what I want to do is I want to bring this project down to my local file system now how do you do that well as I mentioned we're going to start working with these collaborative git tools when there's a repository on the server and you want to bring the files from that repository on your local machine you do a clone now in order to do a clone you need to know the unique identif fire that gitlab has assigned to this particular project to this particular repository fortunately it's not hard to do you see that beautiful blue button there that says code just click the drop down look at the https option to clone it click copy URL and then come down to a folder on your file system I got a folder called underscore repos just right click select open get bash here if you've installed git that should be available that be appears after installing git if you don't see that you may not have get installed but open git bash here and then you just say get clone and I'm going to paste in that URL learn gitlab fast is the name of my account my project is the name of the project I'll click enter and keep your eye on that right hand side there because when I click enter boom all of a sudden I get a new folder on my file system it's going to ask me to sign in so I'm going to sign in with my browser right here I'm going to authorize using git cremen manager so that's good we know that all of the authentication happened well by the way uh sometimes if the authentication using ooth isn't successful you may have to create a gitlab token to authorize or you may be forced to do SSH a lot lot of organizations require their users to have an SSH connection I've got separate tutorials for both of those and it's you know it's Nuance stuff so if you're having a problem go check out those tutorials hopefully you're able to log in right away I've got success it's cloning into my project I'm going to open up my project right here and you'll notice boom I've got all the files that are up on that server I've got the readme and I've also got the alpha. txt file now what do you do locally well you add files right you know you you do work I mean this is your repository to to write code to write software to do your Cloud native Computing JavaScript python rust I don't know um I'm just going to add files git can keep track of any files and I'm just using txt files just to keep things SE simple but I'm just going to add a new file right so I'm going to add Bravo .txt so bravo. txt has now been added to my repository um one of the commands you want to get used to is get status and right now if I move into my repository right so right now I'm not in that project I got a CD into that project so CD my project and if I'm inside that project by the way you know you're inside the project that's been cloned if you can see that dogit folder it's a hidden folder but if you've configured your environment to view hidden files you'll see it and any folder that's got that dogit folder in it well that means it's being managed by git but I'm going to do that git status command now that I've moved my command uh uh prompt into the folder into the my project folder and you can see it says bravo. txt is an untracked file so here's something about Git You should know when you create a file in a project that's being managed by git git is kind of hands off it says you know there's a good chance maybe you don't want me to track that file um maybe that's a temporary file maybe it's a scrapbook file maybe it shouldn't be part of the project so if you want get to track any files that you put into your project you have to what we call stage those files or add those files to gits staging index basically tell git hey I want you to to manage and keep track of this file and you do that simply by saying get add and in this case bravo. txt which will add bravo. txt to the list of files that git will include in the next commit in fact if I do a get status command here again you'll see it says hey there's a new file and that's going to be committed the next time you issue a commit so the git index now lists bravo. txt well it's talking about the next commit why don't we do a commit get commit DM that's what the second commit boom all of a sudden we now have our second commit it's in there it's good and if I do a get I don't know do a get log oneline command this will actually show me a log of all of my commits so there was the initial commit when the get rep repository was created on gitlab there was that my first commit which happened when I added alpha. txt and now there's the second commit where I've got bravo. txt now here's something to to note I've got Bravo on my local file system but if I go over to gitlab and do a refresh Bravo is not there so even though I clone from gitlab as I work locally those files aren't synced together unless I explicitly ask for a sync and one way to take my files and push the changes that I've got locally to the server is to do a g push and that's exactly what I'm going to do here um I'm going to open up a command window I'm already in the the the project here I'm going to say get push origin origin is just the name that we give to the server we're connected to so git lab in this case is the origin that's where we originally cloned from I'll say get push origin and assuming all of my authentication is in line those files will get pushed up to gitlab now if I come back to gitlab it looks like it's made me a liar because it's only Alpha do I'm just joking I got to do a refresh so I do a refresh and boom all of a sudden bravo. txt is up on the server so you can see you know if you actually have files on the server or repository you want to get it locally you do a clone you do some work locally you do your commits after adding files to the index you want to push that back to the server which essentially means making it available to other people on your team you do a get push now after a git clone if you want to get future updates what you do is a pull possibly a fetch but we're going to look at a pull right now and so let's say somebody else went into this repository and added a file I'm going to add a new file here I'm going to call it well I'll call it charlie. txt hey Charlie I'm going to call this the third commit commit those changes and now my Project's got these three files in it alpha bravo and Charlie but I don't have Charlie on my local file system so again it's all out of sync so how do you fix that well you go to your terminal window you go to your command window you go to the git bash shell you say get pull now keep your eye on the prize over there see what happens when I do that get pull command boom all of a sudden now we've got charlie. txt on our local file system so you do a clone the first time you want to get the files from a repository in the server your local machine every subsequent time we want to do that refresh you you do a poll optionally a fetch which I'm going to show you in just a moment but let's add another file here I'm going to use the touch command to add d. txt now the touch command is just something I like using at the command line it's just a simple way to create a file at the command line you can see over there boom d. txt gets added if I do a git status it says hey you got to add that to the git index so I say git add if you want to actually add anything new in the current folder you can just do get add Dot and that says add everything in the current folder or subfolders that's new or changed you have to add something to the index anytime you change it or anytime you add a new file but if you update a file you got to add it to the index as well and now let's do a commit get commit DM fourth commit by the- m means message right every commit has to have a message associated with it so get commit what's the message D m4th commit and now that creates that fourth commit for me um but again that's not on the server I can refresh all day long here on the server dvo is not going to go up there I've got to do that get push and push that file up now after I do the push boom all of a sudden we now see d. txt on the server now I did men mention that there was um uh something called a fetch uh when you do a pull what happens is the the updates come from the server to your local repository and then get will look at your your working directory or your working tree all your files that are local and try and put the new files onto your file system sometimes you don't want to do that sometimes you just want to get updates about the changes into your local repository but maybe you're working on something in the middle of something maybe you just want to compare what you've got to what the changes are in that case you can do a fetch a fetch will uh let your local repository know about the latest changes but not actually write those changes onto the files in your folder if you want to actually get them on the onto your folder you have to do a get merge command let me show you this okay so I'm going to add a file to the server I'm going to call it Echo so a new file called echo. txt echo echo echo okay what is this I'm losing track now is this the fifth commit commit those changes again that's now on the server but it's not on my local file system now maybe I want my local system to know about these changes I have that information in my local git repository about the updates on the server but I don't want to disturb maybe the work that I'm doing in my local file system in that case you can do a fetch so watch this I'm going to control L clears the screen I going to do a get fetch and boom it's going to say you know what yeah there's some changes up there I'm going to bring them down right unpacking all just doing all this stuff but you notice that echo. txt is on the server but it's not refreshed on my local file system it's not there so information about the changes has been brought down to my local repository with a fetch but unlike the pull it does not actually write those changes to my local directory now I can I can actually say hey get you know what it's time bring bring those changes in I can say get merge my local workstation with the files that you brought down with from origin so this is the first time we're going to see the merge command you usually see it with uh branches but this is a a slightly different use case for it um I want to merge those changes you got with the fetch into my local file system I click enter and it says get merge not something we can merge so let me just do a get fetch and then a get merge okay so there we go we didn't like the get merge origin uh get fetch and then get merge and now you can see hey we've now added this echo. txt file right down into the file system so there you go that's how easy it is to to to do a Fetch and then bring that code down I guess I didn't need to put origin in there um so there you go that's kind of how that works so again when you're working with Git and you've got a repository in the server you want to bring it locally you do a clone after that you add files to your local repository you do a git add you do a git commit and then you can do a git push to send those files to the server now by the way there's actually a way to simplify things if uh you've edited a file so for example like right here if I've got echo. txt and I want to add another Echo so I've made some changes get status says there's a couple of changes that you know need to be staged first and then committed you can actually do a staging and a commit at the same time when a file's changed you can't do it with new files you can say get commit Das a-m updated Echo and now when you do this it's going to do a commit and it's also going to Stage any files that have changed since the last time a commit happened it won't work for new files but for any files that have changed right I updated Echo it'll actually do the uh get ad and the git commit at the same time so that's a little trick but yeah after you've done that then you get push and send your changes to the server maybe changes have happened on the server if that's happened you do a get pull that will take the changes on the server and bring them right down to your local file system alternatively if you don't want to overwrite what's on your file system right now you can do a Fetch and then after the fetch you can do a git merge a little bit later on but there you go those are the collaborative tools that we use with with Git and gitlab that's how we share code make changes push changes to the server get updates from the server and just work collaboratively with others it's pretty awesome okay in the previous tutorial we started off with a gitlab repository cloned it worked with it locally and then pushed back to the server I want to do the opposite this time I want to start locally and then do a bunch of commits do a bunch of changes and then push those changes back to the gitlab server so take an existing project and then push it up to gitlab now in doing so I also want to introduce you to some other important commands that you need to know if you want to be effective at git and gitlab and so I put together this presentation here to kind of talk about and step us through some of the important commands we use when we're doing local development with Git so anytime you want to work with Git You have to create a repository every project that you work on should have its own repository we saw how to do that over on the server with gitlab if you want to do it on the local file system you just create a folder folder where all your project files are going to go and then say get in it when you do that get init command what happens is in the folder in which you issued that command you'll see a hidden doget folder be created now it is hidden so if your operating system isn't configured to show hidden folders you won't see it but there's all sorts of interesting Plumbing in there about how git is configured in branches and commit history again we don't want to spend too much time in there but it's interesting to know that it is there and that is essentially the the bite code embodiment of your git repository now once that's created what we do is we start adding files to that repository I like using the touch command from the terminal window the touch command if a file exists with the name that you're trying to touch so touch alpha. txt it'll change the timestamp on it um if the file doesn't exist it just creates a new one and you don't have to use tou to create a file but it's just one way to do it at the command line but if I was to issue that command well a new file would get created just gets put onto the file system right next to that git folder now if I want get to track that file I have to add it to the index so I have to do a g add alpha. txt command and that then moves the alpha. txt file into the git index I shouldn't really say moves it into the git index is just a file that keeps track of all of the different resources that git is supposed to keep track of itself but doing the git ad command stages the file adds the file to the git index adds the file to the git staging index now when you're happy with all the files that are part of that git staging index you can do a commit now in a lot of these examples I do one commit after adding one file but you can add 10 files and update files and edit files and delete files and add all of them to a single commit I'm just doing kind of one file one commit at a time just for the sake of Simplicity now we do that commit we create that first commit and now we've got a commit in our commit history you also notice there's a commit ID there every commit gets a great big long hash code unique number to uniquely identify it on that good old git blockchain that you're maintaining locally now after that you just keep adding files and updating files so touch Bravo txt adds the file then you do the get add Bravo txt so that it's now tracked by the git index and then you want to do a commit and then you say get commit dasm second commit and now we've got a second commit and that second commit is now including Bravo in the snapshot and our file system is essentially the sum of all of the git commit histories put together okay so let's now create another one so you know where this is going right touch charlie. txt that sounds a little unchristian but um we're not going to I'm leaving it at that get add charlie. txt to add charlie. txt to the index then we do the git commit and our git commit history keeps adding up and moving on and on now as I said I'm doing one commit one file each time here but you know we could go in and edit alpha. txt so you can edit something at the command line you can just do this command Echo High greater than greater than alpha. HTML I don't mean to confuse you by command line commands that does the same thing as opening up the file typing high in and saving it right just easier to show it at the command line like that but that would update alpha. txt it's not a new file but it's an update then at the same time we could add dvo are we not devs um then add dvo and alpha. txt to the git index notice right there I did get add dot the dot says add any anything that's changed or anything that's new that's in this folder in which the command is being issued and subfolder so it's like a way to do it all real quickly and then once you do that well after you've done that add you do a commit and then all of those files now get added into that next commit right and then our local file system is just the summation of all of those commit histories added up and so that's sort of how a get commit history works now the other cool thing is we can start doing really cool operations on our history so for example maybe Charlie and alpha. txt change and the d. txt aren't working for us and we want to go back in time well we could say get reset Das Das hard because it's never easy and give the commit ID of bravo. txt and if we issue that command a couple of things happen one is the the the git commit history moves back to Bravo txt uh to where that commit happened ID 26 f149 um and all of the changes all the files that uh happened and all the changes that occurred after that commit will be reversed out um so we issue that command boom we end up going back in time and those files get deleted in fact the change to alpha. txt that happened in that fourth commit would be removed as well so probably should take away that star from there because it's actually going back the way it was right at the beginning now by the way it's dash dash hard dash dash hard means don't not only move the the the pointer to of the commit current commit history back to bravo. txt but also clear the index and change all the files on the file system as well the the hard does it right it does everything at once but that's the the idea of managing your git commit history history how you can maintain a git commit history and also how you can start manipulating that git commit history with commands like git reset now that's a little bit of theory let's actually do this example in code and see exactly how it works okay let's put that theory into practice I'm going to create a new folder on my file system in theore repos folder I'm going to call it history I guess because we're going to be dealing with a git commit history the idea of course is you know these folders are going to are representing projects it could be a python project a web development project a Java project I don't know um but you've got some project you want to track its files so what you would do is you would create a folder for the project and then you go in and you open the git bash shell the Born Again shell I guess bash includes the word shell so saying it again is redundant is redundant but um whatever old habits die hard um and then I say get in it right because it's better to get in it than to get out of it and when I do that you'll notice that dogit folder gets added over here I might even view this and do some large icons there if I go in there you can see head description config remote references objects this is where all of your git commit history or information about how to connect to remote repos what the different branches are what's added to the index all of this information is stored in here this is often referred to as the plumbing and we don't want to get too involved with the plumbing we want to stick with the porcelain not the plumbing so it's good to know that that's there that's what gets added when you do a g in it but we want to just focus on I don't know building our application so how do you do that well you come in here and just create a new file I'm going to create a file called alpha. txt right keep that common Trend right and do a get commit um well I can't do a get commit I got to do a get ad right get ad. get commit DM first commit right there we go now we've got a commit in our git commit history if I do a git log das-1 line you see there's really only one commit in our history even create the repository wasn't registered as a commit we need to create a little bit more of a rich commit history here so I'm going to I don't know I'm going to touch bravo. txt that creates the file boom you see it there then get add Dot and then get commit DM and I'll call this what is the second commit so you create the file you add the file to the index so that it staged so that git knows to keep track of it then you do a commit and that takes a snapshot of the file and permanently saves the state of that file as part of that commit in the git commit history um I'm going to do a couple more uh so I'll touch charlie. txt G add. gomit DM that's the third commit by the way contrl L clears the screen and why don't we touch d. txt get ad are we not men are we not devs and then get commit DM forth commit there we go we've got a nice rich get commit history here and uh one command I really like uh they call it git log a dog um so git log gives you a a log of what's happened I don't like git log because it's busy right like this doesn't look good to me um so no thank you col and Q to get out there get log d-1 line look at that looks beautiful there's also git log Das Dash all decorate that'll put some asteres next to it-- one line-- graph um it doesn't look that great right now by the way one of the things I don't like about git is you have to type the commands in corre correctly um but uh that's called git log a dog because it's a a momic right a d o all decorate one graph a dog get log a dog it looks better if you've got a Divergent commit history but you get the idea we've got a nice git commit history here now one of the things we can do after we've created a git commit history is we can manipulate a little bit so let's say everything messed up after Bravo and I want to go back to the way things were at bravo. txt well you can just do this you can do a get reset oh you know what I need to do I do need to get ref log ref log is like log but it shows you just what's kind of configured local it might show you some stuff that uh others might not see if you shared your code on the server um I also like the format of reflog a little bit better but you can see that second commit has a commit ID of 6 d80 c52 so I can say get reset D- hard and let's reset back to 6 d80 c52 68 I'm just double check in now keep your eye on the prize Watch What Happens over in the files and the file system when I click enter boom all of a sudden Charlie and dvo are removed I've now reset myself back in time if I do get log on line it looks like those commits are gone right the log saying I've only had two commits the first commit and the second commit and you're like no there's a third and fourth commit but the log's not showing it right we reset back to that second commit and as we've done it the tools actually manipulated and managed our file system by deleting files off the file system now I mentioned that get ref log sometimes shows things that uh maybe get log doesn't do get ref log it actually shows you that fourth and third commit get doesn't get rid of stuff once it's in Git it is in git practically forever it is not easy to purge stuff from git I assure you um so that fourth and third commit are there the git log doesn't show it the git log is kind of what other people would see if if they cloned your repository get reflog is is all the dirty history that you've got in there it's the stuff you might not want people to see now the question I often get is well are those commits gone forever right like what if I now decide you know what it was actually Charlie I wanted well you could just do a get reset D- hard back to the third commit which is 12C 5 Deb click enter keep your eye on the prize boom charlie. TX he comes back maybe you say you didn't want to reset at all so you do get reset Das Das hard 2bb 5510 and now we're back to the fourth reset again right the the fourth commit again alpha bravo charlie dvo see we're just quickly manipulating our our commit history like it it's amazing like you manage those commits and then you've got the ability to go back to any commit uh in history when you do that so super cool now that was the get reset command there's another command that people confuse with G reset called revert and I want to show you what revert does now with the reset we reset back to Bravo and when we reset back to bravoo it removed all of the files that happened after that Bravo commit so it removed Charlie and dvo so get ref log let's take a look now get revert people think get will do the what get reset does but get revert what it does is it surgically goes into the commit that you give it and removes the changes associated with that commit but it doesn't affect commits subsequent to that commit it doesn't affect commits prior to that commit so for example on the second commit we added bravo. txt when we reset back to bravo. txt it got rid of Charlie and devvo but if we actually just revert that commit it'll actually get rid of bravo. txt watch the second commit is 6 d80 c52 so I'll do a get revert and I'm going to revert 6D 80 c52 it's going to ask me to provide a get commit message because when you do a revert there's actually a commit that gets registered in the git commit history I'm just going to click uh colon Q to accept the default and get out of there but you can see boom all of a sudden bravo. txt is gone so the revert actually surgically removes information from the individual commit I could even go get revert let's get rid of that alpha. txt so what was the first commit that was 6 c733 1D no commit is safe when I'm behind the keyboard again Cole and Q get out of this nasty Vim editor by the way you can set notepad++ as the editor to come up there and instead of doing colon Q to get out of it you can just you just close notepad or notepad++ it's way easier that way um but the default is vim so I'm using that in these examples but now you can see you know we reverted that first commit now revert surgically removes a commit there's actually something called a cherry pick and what a cherry pick will do is it will will bring a commit back it will take what happened in a commit and pull it into your branch that's usually something you use across branches but I'm going to show it to you here we reverted a couple of commits maybe I want to cherry-pick that first pick that first commit back so I can say get let's cherry pick that first commit that commit 6C 7331 D I believe that was the first commit and git will say okay I'm going to create a git commit message so you got to provide some info but notice my repository just Charlie and dvo I'm going to cherry-pick that first commit and boom all of a sudden alpha. txt comes back actually thought it was going to ask me for a commit message and it didn't so life is good um and if I actually take a look at the git log one line you'll notice that uh the reverts are listed in there um it's ALS so uh the first second third and fourth commit are listed there and it looks like it uh when I do that cherry pick it lists the name of that commit as the message for that cherry pick so that's kind of interesting as well I actually thought it was going to ask me for a an actual message um yeah so there you go that just kind of gives you a good idea and a good overview of how all of these different commands work when you're you're working with Git now we've got all these files on our local file system but they're not tied to a repository on gitlab and that's what we want to do right now we want to be able to to push our local project up to gitlab and create a repository for these files up on the gitlab server so how do we do that well we head over to gitlab and we create a new project I'm going to create a blank project and I'm just going to call it my- history now I'm not going to put any files in it I'm not going to initialize it with a readme I'm not going to do anything like that just create a basic project I'll make it public just to make life easier but this will work public or private I'm going to create this project and then it's actually going to say you know what there's nothing in this repo so if you actually want to you know connect an existing project this is what you need to do and so it gives us the commands here it says okay make sure that the initial branch is main I'm going to come over here notice that the branch is master get status you'll see on Branch Master we actually want to use main because that's sort of the default for gitlab so it gave us this instruction to to do get in it-- initial Branch equals main oh that's not what we want that's not going to work um I think what I need to do is say get Branch Das uppercase M Main and now that will actually change the name of my local branch from Master to main so that command wasn't up there but uh I had that in the back of my head now after you've got you know the branches on your local uh repository consistent with what's on the repository that we just created we need to tell our local environment about that remote server we want to push to and that's called adding a a remote reference to get and that's the command here get remote ad origin git lab blah blah blah blah blah and I come over here and I paste that in now by the way it's get add origin um instead of origin you could actually just say get lab origin is the the convention so origin kind of represents you know the server that we originally cloned from us usually uh the server that we're pushing back and forth to we usually give it the name origin but that's just a name that's made up we could say get lab and the next time we push we go get push gitlab you next time we pull get pull git lab so I'm going to call it gitlab you can leave it origin if you want but um that should do the trick now what's the next command it says do all of your commits and then finally do this get push but set the Upstream branch that you're going to push up there to main now I didn't call it origin I called it gitlab so get push D- set Upstream gitlab main so push that main branch up to the server click enter hopefully all of your authentication is in order it says a new Branch was created we've now pushed up to main I'm from Missouri I want to see this so I'm going to click on the my history project do a refresh and there we go boom all of a sudden Alpha Charlie and dvo to match Alpha Charlie and dvo which are there on the file system I successfully pushed an existing project from my local file system up to gitlab after doing all of those get revert get reset get cherry pick get in it get status get log and get ref log commands so there you go I told you get and git lb isn't hard to learn just got to give it a little bit of time be a little motivated be a little patient run these commands get some time behind the wheel and it's all going to come into place okay that was awesome the next thing that we're going to talk about is one of my favorite parts of git and git lab and that is isolated development in branches that's 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 a 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 git 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 New Branch so here's one command get switch D cev 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 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 git 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 developer could delete those files and delete the development branch and nobody's the wiser um while the developer 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 get 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 on to main You' don't only see alpha. txt cuz the Marain Branch only has alpha. txt it's that other Dev Branch that'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 git 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 thing 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 on to 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 Bravo and Alpha in it now what's in Dev we merg 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 have 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 Dev so there you go that kind of helps you visualize 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 let's demonstrate branching and merging on the file system with a new project I going to create a new project here I will call it Acquisitions cuz we've got mergers and Acquisitions happening here and I'm going to open up the git bash shell and just type in git in it because again it's better to get in it than to get out of it that gives us our git repository and what I want to do is well I'm going to start off by just creating alpha. txt adding it to the git index and doing a git commit dasm and call it the first commit right okay so now we've got a commit on our Branch we're on the the master Branch the master branch is the only branch that we have right now um so why not create a new Branch I'm going to click contrl l to clear the screen and say get branch and call the branch Dev so then that now creates a branch called Dev I'm still on the master Branch um if I did get Branch d-list you'd see there's a Dev and a master Branch to move on to that Dev branch I can say get switch Dev and boom now I'm on the master Branch now by the way I think in the example I did get switch dasc Dev which switches you on to the dev branch and creates one at the same time but there you go uh six of one half a dozen of the other but I'm on the dev Branch right now and if I do an LS command you can see that I've got the alpha. txt file and you know you just look at the file system there's our our folder alpha. txt so let's add another file let's go and add what have we been doing lately we have been doing a touch bravo. txt so now we've got Bravo get add dot get commit DM that's the third commit right okay now we've got Bravo over here let's do a touch Charlie sounds unchristian but here we go get add get commit dasm that's our one two is that our third commit I think I got the third and fourth commit so I'll call that the second commit although that's not the second commit um get log on line is going to be confusing as anything but you get it the second and third commit happened on the dev Branch although that happened out of order the first commit was on the master branch um now we've got alpha bravo and Charlie on that Dev Branch but what watch what happens if I get switch back to the master Branch what do you think is going to happen so keep your eye on the prize boom all of a sudden Bravo and Charlie disappear they're still in the G repository they're just on that other Branch but that branch is isolated from the main branch and let's just say I don't know maybe the main branch had a fix come in so touch d. txt that's the fix always is are we not devs get add. get commit DM that's the fourth commit that is the fourth commit okay and now if I do a get I know ref log we can see the history um more importantly if I do get log d-1 line d--- all d-1 line d D decorate D- graph it should be all decorate on line graph git log a dog that's the momic a d o that's how you remember it but when I ask it to do a git log and kind of create a graph you can see you know there's that first commit where Dev and master were pointing at the same thing then Dev splits off to that second and third commit and then the master Branch branches off to that fourth commit and now we're in a situation where we need to bring all of that together so how do you do it well I'm on the master Branch I've only got Alpha and dvo as you can see I want to bring in those other two files how do I do it will I say get merge Dev and that's going to merge Dev into the master Branch that'll bring in Charlie and Bravo I click enter boom you can see those files added I do colon Q to accept the default get commit message and exit out of vim and boom all of a sudden we can now see I've got those files if I do a git log a dog again notice how we've got that merge point so you can actually see how we've gone from uh the the files and Dev have sort of merged into that Master Branch but at the same time notice it says Dev is still only pointing at that commit that has Charlie and Bravo if I do get switch and move onto that Dev Branch dev's not going to have dvo boom dvo goes away and that's because uh we merged Dev into Master we never merged Master into Dev right but we could I'm on dev right now get merge Master nothing says we can't right boom now we merg that in we've got d. txt on our file system as well let's see if we can do that get log a dog right now okay and there you go now it says that Dev and the master Branch are both pointing at the same commit previously Dev and master were pointing at different commits now it's pointing at the exact same one that means they're in exactly the same state and so there you go that's how you create branches in git how you switch between branches in git and also how you merge branches together in git when they potentially get out of sync now I just demonstrated how to create branches and merge branches together using Git You can actually do all of this up on gitlab as well now you don't usually code your entire project on gitlab online but a lot of times you'll get branches in and need to merge them together and that's what I wanted to demonstrate right now I'm going to go on to gitlab and I'm going to create a brand new project and I'm just going to create blank project call it Olive because we're going to deal with branches Olive branches I don't know it'll be private maybe have a read me in it I'll create the project and boom all of a sudden we now have this nice simple project created for us I might even go so far as to add a file to it so I'll click this plus button create a new file you know what I'm going to create I'm going to create alpha. txt do the commit on this file and boom all of a sudden we've got alpha. txt as part of our Olive repository but we've only got one branch right so we've got the main branch right there uh but you can create branches on gitlab I'll just say new branch and I'll create that Dev Branch because there we are boom I'm on that Dev branch and I'm actually going to copy this URL move it into this second window so you can actually see we've got the main branch on the left hand side and the dev branch on the right hand side and right now they're both pointing at the same commit 88 F6 cc70 they're both in the same state right but let's go over here and add a new file onto Dev so we'll call this bravo. txt Bravo and then do this commit I'll accept the default messages here and now if we look at that Olive project and we look at the dev Branch we've got Alpha and Bravo here but if I refresh over here on Main only got Alpha right the two are out of sync and I can make this worse I'm G make this a lot worse alpha bravo charlie Charlie wake up Charlie going on an adventure Okay so now the dev branch has uh two files in it Bravo and Charlie and the main branch only has one so we are out of sync even I can do refreshes all day long it's uh not going to help out and I can make it even worse over here now over on the main branch I can create a new file and we'll call this d. txt are we not devs and over here we'll do the commit and now like as I said we've now got that situation which which I characterized before where we've got two branches the main branch and the dev Branch uh the main branch has d.o. txt which the dev Branch does not and the dev branch has Bravo and Charlie which the main branch does not and now we have to consolidate them um we're even getting messages here right hey you push to Dev just now do you want to create a merge request so the tool is actually telling us you know what you are out of sync and you're different branches of different code in them that's not the end of the world and that's not necessarily a problem right like that's just part of of normal Branch based development in git but a time does come when you need to merge them together and uh that's exactly what I'm going to show you next so as you can see Dev and Maine are at of SN and I need to take the files from Dev and bring them into main so when we're working with Git all we had had to do was just do git merge but when you're working with a tool like gitlab there's a workflow and remember there there is the possibility that when you're working with gitlab the stuff that gets merged into the master Branch may be part of a continuous integration or continuous deployment pipeline that automatically pushes into production so uh you don't want to give everybody the ability just to merge into the the main branch typically what happens is Junior devs have to create a merge request and so so I've gone and opened full screen here so I can see this merge request option and I'm going to go in and create a merge request so I click on merge request it says do you want to create a new merge request camera I'm like yeah I do I definitely want to create a merge request well says what do you want to do well I say I want to take that Dev branch and I want to merge it into the main branch so that the main branch is going to get all of those files in depth I then click compare branches and continue this just going to going to make sure that this is possible sometimes you get conflicts everything looks good here I'm going to say let's create that merge request so the merge request gets created and now there's a a merge request that goes out it might actually go to a senior developer now I'm the only person on this project so the merge requests go back to me um but somebody else who might be senior uh will look at this and say yes you know this code looks good I approve this uh and then we'll merge it or they might reject it and say no no no no there's some bugs in there it's not quality enough I'm happy I want to this merge to go through there's a beautiful blue button there that says merge I'm going to click merge and now all of a sudden all of those changes that happened from uh that that happened inside that Dev Branch are now going to go onto that main branch so let's head back to the Olive project I'm going to head over into my main branch and now you can see alpha bravo charlie and dvo have all been merged into that main branch now however again if uh we were to go in and take a look at that Dev Branch um that Dev Branch wouldn't have all of the same commits in it although actually here um as part of that merge request we also deleted that Dev branch and by the way that's something that you typically do with topic branches right after a temporary branch has been used and you've merged all of the code in may as well go and delete that temporary topic Branch because you've got all the changes no point having that ugly Branch history and Branch structure floating around now there is one way to to get around that and that is to not merge code but to do a rebase instead a lot of people people get intimidated by the git rebase and prefer the git merge I'm telling you a git rebase isn't that difficult to understand and that's exactly what we're going to do next 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 super fluous merge commit right no work happened in that merge commit 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 it 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 Maine off that first commit when we first create the dev Branch Maine and Dev actually are pointing at the same commit they're both pointing at that first commit it's not until we do that second commit exclusively on the dev Branch where they get out of sync when we do a a rebase and we were're on the the main branch and we say let's rebase Dev onto Main what we do is we take the commits on that Dev branch and 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 Main and Dev both for the first commit and the second commit are the same we then get the files from Dev pushed into Main 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 onto the main branch so for the first commit second commit and third commit well Main and Dev are exactly the same we don't have any Divergent Dev branches and you can see actually the files in Main 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 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 Maine and Dev are the same all the way through to that third commit and in fact if you looked at the files in Dev you would see that the files in Dev are 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 rid of the Superfluous commits all by doing a get rebase instead of a get 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 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 uh we've got all of the files in Dev I actually open up a a new git bash shell here just so that I can do the git log all on line decorate graph get log a dog actually get log a get 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 rebase it's just a way to to keep your commit history clean okay let's take a a look at how a rebase would work in an actual repository I'm going to create a new folder here and I'm going to call this folder linear because we want to create a linear history instead of doing merges and I'm going to do the requisites here so I'm going to open the git bash shell and I'm going to say get in it to initialize a repository and I'm just going to put that first Alpha Dot HTML or Alpha dot yeah make it HTML file in there so now we've got one file we need to commit get add dot get commitm first commit okay and you know where we're going here now at this point I've only got one branch master I can say get switch DCd which will create the dev branch and put me onto it I can even say Dev git Branch all you can see I've got the dev branch and the master Branch the star indicates that I'm on the dev Branch but right now the dev branch and the master Branch are the same right they both got Alpha but let's change that let's add beta. HTML add that and commit it okay and then let's touch Charlie dot HTML get add dot get commit DM third commit I got the order right this time okay now let's switch back to the master Branch get switch master and notice when we switch back to the master Branch beta and Charlie are going to disappear boom they are gone now let's really mess things up let's uh add dvo do HTML to this particular Branch to the master Branch get add get commit DM fourth commit okay and now we've got a real mess of a git commit history git log uh what is it a dog all decorate on line graph shows that uh well we've got the dev Branch with a couple of commits we've got the main branch with a couple of commits and we want to bring them all together now previously what we would do is we would say get merge and then that would maintain that Branch history um and add a new commit instead I'm going to say get rebase Dev and now this is actually going to take Dev put it on top of the main branch put the commits from the main branch on top of Dev which is on top of Main and create a nice linear history watch this get rebase successfully rebased you can see that extra files boom were added to the folder there so now that Master branch has got all of the files from that Dev branch on it and if I do that get logging of a dog you can see that that disjointed history is no longer there everything has been brought back together you'll also notice that that Dev branch is a little bit out of sync with the master Branch so the dev Branch still only points to that third commit so if I come over and I say get switch onto Dev well you notice that the dev Branch doesn't have everything from the master Master branch and that's because we rebased Dev onto the main branch onto the master Branch we didn't uh rebase the master Branch onto death so at this point in time we still have Divergence between the uh different branches master and Dev dev has three files master has four but we're in the state that we want it to be in which is master has all of the changes from Dev inside of its folder it no longer has a Divergent brand history and there's no Superfluous commit for merges and there you go that's how a get rebase works you know you can't talk about the git rebase command without talking about squashing commits 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've 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 D4 d 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 reflog command or even the git log on line command boom all of a sudden we've smashed that g Comm commit history down rather than having seven Comm well now we've only got two that's how you squash your get commit history 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 get add. get 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 on to 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're going to go and open up alpha. HTML and of course on the master Branch we haven't made made any edits to al. 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 dot get commit DM fabulous okay and uh now maybe I want to merge everything together I'm going to do a git log-1 line-- decorate D- graph D Dash 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 go in and find that file I'm going to edit it and you can see GS 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 on 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 mess right CU you got to worry about compilation and stuff this is a great and fabulous and 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 mer mer 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 in separate branches so that's what a merge conflict is it's when people work on the same file in separate branches and git has no idea how to merge them together how do you fix it we just got to go into the F and resolve it figure it out get those two people together put their heads together and fix fix the darn thing best way to fix uh a merge conflict is to not have them in the first place there you go that's your merge conflict okay let's put all of these pieces together and add a few more pieces and kind of replicate what might happen the first day on the job when you go in and are given a git repository to work with well we're first going to need a a git lab repository on the server so let's say there's an existing project and I don't know maybe you're working for the CIA in Black Ops and you've created a project called Black Ops they've created one for you it's got to read me some files in it who knows we'll create that project so you're going to get onto a a project and I don't know the team lead's probably busy and you're going to say hey what can I do and the team lead will say just you know uh here is the URL for your gitlab repository just go poke around there see if you can find some bugs and fix them so you type in that g repository there you look for this blue button that has the gitlab URL on it you copy that you go to your local repository and well you clone it right so you open up a a get bash window you say get clone you clone that repository and now you bring everything from Black Ops down to your local file system okay now right now if you were to actually go in here and take a look you would see that you're on the main branch and you're going to get fired if you start doing work on the main branch so you got to create your own topic Branch right a temporary branch that you do your own work in that probably you're going to delete after you're done with we refer to that sometimes as a topic Branch so um let's create a new Branch so it is get Branch Das C to create and to create that Branch uh what we call just call it Dev again right we'll create a Dev Branch right we're kind of just following a pattern here to to reinforce these ideas now you notice we're still on the main branch we'd have to say get checkout Dev in order to move onto that Dev Branch now check out's an older command it's been replaced by switch checkout kind of uh is used for SVN and older version control tools where you would check something out and no one else could access it cuz files would be locked we don't have Concepts like locked files in git I think they had the the checkout term to make it easier for people that were using older version control tools to adapt but really we just use get switch now that's the preferred one but I thought I'd show that to you because if you look at some older tutorials you see that used all the time the point is it achieved what I wanted I am now on that Dev Branch a separate branch and now I'm just going to add a new file to this branch and I'll say you know touch Alpha do I don't know make it a python file right alpha. python maybe beta. moojo Mojo is the new programming language that's going to replace python I'm really big on Mojo got a bunch of tutorials on those on my uh uh YouTube channel so check them out um and now we'll do a get ad do a get commit DM and say that's my first commit and there we go now we've got a little bit of a commit history and some files added to the repository who knows maybe we fixed some bugs maybe we created some amazing Mojo or python algorithms and we want to show this to the boss right we want to show them how great our work is so now we have to push this back to the server um a couple of things so we don't want to merge this in main we want the the the the tech lead to do that um but we do want this Dev Branch to go back to the server now it's actually going to be a problem because uh it's one thing to clone a branch and then push back to that Branch it's another thing to clone a repository and then create your own new branch and then push that back so watch if I do a get push origin here it's going to say h you know what um yeah uh you're on a Dev branch and the only Branch we know about is Maine so you're going to have to do things a little differently this first time around and I'm like okay I can do that so get push origin head colon well Dev is what I want to push up I'm going to type that in it's going to say okay we can do that that looks good so now we've actually pushed this new Branch up to the server okay well let's head back to the server so we do a refresh here and we can see that well we've got the main branch and there's also a Dev branch so the tech lead's going to be very happy that you weren't working directly in Maine you at least made it to lunch today so congratulations on your first job now what do you want to do well you want the boss to see your work right so maybe you're going to create a merge request and you can actually see this option to create a merge request right up here if that option isn't available to you you can always click over here to create the merge request as well and so I can come over here and say I want to create a new merge request what do I want to merge well I want that Dev branch of mine to be merged into that main branch now again I could have done this on the command line and then push the changes to the main branch up to the server if there's Branch protection rules that may have got rejected if there's no Branch protection rules and they're continuously deploying applications from the main branch and I put a bug into the system well I'm not going to be invited back for a second day so you do these mer merge request so I can do a quick comparison if I want make sure that there's no errors in there you notice you can assign uh people to the merge request you can assign reviewers I'm not going to worry about that I'm just going to click create merge request notice you've got the option to delete the source Branch when the merge happens I'm going to deselect that because I'm not necessarily interested I want to keep that Dev Branch around but I'll create that merge request and now the next time the team lead or whoever logs in or whoever got assigned to that merge request comes in well they're going to open up their tool and they're going to see hey there's a merge request that's waiting for me so they'll click on merge requests and they'll take a look at all of the merge requests that are open for them they'll click on the merge request and then I don't know maybe they'll take a look at some of the commits dig around a little bit see what happened there and then finally if they think that it's good well they'll approve it and then merge that into the main branch potentially and then all of a sudden now if we actually go back into that main branch boom we'll see the main branch has readme but it's also got the alpha Python and the beta Mojo files in it as well and there you go that is the basic round trip right you clone your repository um you add some files you stage them the git index you do a git commit when you're happy with your git commit history you then push back to the server maybe even having to worry about that that Upstream that new Branch being pushed up to the server and then maybe you do a merge request and then your boss takes a look at the files and hopefully merges them into the main or Master branch and in fact you might even be using something like git flow where you actually don't do a merge request into to the main branch there's actually might even be a buffer branch like you know a specific development branch that your topic branches go into as well I've got a couple of other tutorials on git flow and GitHub flow and get lab flow if you're interested but I think that's beyond the scope of this but there you go that's a bit of an overview of how you would use gitlab from the server to the client back to the server with some workflow wrapped around that gitlab pull request it's all looks pretty cool Sorry get laab merge request that's what they call it pull request we see more over on the GitHub side there you go that's kind of how a round trip works you know here's a a little trick that you know I forgot to mention it's uh it's the get 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 git stash everything that's currently in the index and they'll say okay I'm going to stash it 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 pop 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 G 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 so there you go that git and gitlab tutorial gives you a whirlwind tour of how these Technologies work together now admittedly that's not everything that get gitlab has to offer I mean with gitlab there's Branch protection rules there's security you can even get into continuous integration and continuous deployment with their devop tools but of course you know that's beyond the scope of just working with with Git and git lab together and I mean I guess there's other stuff with Git too but you know we covered a lot of that git porcel I mean I guess you could talk about get smudges and stuff like that but I think we covered everything that you're really going to use on a day-to-day basis with Git and even a lot of commands that you may not see too often now if you did enjoy that tutorial a couple things I would say one subscribe to my newsletter I've got a newsletter where I keep you up to the the latest on git and devops and containers and artificial intelligence so the Link's in the description so please sign up for that also if you're interested I've got a number of books over up on Amazon I wrote one called Pickering Springfield all about uh the origin of the fictional city of Springfield in The Simpsons and also I've been working with a young freelancer named Darcy de Clute and she wrote this really awesome scrum Master certification guide I can't recommend it enough so if you're working in scrum or you know somebody working in scrum and they're interested in gaining a scrum Master accreditation that book is going to make it happen so again links in the description if you're interested in my personal Antics I would encourage you to follow me over on Twitter at Cameron M cnz and also if you enjoyed this tutorial please recommend it to some of your friends I work really hard on this and I'd like more people to just get passionate about git and git lab through this tutorial finally I mean you're probably watching this on YouTube um if you are subscribe and I am the editor and chief over at the serers side head over to the serers side.com we got lots of great content on git GitHub git lab bit bucket devops agile scrum Java Mojo artificial intelligence and machine learning you name it but in the meantime have fun working with Git and happy G lab
Info
Channel: Cameron McKenzie
Views: 6,827
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: NK2BrGpA9wI
Channel Id: undefined
Length: 104min 8sec (6248 seconds)
Published: Mon Jan 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.