An Introduction to Git Internals

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey David thanks for joining today hi Gwen hey so today it's gonna be about get internals hopefully that will be something that is very exciting I have a couple of prerequisites and some couple of slides to go over with you so for the good internal slides first just a couple of prerequisites so if you're just starting to watch this you're participating or if you're someone new you should be comfortable with the command line if you're not then it's probably going to be a little overwhelming today well you should already have get installed and a basic understanding of get if you're watching this trying to say look I want to understand how it get works it's probably going to be too detailed and you should be generally interested in learning about more about it because we're going to get really really deep into it and today's purpose is we're gonna really try to go over creating a new repo and it'll be a lot of hands-on watching and seeing what's happening underneath will change some branches will actually review get objects look like we'll dive deep and to get objects will do a get at least briefly Tucker with the concepts of agate hash we use the get cat - file command because that's a very helpful part for us to actually see what's happening with objects will start comparing branches seeing what happened what's happening without making too complex of a branches here you pick a little bit do an interactive rebase a little bit just to see what's happening there and hopefully have time to talk about working practices so that's mostly what we hope to get covered in the next hour so how does that feel sounds fun okay excellent so what I'd like you to do if you don't mind is can you share a terminal screen with me so we can kind of start and I'll have you creating the database the get internals branches and repo for me yep you just shared it can you see this I can that's perfect okay go ahead and make and database screen go ahead and make a database directory of any kind make tower example whatever you want to call it we're gonna work out of this directory okay internals so good CD to internals and now what we want to do is we want actually inside of internals we're going to make a brand new get repo and to create it from scratch that's done with they get and command just know it's that okay okay so it says that initialized so we've initialized an empty get repo ah perfect and you've already said well I don't see anything what do I see in the directory an LS minus a shows that there's a dot git directory do an LS minus al to show that that actually is a directory and not just yeah if you'll you can see right there that there's a directory on the left perfect so that is the directory it's actually something inside of it and if you less that directory and look at into the internals you'll see that there are a lot of data structures there so there's something called branches config description head and all uppercase letters hooks info objects and rafts we're gonna dig pretty deep today on heads objects and refs okay make sense so in that repo if I were to type a get log command go ahead what do you think you will see oh well have you done great so we will see this really wonderful can our messaging that gets famous for that tells us exactly what's going on or not what you mean not so wonderful but after a few minutes I think you'll understand what this message is saying so what does a git log normally show you do you remember yeah it'll show you the last couple commits that you've made the commit messages right like the yeah some other information it does yeah and since we don't have any commits yet it makes sense that I can't show you a log the message is weird but the idea of get an error is not so weird because we've not made a commit yet we've really just repo with no commits in it so bad default revision head Wow the only thing I look in there that says that's kind of familiars if you look at your LS above and you look at head below they're the same thing so the type cat been looking and not get well you're you're gonna do what get to the subdirectory head perfect you actually have something called head head that is readable it's a text object and it looks like it's a reference to refs heads Master yes that's pretty cool we can actually see what's going on and catch up pretty quickly with that so very very cool so you have something called head why don't we go ahead and I can see you're already jumping on you're like oh okay well there's refs heads Master I saw rest as part of my data structure so let me just go look in there oh and you noticed when you were trying to you were doing type-ahead tab-completion I could tell and you're like wait there's no master so this error it's complaining about it's saying hey look dot get the head is what dot get always uses to point to things yeah pointing to master but master doesn't exist so that bad default revision head message is really saying I'm looking for your first commit head points to the master branch but there is no master branch is nothing there I can't find it and that's why we can't create her you know we can't go on and while you're doing that I'm going to go ahead and I'd like you to go ahead and create a first commit and while your first commit I'm going to start preparing a whiteboard so you can kind of see what's happening I like your license okay okay so I like your license and I like your commit your first commit basically says that anyone can use a repo that's great so if you were to look at what I'm showing you can actually see that I have a head that was pointing to nothing that was what saying before head pointing to we don't know let's do the same set of commands we'll be doing a lot of times let's actually look at what your head is now pointing to so go ahead and just cat dot get slash head okay so it still points to ref heads master you know let me go ahead and in my whiteboard hold on for me for a second I'm gonna into this to say master okay so I have heads pointing to master we had that before but we didn't really understand it but now let's go ahead and do the same cat you did before to say what is master pointing yeah go ahead perfect so instead of reading the whole hashes I'll probably take the first four or five letters but you now have get is now pointing to something so when you actually have master Matt these reference riffs are for references so I have a reference that heads pointing to it's the master branch and it's the head of the master branch and this is what that master branch is so because we can cat that means that that it's not a binary file is just a plain text file and it's pretty cool that we're digging in and seeing the details of that text file so we're gonna throw the git command away soon except for one thing we'll be doing everything without good alright so in my graphs real quick let me go ahead and put six to nine me so six to nine B I'm gonna erase what was here just to kind of show what we're doing I'm going to change this to 69 V was that correct yep six to nine ba okay okay and then I'm gonna draw my pointer right down here so that we have a pointer to 69 ba okay well great and we can actually see what the structures look like but that's not as helpful yet because you know we're still pretty detailed without seeing what's happening why don't you do a git log again okay and now you see that the git log did the same thing in the fact that six to nine ba is the commit message or the commit ash okay you have the first commit in your head is pointing to the master branch and the master branch is pointing to a particular commit and that commits somewhere happens to have the information that we're looking for that you're singing get log I don't see it yet on how we've done it so let's take a look again at your dot get sub directory so LS not get and let's see what objects we see there okay um so we see a head that's what we've been using so far we see a boss we've looked in rest because head seems to point to things as references to different rafts and there's something else that I mentioned we have branches what we've looked at before - you have config that some people have looked at but we also have objects do an LS of objects and let's see what it is perfect okay I don't see the immediate correlation but do you know there's a six - and this commit starts with the six - perfect if exactly was going for so it doesn't have the entire commit message or they commit in here but it does have the first two and that six - because it's blue on your screen tells me it's a directory as well yep go ahead and LS the dot get objects six - and see if there's anything else there okay see Nyami okay I can see the entire commit message this is pretty simple um why do we why did we take the first two I guess when you're generating the hashes if once you've been you don't want this directory to be too enormous I guess yeah you have limits on how many files can be in a directory and if we're gonna hit those limits why don't we split it out by taking the first two characters so we can have many directories that could hit that limit have less they less content in it so we have a less of a chance of hitting element and just having two is enough because we these limits are pretty far out there for where we are today so okay so we actually so far have been able to look at everything we just catted that directory of ahead we actually looked at its references and for the references we found the objects we've gone into the object subdirectory and now we know where the object is we don't need to get command we can do it all manually if we wished painful like right now um I forgot to one thing I need you to do is to look at that command but don't cut it I'll warn you that it'll mess up your terminal so why don't you have you ever used VI - some kind of the I command or how do you normally look a file that might be binary oh I don't may I use fine I mean I use file to pronounce let's do file and then you'll want to finish the yeah finish the file name there okay so you'll see that it is a particular executable not script it's not even executable it's a binary file and if you were to look at those contents have you are you familiar with feel at all I'm not great but I can do it I'm Annie max user okay if you can get in and get out that's all we need so Titan view instead of view is not VI it's like a read-only version of VI yeah just do that so you can tell at the top there's a bunch of garbage data so that's kind of showing us not just the file command but showing us that is really garbage data so okay I cannot read this directly so escape : q perfect that means that I have not changed that file at all good I believe that that's actually a binary file either dreams of never using the git command or being a little crushed yeah Norris I have something that I can use that's a get internal command that is somewhat helpful so type git and then we're gonna use something called cat - file there's another argument yeah go ahead and type that in there is another argument we're going to need so we'll backspace when you're done when you're ready for that so one of the first arguments if you did this without the argument it would say syntax it needs an argument - no what you want to do next there are two arguments that can be helpful for us the first one is it to type and if you just do a space - T there and it says all right I have an object in the object database what type is that this will actually tell you go ahead and press Enter not a valid object name oh it oh sorry thank you for doing that you did exactly what I said even though I was wrong everything we've done before has been exactly following the data structures underneath but can't file is actually a git command so it doesn't it's not gonna operate on the file exactly we're gonna kind of say get I want you to cat a file of this object so don't give me the file path even though it's the same thing just give me the hash which is the six to nine being take off that I kept right there that that's actually an you can tell what it's doing underneath it's actually saying oh okay that's in my get directory it's an object's the first to a 62 here's the rest of it I found the file you want to add it what do you want to do with it you want me see its type okay I can check its type oh look it's a commit object okay okay there are different kinds of objects in here we've only been looking at commits and today it's really just about looking to make the commit so we can kind of see what's happening but you also have something called a tree object which we will see today but we won't go into details on and you also have other types of objects to at least one other okay all right that's great but it still doesn't help me see my log command so what I want to do now is not use the minus T but I want to use ap for pretty so T was for type P is for pretty okay press Enter so this is the actual thing that will create the hash out of and look it has it looks very similar to your get log it has something tree hey look if I were to go just real font for fun type get cat that file space the minus T it might be a little bit more typing because don't use the yeah use that object yeah so we want to say the object that's here in the tree object let's just type it perfect what type of what type is it okay it confirms it's a tree object so if I were to go into trees later from the commit object I would I would explore that path okay but for now we just know that hey if you'll press up arrow twice and give me them minus P for the pretty ones to see its last on the screen again yeah though I have some things that you might have the actual tree I have the author I have the committer and they have the commit message oh and what's to the right of the author and the commit sir the exact timestamp got it so this information is is what's used to calculate the hash that's going to create the 69b so if you were to say that let's say you need to follow this exactly again tomorrow at exactly the same time Morrow that you were to do the exact same steps that we did would your hash be the same or different Shh the same I don't know if the time there's a part of the hash oh look to the right they're right past author okay well then this is part of the yeah yeah so but you're right exactly I'm thinking if the time weren't part of the hash you're exactly right it would be the same but because time is you can see how the chance of having a hash collision here is incredibly unlikely and even then as we will see for the next step when we get into more details you know your first commits one thing but what happens when you have another commit another commitment we will see that in a few minutes what about an author and a committer we have two things we have one that's an author and one that is a committer so they're the same person and did you know that if you had someone else's work that you were able to pull that work and say look I want to take that commit that someone else has done I want to pull it into my branch there the author but I'm the one who's committing it into my branch you can see how would have two different names the original author and then my name for committing yep I've never done that before then I pick not yet I've sort of followed instructions but not known what I was doing for a cherry-pick well say no because I want to show you a chilly end there you know it's close to the truth okay good so we'll hope they cherry pick soon as we look at these structures and do some some changes around alright so I think if you do a git log right now and you look above we have very very similar data structures you don't need the get log command I'm sorry yeah you don't need any get log commend you've done the same work by using everything above by looking at the internals except for the cat file which helped me look at the contents but other you you you've not used the get load commit and you can see what's happening great yep let's make another commitment I'll let you do that however you'd like I like your commit messages and I like how you're doing it in one terminal that's nice okay so what you've done is you've actually changed a file the readme file now you've added that file to the repository and now you've made a new commit message that actually has the message of I've added the readme so let's start from the beginning on the get log ram but don't use git log we're gonna do it from scratch pretend broke our git command and let's look what's going on and let's do it together so first things first let's look at the head what does head look like good okay so head Refs I'm sorry best heads master let me quickly switch over to my screen while you're doing that and say okay head points to master so that hasn't changed that's great okay let me switch back to your screen what do I see go ahead and you know you see that heads is a pointer to these director I look perfect and so now it is a pointer to the same hash and then hold on for me you're great I'm good alright so is it plenty to the same house for a different hash different hash uh so that's perfect so my video or I'm sorry my my whiteboard is now wrong I no longer have this pointer to that hash I don't know what happened to it so I'm just gonna leave it hanging I'm gonna leave that out there somewhere and I'm going to draw a new one right here yeah seven de zero eight seven zero e yeah seven zero e e8 that says Bureau need a zero eight did I get it right no perfect okay and you're saying that head now has I'm sorry master has a pointer there you're right okay so great we'll draw that in the same line we don't know what this is this was weird we're just gonna ignore it for now all right so go ahead and go ahead and do the next step which you already started typing you're going to do a cat file on the p4 pretty don't forget your P objects print out the contents your P argument I mean you print the contents great no no complete no because it's not a file it's only a hash as far as its concerned okay so oh this makes sense this is what you just last committed oh but wait if I were to look at the details tree I've seen tree before parent that's new but I have author committer in the message so this hash is going to be have this hash but what is that parent object how do you think we can find out probably do it cap I mean by doing account let's do a cat file on let's do the type first just to be clear let's first do a type of the parent to make sure yep good good and press Enter so it's another commit so the first commit is pointing to the to a different commit all right so before you go on let me just quickly make sure I have this in my picture so seven e08 has a parent what was its parent parent is six to nine ba ah so look at this it seems to point backwards so we always point to the last one seven zero zero eight but he'll pointing kids parent which was was the previous one all right so go ahead and then say all right I see the parent let's follow the parent and dump it as well so so do a minus P for the parent like you did the T above and what you have on your screen looks a lot like a git log to me yeah this is the this is the parent again I'm that same one yeah so if you were to type to get logged command okay you or see the results it would be almost exactly the first one and then the previous one it looks great almost the same can you up arrow it twice just to go back to your two commands ago that one and let's look at it one more time so that's the first commit that we have I don't see parent right why this is it has no parent the first one so really the get logged man if we went into by code we could write our own script that would do the same get log if someone took our getaway but let us have a cat file we could start with whatever we were wherever we were we've got ed we would actually look at whatever it points to we would grab its hash wherever it is in the tree we would then start we dump the contents of that hash we would find its parent and dump its contents we would find its parent and dump its contents and we would go until finally there was no parent and then we would stop yep okay so we understand exactly what get log is doing from its internals that's pretty cool all right great then let's move on let's do something new fun and exciting let's know it's decided to make a new branch so go ahead and take and to keep it in two steps for those who are watching let's do a git branch and a new branch name and whatever branch name you want to give it I never do it this way so I just typed in the new branch name that's right that's right you probably do it all at once oh and you want to make that branch name that whole thing there is actually the branch name it's not new branch but like new underscore ang my branch sally's branch okay so sorry this is good this is the space between the new and the branch is what has me Oh crazy there makes sense yeah it's an underscore but oh oh that's why I would make sense I didn't see the underscore my apologies from where it was on the screen I could only see that it was a space okay then that's perfect all right let's pretend that we are get log and let's look at what's going on now so first thing I'm going to do is I always look at the head so let's can't nope I think we're get log we're actually going to do it manually so can't get slash head what do you think it's going to point to trick question by the way it's going to point two seven zero zero of eight because we haven't switched branches don't yep good question it's not only it's actually gonna point to the different branch that we're on so the heads actually not just what hash that you're on but what branch which points to the lash that you're on and what branch did you they were pointing to we created a new branch but we didn't actually changed her yeah I did that so do an LS of your dog get slash refs heads yep and you heads yeah and you can see that there's actually two references there master and new branch but what we're currently pointing to is his master still there still because we didn't do it so now just get check out new branch which is the step that actually changes it and I think what you're used to doing is actually creating and checking out in the same step with the check out - be commend yeah yeah that's fine but breaking it into steps we can see what's going on well if you cat that so go ahead and do a cat get slash head you should see hey we're pointing to the new branch okay so give me a second I need to make a picture that's up to date I am so that was so two minutes ago here so this is not the most elegant in the world okay it's functional hey it's a new branch and this part of the picture is wrong sound effects free here we go new branch but we have no idea what everything else is looking like so what everything else looks like so go ahead and help me see what would you do in this case I get new brain new perfect and it has seven zero e zero eight seven zero okay fair enough so I'm gonna draw an arrow from here do it ups let me switch so everyone can see I just had this was what it looked like I had just drawn an arrow right here but I hadn't okay so we now see that head points to new branch master points two seven zero seven zero zero eight but so does new branch but if I were to make any changes where where would they go to what branch would it be put on new branch yeah exactly in fact that's what I kind of want to do right now let's go ahead and go ahead what are you saying nothing I said okay okay so let's go ahead and make a new commit and to prove where we are go ahead and just do a get branch so everyone's can see what branch were on okay it was the new branch just like we saw on the internal structures and they make a new commit gonna have to make a new file or add something to another phone Oh Python awesome echo echo instead cat very good okay and so you'll add Cody wine you great alright let's do the same step we've done a hundred times now let's look at what the pointer points to just I know it's pedantic but let's do so can't get slash head what is head point to it points to new branch wait for we let me switch pictures head is pointing a new branch ok that's good right coming back and you are now going to look at what that branch points to okay heads new branch good and it's oh that's different see nine seven three a touching to my picture see nine seven three eight I don't have it anywhere it's a new commit Oh see nine seven three a I think I said direct that's right okay so and maybe drawing it right there wasn't the best place to draw it because I'm gonna have to draw arrows around things so new branch no longer points two seven zero e all right let's take that away okay that's going away and I'm now going to draw around oops sorry about that bro it's not what I meant I meant to take an arrow and draw new branches plane tear okay I'll do it this way around the point to see nine seven three eh okay now go ahead and just pretend you're get log and work your way backwards let's see what we see wild minus P for pretty and we've seen all of these are our commit hash is so that's fine now we have a parent so you just pointed to parent of seven zero zero and if you were to quickly do the same thing on it we should see that it's the previous commit okay and then there's no parents oh yeah there's one more parent there's one more so we'll do it again okay and then we're done and there's no parents so we're done so does that what is that reflected in our picture so far so you know not a parent from I just need to draw the way so you do this but I didn't draw in my picture so let me draw it now is I actually have that pointer from nine seven three eight two seven zero zero eight all the way down and that makes sense so if I were to switch to new branch which is what I'm on now I would have three commits and they would start with C nine seven three a back two seven zero e zero eight back to six to nine ba and then would be done if I were to switch to the master branch and let's just do that real quick but before you do switch the master branch then I would actually just have two pointers seven zero zero eight point two six two nine ba oh yeah let me switch so everyone can see your screen go ahead and do that so just switch branches again so just check out master and then just do a git log I'll be faster just to do a real get log and you'll see that there are only two commits and we can look at the hashes seven zero to 6000 graphically we can start seeing what the internals are looking like does that make sense yeah all right the only thing I didn't do is I didn't like internally when you switch branches all I did was just change that like how hard is it internally what oh notation or how difficult does it forget to change branches I simply change the head pointer from that branch to that branch super simple super efficient okay yep so I can have branches diverged and draw this picture in any way I want but clearly I can just have these different branches pointing to these different branch pointers it's really head is a pointer to whatever branch pointer and those branch pointers are all pointing to different parts of the tree so if I wanted to I could be able to take things for one part of a branch and put it onto another part of a branch you know do all kinds of things like that but before I do I'd like you to switch from head back to new branch so get checkout new branch great and now get low if you see three things great now I am going to change my pointer to reflect what you just did so that we all can see what's happening just switching this one pointer back and forth and now I would like to do something new I would like to do something called a get a min okay okay so you're going to so get commit dash dash amend yeah I think 1m I always make the mistake so I never know until I've done it once great now unlike before where you had a you were been doing it by the command line you have an editor that comes up but that's fine you can change something but let's pretend that you don't have to control X to save you okay we have we have amended that commit but we changed nothing theoretically well well we appears to me that we changed nothing do you think that we've changed anything probably the time stamp yes time stamp so if I were to go look at that git log real quick so I'll notice that that top hash yeah if your friends so I have rewritten this commit it is now seven zero zero seven II let me switch to my picture and type in zero seven E and so I have really changed that commit and I've only changed about changing the timestamp that changed its hash but everything else was the same it's parents still pointing to the previous thing I didn't change its parents or anything else right um so I can't actually change history by simply just by doing an amend I am actually changing that commit and if I were to actually have given my copy of this repo to someone else and they've already updated all of their pointers to talk to everything that we're pointing to and if I were to do an amend I could make things very confusing for that person who's now pointing to a different than what we're pointing to yes okay and I'm sure people have seen that before if they ever share branches and change history on that branch after it's shared okay what I'd like to do is move back to master let's get check out master great get logged prove that there are only two commits there and I should have had you if you can scroll upgrade if not we'll just have you switch back but I'd like you to save in your buffer the hash of that top commit I believe it was zero seven II okay I'll just I'll just make a note of it in another window okay so that's fine yeah I think it was zero seven EE something and that's probably good enough yeah yeah okay so since you switched back while you're doing that I'm going to update my picture so we just switched to master I believe correct yeah yeah we're in master okay so now that we have head pointing to master which is pointing to commit seven zero zero eight master doesn't work by there's no arrow from master back to zero seven EE master does right okay so zero seven EE is not on the master branch it is in our object to repository it's in that database of objects but master doesn't have a path to it yes but what I'd like you to do is I would like you to say you know what I want you to take a commit from a different branch I want you to put it into my current branch and I want you to update the pointer so that it's the the committing question that's called a cherry pick got it so just to prove to everyone where we are let's just do a get log again I think you just did so get log we have two commits the last one is seven zero e zero eight eight I'll switch screens real quick to show that zero seven seven zero zero eight is what it's pointing to so though now let's type get space Harry - pick and then that commit message no zero seven E the one you pasted from the branch yeah the master doesn't know about that commit it doesn't follow the mass report but what we're gonna do is say master great all right let's now do a get log okay I see three commits there it has even added code added readme in first commit so let's go and look at my picture and see what needs to be done so master doesn't point directly to seven zero zero eight anymore it did but it doesn't matter green yeah I agree with that the point go ahead it's actually not pointing to 0-7 EE either though it's point to easier of BB interesting very good all right then let me not draw this interim picture let's go draw a picture what's going on and let's look at everything from the beginning so just a quick looking at your screen to acquit counts get slash head so everyone can see and that refs heads master so quickly I'll switch back to my picture head points to master good ok switching back to your screen let's look at what good so what is it point to it points to a 0 BB 4 back to my picture you're right there it doesn't point to that one it points to something called easy row BB 4 so let's go ahead and it's because what you mended we had to change so easy Roby before correct yeah so I now have easy 0 BB 4 master is now pointing to that particular branch and then I'll switch back to yours what is its parent so if you'll do a quick it's cat - file - P in that hash and its parent is 7 0 e8 agreed given so it almost looks like we have a 7 0 7 ee & a 0 BB 4 they're very very similar they look identical but they're actually different hashes no and if we were to go look at the objects real quick let's do a get cat file on two objects so in your buffer we're gonna use the one that's a 0 7 te and another one that's easy Roby b4 okay that's so the last if you'll do a git log on that branch perfect yep and then just cat while mine is P I don't forget - P yeah and then do it again on the last night yeah look at the minus P hey added code added code so that we look at what's going on sometimes we aren't actually changing commits because changing commits could actually change anything upstream what we're doing is making a new commit that's identical to the old one except it's not identical it has a different time yeah and there you go you're committer and you're committed time there has changed because you're doing it not at the exact same time you're drinking time does that make sense yeah and the author time is still the same though because I didn't actually make any changes exactly you committed that committed a different time but you didn't actually write the code at a different time and that could go confusing if you look at my graph I now have crossing shape because I tried to keep things in one line to keep it simple we could redraw this but we'd run out of time those are all two pointers and really we have a box on one side and a box on the other side and really they both point to this object so I'm going to hand wave just a little bit only to save time that we have something that looks about like this and for an exercise we can do this on our own reconstruct this and see what's going on so you can see that we both have two different pointers from different branches pointing all the way back in time back to the original makes sense yes I don't see where new new branch has an arrow waiting I left it hanging thank you for catching that let's let's draw this now so it makes more sense and I hopefully I got the arrows to the right ones a new branches pointer is actually to the right commit this one so if you do a get log on new branch real fast is it this is real 70 then seven was easy zero and then 69 no perfect okay so we starting to see how these branches look and it's very counterintuitive the first time ever anyone starts looking at this they think that anything new that we're doing is going at the head like in the very top and they points forward and I think it's very confusing for people to start saying how we're pointing backwards but it's so efficient each one of these things so one thing to switch branches and it's one thing for us to just add a new commit and then just have its parent point to the previous I don't have to do any more work down the tree it's already done so efficient it is very very elegant that way I love it but you almost have to know it's internals to know what is doing sometimes alright so I think that that's a lot but I'd like to throw one more thing on if I may ok all right don't forget well two more things really I'd like to do something and I'd like to talk about the consequences of doing something so both of these things both of these branches look like I have almost identical histories get logged between both of them like you're I get check out master get log get check out new branch get log if you're not observant to the commits you would actually they look really dumb really similar right right because they are almost identical yeah I like you to pick one of them I don't care which yeah you do let's pick new branch just to be more systematic I think that's a good one so good check out new branch the nice thing about watching this as a video is anyone can pause if I go too fast if I start speaking too fast that's okay we can just pause I would like you to completely change the entire history of all of this going backwards in time I and I will show you how I'm going to save it you know what I don't like this history I think adding code identically makes a lot more sense than adding everything now in real life I don't or read mean because more sense but I'm trying to make an artificial example alright what I'm saying is first commit add code add readme I don't know if the readme looks like until after code and generally I don't do it or do it just like you just did it but I'm gonna force that just for now to make you have to just swap two commits okay all right so zero seven EE comes after this seven zero zero which comes after six to nine B and I'm gonna switch it so that seven zero e 0 8 8 is the top one it points to zero 7 EE and it points to 6 to 9 VA and that's called an interact you base ok you will need a text editor that comes up but what we had before was fine so get rebase - space - - interactive and we're going to rebase the last two commits and I use I'm gonna say head to lead to I'm there no space down say that again head no space Tildy yeah okay press Enter so um might be hard for some people to see what's going on but if they can see there are two different commits at the very top the very very top one is the added readme it has a small abbreviation of the commit to it and the next one down is added code so I could have actually had up if I had 20 commits here I could have you know rebase 20 what did the head tilde to mean it just says from head which we now know how to follow it back go back so from head which is that commit that it points to now take two of those from it backwards and I'll take those and put them in this window so I can look at what's going on and I have lots of options I can squish them into one commit I can reword the messages I kid yeah exactly as you're putting your mouse down I can actually edit the actual commit so I can change the code as I'm writing it I can do all kinds of fancy things but all I want to do today is just pick I'm gonna use the commits if I were don't do this yet but if I were to press to save this it would rewrite that history exactly the same structure in the same path it has now just different hashes because the time would be different okay but we don't want to do that we're gonna switch lines so it works backwards I'm going to take that top line and move it one below the one it is now but what editor man I don't know how to copy and paste whatever I think you're in Nano yeah you have three commits there - Oh perfect I see what you do that's great Nano is a really simple editor it's mostly just copy and paste and then control buttons okay so that's all I'm doing is I'm switching the order that's all you're doing is switching the order so that means it's gonna say okay take the top one but it's gonna basically reapply those commits but in a different order so control X to save them control that do I want to save yes please file my mentor gonna night change it let it do its thing successfully rebased well you see your git log from before add it from backwards it's added code to add your dreamy first commit or from the top down now do it oh yeah I get log and now it's add readme added code first commit so you've changed history crazy it more importantly unchanging that history you actually did you just have two pics or three your terminal I can't remember now I had two okay good so the very first ones probably the same then alright let's take a look at this picture and let's let me just go ahead and remove all of what I see here because I have a feeling that this can get really confusing if we try to show everything let me erase all of my arrows leaving a little red and white White's just like a real whiteboard I'm being a little smudges and I am what branch will be on a new branch I'm on new burnish yeah alright so so far the picture looks about like this now let's work our way and see what's going on oh I should have left those hashes so we could have compared okay so go ahead and do a cat get slash head you know press ENTER and so you can see it points to new branch so can't space get sloshed up refs nubra heads of a new branch and it's pointing to 90 though I didn't think to leave the other hash in but we can always rewind that to go look at it before and draw our own pictures 90 what was it 9 d 4 d ok so that points to 94 D I don't remember that hash it's a new one it's a new one and it points to go ahead and follow its parent like before oh I'm drawing pictures but no one can see what I just did I can see it yeah but the only one of these is visible at a time and I just switch the visibility on the recording for this one so I just erased his other whiteboard stuff I put this up and I pointed from 90 down all right so go ahead and do your next one yeah it's parent is one f00 okay let me switch over and then type in one f00 again I don't recognize that commit not that I can remember all of them but they do seem new for me and it still has a parent so I'm going to draw another empty box and let's go look at it it's parent is 16 9v 69b okay which is R which was there before so we recognize that one so really and I will draw this in blue we had the exact same old picture but these commits even though identical information except for some committing time our our new these two mates kind of got rewritten because there and the old commit does that make sense yeah and your old commits all of that old stuff they're not being pointed to in our pictures because we only have two branches but they're still there there's no database we don't go back and delete them until the end so go ahead and do what's called garbage collection get base GC that's garbage collect let's throw away that other stuff we didn't need that we're not pointing to anymore so data compression writing compression that are counting the objects not a lot it's not the biggest repo and the other day we have deleted and clean things up and so we now have just the stuff that we're pointing to and we have a fairly good understanding of what's going on underneath I think okay did get rid of anything um why don't we do an LS of objects that get slash objects Wow where'd they all go it get packed so it was kind of an unfair for me to actually show Kings because it doesn't just do it it doesn't just do what I was doing but it also does some efficiency of cleaning up so I was hopeful I should have actually been able to keep the old hashes and go look at them but yeah it cleaned things up it also poppin so it was more efficient for for storage and things like that so at the end of the day um let me go ahead and and switch screens that I'm sharing to my original slides so I will stop sharing this and I will switch over to my slides that we started with and let's see how we did so we created a new repo I think that's pretty obvious I think we did pretty good without one it's just that get a nectary that get directory that would created change branches I think that was pretty easy we could see what was happening underneath when we changed branches the CV objects some really cool things going on with objects we actually dug deep with how to look at them and we kind of understood what hashes were doing by the actual content that was being hashed that created those hash objects those hash IDs use cat file to go look at a lot of those things and we compared those two branches we cherry pick from one of their branches we didn't interact a rebase and the only thing we have left to talk about is the working practices don't forget that a git repo from github for example or that I get repo that you have pushed or pulled code from all of your pointers are pushed and pulled from someone else so I might be pulling code from github that someone else has given me and if they change their pointers that I assume are already correct they're pointing at something different than I am and that can be so confusing in fact it could cause a total get hell situation if you have never seen good hell it's pretty messy and pretty bad because at the end of the day the pointers are different but if you if the person in github had all the same pointers and they just added to it and I had all the same pointers and I added to it then at the end of the day then at the end of the day then I just realized that I hadn't switched to the other screen so people are looking at your terminal instead of mine so there I was reading here but if I had pointed different things up if I had actually had different pointers than you have be very confusing but if I have the same point just and you're just adding more that point backwards and I can share my work very very easily whenever I do an interactive rebase I'm changing history clearly when I do a cherry-pick I'm changing history but generally as long as it's on the leader branch it looks like a new commit no one cares about I'm doing an amend I could be changing history if that's pushed as well but if there's ever a question at all what I'm working with someone else what I can always do you saw in the very beginning we had new branch and we had the master branch they both do the same branch I can make a brand new pointer if there's any question if someone else like I want to repay something I'm like I owe but I don't know if anyone else is looking at it let me make a brand new branch so go ahead let's do that real quick I'll change the user screen so people can see it and just make a new branch from you are what branches you want new branch yeah so do get checkout - be that's a way we're making new branches and checking out but use the new branch name of new underscore branch underscore our zero for revision zero now that's a new yeah enter so that's a new branch and if I push that branch it's not gonna affect into the other pointers it's a brand new set of pointers a new path in it can't affect anyone else mm-hmm so as a working practice if I think I'm rewriting history at all I will just make a new branch so that I don't affect anyone else don't change the branch that you're on that someone else is already looking at because that's just crazy but if I create a new one it looks like dinickel two copies of the same thing like we saw they work off of the new one then they can see what I'm doing and it doesn't hurt anything at all and I could always clean that up later when I'm going through a rebase so if in a new branch r0i do something like I am and or I do an interactive rebase then it won't change anything about new branch right exactly because it's new pointers off of that branch the old branch is pointers are not being affected by this one okay really yeah so that's a good working practice to keep keep us from from you know the more you have a different branch the more people can can work cleanly will have extra objects in your database big deal we can throw those away later right so in general how helpful was this well I've got so I used get all the time for my for my projects and it's mostly just been me working on them so I I usually have a stable branch that I share with people and I work on my own branch but lately I've been working more and more with other people and a couple times we've had we've yeah we've run into get hell I just don't really know what's going on so what I tend to do is copy the code and delete everything and just start to start over like that but I I think I have a better understanding if I have if my collaborators are working in another branch than even if we go back and change history and another branch then that's not a problem and then in the original branch that's right in fact as I highly encourage keeping different branches even if it's just underscore our zero underscore one squirrel or 99999 it's just different things and if you need to you can just switch back to where you were to kind of see what things were before your before history got rewritten so I have one question so if I do a merge so if I've got so in my new branch I have stuff so I have one sort of string of backwards pointers I check out a new branch or zero say an interactive rebase and then I want to merge that back into new branch then is that gonna emerge won't won't work quite right is it fine emerge will be will be quite right but you want to make sure that you know you like when you're merging it should be the final version of the branch after its merged it's done in it's permanent in that particular branch if that makes sense okay let me switch to a picture let me switch over to my original whiteboard and I will show you a really quick artificial example that I'm throwing together so these are different branches okay there's a code that's changed here it points backwards and there's code that changes oops I'm not showing us again sorry okay so there's code that changes here on the left and there's code that changed here on the right and my picture is not accurate if I'm pointing to my branch pointer so let me just remove the branch pointers for now it's not really relevant so I have or I will at least make it accurate looking so it's something like this so if that's a particular branch and I have another branch over here called new branch five or you know some different type of working branch I have two different branches that are working away backwards to this original initial one what emerges is a combination so it basically says from that from the history point of view a merge will have two parents you saw how you have parents and the file output right a merge has two parents so in this particular one you can see I made up the numbers obviously but and I'm gonna make one more hash at the bottom so it's obvious that it continues linearly backwards so when I have a merge there's code that's common to the left and the right and this commit hears it all out even if you have to like go resolve some conflicts so that whatever this commit does it says it basically is a combination so that everything backwards from the arrow at this point in time is consistent so I could follow the history back this way I could follow the history back this way at the end of the day when I get to this point this top point it's a merge of all the code so at this point anything that has it as a parent is is fixed in time if you will okay that helped yeah you know so a merge is really a combination of two parents and so that's how you can once it's merged all of those history at this point are basically a set and so I what I do is I use something called get flow or a variation event quite often networking practices and then is where we will merge on what's called a future branch but it's just a branch that I'm working off of and then we have like release branches and we have master and we have developed so frankly it often happens to be that I work in develop let me see if I can erase this picture here and start over so often we have the long-running master branch where the Easter branch is like what really is going out there and often these will have tags like release one release two probably much more about production releases than you care about but for what you're doing but still it the idea is I have fixed months moments in time where this master branch represents everything we've done long-running branch that is often develop branch which is where everyone's merging their code into so you don't merge it directly into master you merge it into develop you know you can continue testing before you go to master you have a point where you can before you merge it in a master you have a point to make sure that all is good test pass things are very clean but then you have all of these feature branches many many many future branches which is much more what we looked at and so we're all working on a different branch and typically I wouldn't even need to share my work because we all are working our own little thing a small small small future branch and then as it's happier one likes it's merged into develop so develop is something we all share that's fairly stable but my changes are in the future branch make sense you know so that's how you avoid a lot of this instead of having to work on the same branch like developer master with other people each person has their own little branch like fix bug one two three or go work on this bit of code and then people can kind of look at it and even do a code review with github and say oh I think that makes sense oh that doesn't make sense and then when it gets merged into develop it becomes kind of canonical for everyone else no one will change the history after hits develop that would be the big big sin if you well it would mess everyone up and then of course going to master and no one should change the history there so the only history should ever be done is on a future branch and that's only until it actually gets merged into develop okay yeah this is sort of this is what I guess this summarizes what it is I'm just right starting to try to do with with my projects excellent and the nice thing about what we're doing now is that you are able to start seeing what's going on underneath if you have to draw a picture you could actually go look underneath and see what's happening and you could actually construct a picture of what the branches look like if you ever get into a confusing situation follow the branch just look with their parents they'll be very nice and detailed but it can help you figure out what's going on if there's something that happened but general of working practices can avoid that simply by working in future branches rebase all you want but only you have once you've shared that branch that feature branch with someone else never change it make a new feature branch instead once you well up never change it ever ever ever once it go into master ever ever ever ever ever ever ever don't ever change it and their hair people will be fine how they'll be working and there'll be no problems at all the problems are much more or less of a code you'll have merge conflicts from time to time right hell yeah merge conflicts is going to happen a little bit by just communicating a little bit Bower on the future branches but it's much less about you know people are changing history while I'm working on it that can be very confusing okay all right well thank you very much volunteering today I hope it was helpful yeah no thanks a lot it was informative Oh all right look Epstein take care all right
Info
Channel: Glen Jarvis
Views: 10,161
Rating: 4.7876105 out of 5
Keywords: Git (Software)
Id: GFNPoHGeVDM
Channel Id: undefined
Length: 66min 31sec (3991 seconds)
Published: Sat Mar 07 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.