Rewriting Git History - Amend, Reword, Delete, Reorder, Squash and Split

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys it's Jacqueline modern coder back again with another get video so it's been a couple months since my last coding video and I figured this week I take a few minutes and talk about some of the different ways get allows us to rewrite history so there's quite a few ways but specifically we're gonna go into amending commits rewording commits deleting commits reordering commits squashing commits and then splitting commits that's quite a few techniques when we're going to go through them each one by one so if you prefer to jump straight to one of them you can check the description I've left a link to the timestamp so you can jump straight to that part of the video or I link to my written article which has all the commands written out so you can kind of use it as a reference if that's more your speed alright so let's get written to it so we'll start with the mend so if I run git log one-line here we'll just check out what I'm working with so I have my master branch and I split off to work on a feature branch and the last came you know I've made is I've committed the about me page and that's all looks pretty fine but if I run get status you can see that I have two files that really look like that want to belong and that about me page commits so I could either unstaged the previous commit and then recommit everything I could commit again and then squash the commits together but it provides us with some better option which is amend so to get started with amend I'm gonna just stage both of these files like normal so I'm gonna write git add about me a file picture so I've staged them if I run get status again you can see they're staged and now whatever on the amend I'm gonna tell git I want to take these two staged files and then stuff them into the last commit so Thorin get commit - - amend - - go edit with the no edit parts gonna do is just gonna say and tell good I don't want to reword the commit message so if I run this you can see that our commit history is the same the only thing that is different is that get changed the ID of the last commit that just shows that it's rewriting history but everything else is pretty much the same and if I were to get status you can see both of those files are now committed and added to that last commit so that's amend all right let's switch gears a little bit and talk about rewording commit messages so let's take a look at what we're at so far okay so right now I have my master branch with four commits everything looks pretty fine except for this commit right here I've made pretty egregious spelling errors I misspelled front page and then fix so I want to get that taken care of we can't use a men right from the command line because a man was only gonna operate on the last commit back from head and argument do we want to operate on is to back so what we're gonna do is take advantage of the plethora of options that interactive rebase provides to us so to get started we're gonna run git rebase - I head tilde - and what this is gonna do it's gonna tell git I want to operate upon the last two commits back from a head so I run this command you can see the get open up my default terminal text editor which in this case is vim and most like for U of M as well and present us with some options so at the top you can see our two commits back from head and then a big old comment right here with some of the options that we have available to us so right now we're gonna look at this reword option to get started what I'm gonna do is I'm going to change pic to reword in front of that commit that's misspelled right here and so that's gonna do some stell get that I want to operate on this commit so all I have to do now is just save and close the file so if I'm in them that's command or that's escape : WQ - enter so now git is gonna pop me up into another vim window and allow me now to edit that commit message which is right at the top so all I have to do is then press I to enter insert mode again and just change those spelling errors so after I've changed spelling errors I'm gonna again save and quit this file so commit escape : WQ enter and now you can see git has successfully rebased and updated my commit message let's see if it worked so hard one get log one-line there you go you can see that now my message is different and it's fixed and again you'll notice that we have a different commit ID because git did rewrite the history of this commit but everything else is the same so I just saw interactive rebase had quite a few other options we could we could work with and so let's talk about removing or deleting commits so let's take a look at my history so I have a master branch two commits and I've split off to work on my future branch my future branch has three commits but this commit right here in the middle I think I wanna get rid of the idea of this read me all together I'd rather this commit me gone so once again we're to take advantage of interactive rebase to get this done so when I first start off by running git rebase - I for interactive head Tilda and then we're gonna run three just for an example example purposes so it's gonna pop up again our default terminal text our which is most likely vim and present us with the last three commits back from head right here at the top as you can see this commit right here in the middle is the one I'd like to delete and so I can take a look at my options and really what I'd like to do is just drop this commit I want to remove it altogether so I'm gonna press I to enter insert mode and once again change this prefix here from pick to drop and all this is gonna do is gonna stay hey get I'd want to drop this commit so we're gonna save and close the file which is again escape : WQ if you're in vim hit enter and that's all there is to it so if we run git log one-line again you can see that that commit is now gone and that's really all there is to it so deleting commits is pretty easy and thankfully reordering commits is also just as easy so if we're on git log one-line you can see that I have the same structure as last time my master branch two commits and then my future branch with three and so what I'd like to do is I'd like to reorder these last two commits and I'd like to just flip them around so we're gonna use interactive view base to do this again so we can run git rebase - I head tilde - and we'll tell gate I want to operate on my last two commits back from head hit enter so as you can see my commits are here at the top and unlike some of the other techniques we're not gonna actually use any of these commands here below what we can do is we can just literally just change the order of these first two lines and get will reorder these commands or these commits automatically so we're gonna press I to enter insert mode then we're just pick off that last commit put it right here at the top and then just go ahead and delete the line now yes I know that there are shortcuts in vim for cutting a whole line and pasting it and reordering lines but I'm not very good at vim it's not my default text editor so maybe cut me some slack anyway so after we reordered the ordering of the messages here at the top all we have to do is again save and close this file and git will automatically rewrite history and move those files around so if you run git log one-line again you can see that now both of these commits have been reordered alright let's start to get into some fun stuff now let's talk about squashing commits so if we can see my commit history here I have my master branch I split off to work on a feature branch as you can see by these commit messages I've created three commits that are all related to the same feature I've added an API endpoint and all three these commits are fine on their own but I'd really rather than just be in the same commit they make would be a lot cleaner that way so we can use once again use interactive rebase to help us out with this so to start off we're gonna enter interactive rebus running git rebase - I head tilde 3 and once again this tells get we want to operate upon the last three commits back from head which are these guys right here so hit enter gets gonna once again pop up our default turn on text editor with the commits at the top so as you can see our hair on the bottom get provides two options for combining commits together so I have a whole other video about the squash workflow so if we're to check that out if you want more details on squash but for right now I don't care about preserving those extraneous commit log messages so I'm just gonna use fix-up now fix-up indicates to get that I want to melt the commit into the previous one and then discard its log message which is pretty much what we want to do here so here are three commits as you can see get adds them in reverse order so if I want to go ahead and press I to enter insert mode I can change the prefixes of the second commits from pic to fix-up and this tells get we want to take both these commits and just push them up into this first commit here and disregard their log messages okay so once I've changed pic to fix that for those last two all I have to do is once again save and close the file so in Vinnitsa scape : WQ and I hit enter and you can see git has successfully rebased my commits and if I run git log you can see they've all been squashed into this last commit and once again if you put these logs side-by-side you can notice that the get commit IDs are different just showing once again the gate is actually rewriting the history and that's all there is to it for squashing alright last let's talk about something really fun let's talk about some fitting commits so it can't be the case down the line that you decide certain changes we better off if they were committed separately and this tree is a good example of that so I get log one-line we can see what I worked with here so I have a master arrangement with two commits and they're split off to work on my future branch with 3 commits and you can see that this second commit here really would kind of make sense if they were split apart you know the nav bar and the bug-fix are sort of separate features and I'd like them to live in separate commits first we're gonna use interactive rebase once again to make this happen but it's gonna be a little bit more complicated than the operations we've done in the past but sort of stick with me a video is actually a great way to explain this so we're gonna run git rebase - I for interactive bug-head tilde 3 and this is gonna tell git once again we want to operate on those last three commits back from head so we're gonna run it so it's gonna open up our default terminal text center which is most likely vim and you can see here at the top we have our three commits and down here we have some options so what we're gonna do is we're gonna use this edit command which is gonna tell git that as its rebasing to pause right here on this commit so we can do some operations on it so I'm going to change pick to edit for that one we want to split apart and then we're going to save and close this file so when we do this get will start executing any rebase operations I specified and since I left the other commits alone it's gonna skip those but it's gonna break me out into this special rebasing branch right on that commit we wanted to edit so if I run git status you can see I have an interactive rebase in progress and this commit right here is the one I want to edit so the first thing I want to do is I want to unstaged s commit an done stage all the files that were added to it so do that I'm just gonna run git reset head and then care it and if I hit enter what this is gonna do is I'm gonna unstaged all those files so if I clear and I run git status you can see them still on the interactive rebasing branch here but I have these three files now uncommitted so now I'm free to commit these files in any way I want so what I really want to do is I want to take these add at the navbar commits to a new commit and I want to take the analytics which is gonna be my bug fix and add that to a new commit and I can do that like any normal workflow with using just get ad and get commit so first I'm gonna clear my screen here so you can see it better I'm gonna run a git add that Marta HTML and then navbar that CSS those are now staged I'm gonna Maddy's added now when we run git add analytics and then commit that to a separate commit which would be my bug fix commit great so after I've done that I've created now two separate commits instead of one I want to get back to this rebasing operation and finish it up get off this special branch all I have to do is run git rebase - - continue and get will continue the rebasing process so now you can see I'm back on my original feature branch if I run git log - one-line you can see the other commits have been left unchanged however this commit in the middle that used to be one is now split off into two commits and that is gonna do it for this video I hope you enjoyed I hope everything made sense as always if you have a question leave it in the comments and especially for this video if you prefer a written guide you can check the link in the description I've added I've written a blog post that goes through each of these commands and sort of explains them step by step just like I did here in the video so I will catch you in the next one see you later [Music] you
Info
Channel: The Modern Coder
Views: 79,214
Rating: 4.9861941 out of 5
Keywords: software developer, git, git amend, rewording git commit messages, deleting git commits, reordering git commits, squashing git commits, splitting git commits, rewrite git history, amend git commits, git squash, git reword, reorder git commit, git rebase history, git repo, fix git mistake, github, git tutorial, github tutorial, software engineer, programming
Id: ElRzTuYln0M
Channel Id: undefined
Length: 12min 58sec (778 seconds)
Published: Mon Jan 28 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.