Everything You'll Need to Know About Git with ThePrimeagen | Preview

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi my name is the prime engine I have a beautiful wife four kids and I've been a professional software engineer for over 15 years I created this course everything you need to know about git because git is the single most important tool to any software engineer in their career in this course we start with the basic commands of course that is Push Pull commit add then we go into some of the more deeper and lesser known features of git including some of the config op options around re re re how to properly rebase and even exploring ref log and restoring deleted code by the end of this course you will never run into another git problem you can't solve this is pretty much the 80% of you'll do with Git if this is all you ever learn about git you'll be moderately successful like you can get pretty dang far knowing about get ad path to file or a a file pattern get commit dasm and get status if you can just know those three commands you're already doing pretty dang good okay so get uh get add will stage the file it will add it to the index get commit will take your staged files your index file and then turn it into a commit with your you know the author name the time the contents of the change all that and will produce out a sha did you know that Shaw stands for secure hashing algorithm isn't that kind of disappointing like it's a variant of md5 either way it just it just is always disappointing to know that sha means secure hashing algorithm uh and then get status will just say here's the state of your repo all right problem uh we want to trace the steps of git from untracked to tracked so here's this don't forget these ones right here this is what I want you to do I want you to create a file called first. MD I want you to check the status of git then I want you to add first MD to the staging area recheck the status see that it's being tracked commit it with a friendly message and then check the status again if you're pretty good with get already you already know these basic commands uh please just uh just create a commit really quickly with first. MD we're going to use it we're going to do something all right I'll start doing it right now so I'm going to jump over here I'm going to go to uh first. MD I'll go hello fem there we go get status you'll see right here untracked file git has no idea anything about this file first time it's ever seen it it doesn't have any information about it if I I go get add this and then redo it you'll see now we are tracking it it's officially a part of the git ecosystem but only on a singular level meaning that it's only a part of the staging it has no prior information besides for staging if I go get uh commit DM uh friendly message look how nice that is it's going to tell me hey this is the first commit of the repo the root commit it has no parents typically I usually go with Batman for my first commit I think it's funny um because Batman doesn't have any parents right it's like you got to always go for that on the first repo I think it's fantastic um and there you go so we've made our first commit so that means if I go get status says right here you're what branch you're on nothing to commit there you go the tree is clean so we already kind of walked through all that so hopefully everyone has one commit I think even in here I do use uh I use my first commit okay so the changes are in the repo uh one of the common activities you should get into the habit of or at least know it's available is get log get log allows you to look at the history of the repo it's a very powerful thing and we're going to go over a few of the options today and we're going to use it very regularly throughout this course because it's just awesome to have in your uh in your in your bat belt I guess we're going with Batman themed things uh anyways so reviewer explore check out uh man get log this I want you to open up the man page and actually look at it and I want you to search for-- graph and-- decorate and just kind of do a quick read get kind of used to reading the manual I'll do it along with you man get log there's graph right here and if I find the option there's the option look draw a text based graphical representation of the commit history okay good to know we have that all right and then we can go decorate uh let's find the option may have to go backwards decorate often is uh it's a little bit harder to read prints out the ref names of any commits that are shown probably not really sure what a ref name is remember everything's a commit everything has a Shaw and you can reference those commits with some sort of named item branches right branches tags there you go or the very special one named head we'll go over head in a little bit all right problem uh display the history of your repo with graph and decorate so I'll do it right now get log graph decorate often you don't actually have to use decorate if it's just printing out to standard in kind of like a little fun exercise to do watch this if I go this I drop decorate and I just do graph you'll notice right here head points to master if I take this and put it out to a file and then cat out the file look at what's missing that bit of information so if you're ever you know printing out anything to a file you need decorate to actually get uh oh decorate you need uh you need that you need that flag to tell you hey list out all of uh all the named commits for me nice little just nice little thing so often you don't use decorate like in practice because you're just looking at the log just something to be aware of so now it's time to merge these two together um there's actually two different ways you can do it there's merge and there's rebase we'll go over rebase at some point but we're going to start first with merge now remember a commit is just a set of changes in the entire code base at a specific point in time and if you have work on one branch and you need to get it over to another Branch we got we're going to use merge now the thing about merge that's a little bit confusing is that when you use merge you can have kind of two different outcomes you going to have one that has a merge commit and one that doesn't have a merge commit and You' probably experienced this where sometimes you merge merge and you have to input a message and then sometimes you merge and you don't have to input a message and there's a very specific reason why because it depends on your history we'll go over that in just a little bit we'll create the two situations which exist uh so what is a uh emerge aemerge is just attempting to combine two histories that may or may not have diverged uh diverging means like how we set it up a is in the past and we both share a but there could be zero or more commits in both branches that cause us to diverge now whenever whenever you are merging get used to something called the best common ancestor which is going to be the first incommon commit so for us we have ABC and we have a d a is the best common ancestor in the docs you'll often see it's called the merge base and so really it's just the first thing that's in common so if you were to walk up the tree like if you were to think about you having to write this algorithm you literally start at each commit and you just keep on walking the parents with a hashmap by the way you'd want to use a hashmap here unironically you use a hashmap to contain or really a set to contain all the uh all the things you've seen up to this point all the commits you've seen up to this point and the first time you have something that is in common you know you've just found the best common ancestor funny enough it's always a hash map I don't know why but it just always is anyways um git then takes these two commits that you have checks out the merge base or the best common ancestor then plays those commits on top of it creates a new commit called a merge commit and that will actually have two parents it'll have one parent from one branch and one parent from another branch and that is a merge commit and so this happens uh that's what you'll get a message because you have two different diverging branches so we'll go over that here we go all right so how to merge very very easy you have a Target branch which is the branch you are on and you have a source Branch the one you wish to merge uh with and the source branch is going to be the one that you provide the name this is how I think about it at least I think this is probably the the easiest way to think of it and so you'll do get merge the branch you wish to bring onto the one you're currently on so let's do it now let's merge Fu onto trunk but remember we don't want to ruin our current trunk state so I want you to check out a branch called trunk merge Fu so I'm going to go get check out uh trunk merge Fu have my new Branch remember dasb just creates a new Branch if it doesn't exist or awesome so now I'm going to go get merge Fu there we go we're presented this menu saying hey we need you to merge this together we want you to add a commit message it's been Auto merged for you but we still need a commit message CU remember a commit is a series of changes that represents the entire state of the repo plus an author plus a message and time as well when we're done I want you to use g. log so everyone that has just merged use g. log and see what happens so I'm GNA go scroll down here and I also want you to use the term parents when you do g. log or g. log when you do git log I want you to add-- parents so as show the parent commits along with everything so I'm going to go get log on line graph parents so you can see right here the merge commit has two parents if you look at this ID this Shaw you'll realize this is trunk where trunk is currently pointing to when you look at this one this is where Foo is currently pointing to this is C this is uh e and then now with the graph it's able to draw this nice graph because it has all that information it's able to walk back the graph and be able to display it for you so you can actually see in line what is happening the thing about rebase is I am completely convinced people don't like rebase cuz a they heard on Twitter that it's bad B they used rebase that one time screwed it all up and now they hate it and it's ruined their life therefore rebas bad it's not bad it's actually fantastic rebas is just yet another tool in your get bat Bel to uh be able to manipulate and use your repo effectively there's actually a really good reason why you should use rebase we'll kind of get into it a little bit more so this is our current setup right now we have Fu which is BC and we have trunk which is all the way up to Y at this point because we have merged on uh bar so this should be your setup right now now we can demonstrate the power of rebase by updating where Fu points to we're going to create this graph by using rebase this is what rebase does is it allows you to update underneath your set of changes and this is very important because it's not it is rewriting history in some sense but it's allowing you to have what is currently the reality then your changes as opposed to your changes that are tested against some previous reality that's no longer true you can now have it properly in line and then to be able to find out did you actually write something that's good or does it suck all right so and this also means when we decide to merge Fu onto trunk we can do a fastforward merge because again our best common ancestor our merge base is the tip of trunk now that we rebased and this is typically why people really like rebase is that it allows no merge commits and there's a good reason why you don't want merge commits and we'll we'll cover a little bit later all right so basic steps of what rebase does first thing rebase does is that you will first go get rebase Target Branch uh second what it's going to do is it's going to actually check out the latest commit on the target Branch so you're no longer on your branch you actually switch branches to the Target Branch then it's going to take the branch that you were on the current branch and play the commits one at a time in order onto the target Branch then it will update then it'll go back check out your current branch and update current Branch to point to that latest commit so therefore it actually just moves your commit forward in time and plays it one at a time so that's how you got to think about merge it's a little bit or rebase it's just a little bit different uh but it's very important to understand that because down the line rebase is going to cause a lot of problems if you don't know about that specific feature all right I want you to rebase Fu with trunk uh create a separate branch called Fu rebase trunk and so you're going to do a uh you're going to literally do a git rebase trunk on this Branch so I'll do it myself too I'll check out Fu and you can do get log if you're if you're curious right there you go ABC and so then I can do one on trunk AB or a d XY and then I can go get rebase oh get check almost almost messed that one up there we go we'll create the new Branch Foo rebase trunk and then I'm going to go uh get rebase trunk all right we've rebased so what happened well let's look at the log get log one line look at what we see we see trunk but we also see our two commits that differed from trunk now after y we now have history that is linear so now you see that trunk which contained bar as our last merge is now the base for Fu rebase trunk in other words fu is no longer diverging from trunk it's now linearly walking the history forward which means we can fast forward merge if we want to
Info
Channel: Frontend Masters
Views: 31,674
Rating: undefined out of 5
Keywords: #Git, #Rebase, #ThePrimeagen, #FrontendMasters
Id: 0Hk2XjGSzbA
Channel Id: undefined
Length: 14min 43sec (883 seconds)
Published: Wed May 08 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.