hey there nick dunatakis here in this video we're going to go over amending or changing git commits this could be anything from changing the git commit message on the previous commit all the way to doing an interactive rebase where we go back to let's say five commits ago and then we make some change to that commit by maybe adding a new file or altering one of the files there perhaps even changing the git commit message as well so this is stuff i do all the time in local development before i push my code up because what we're talking about here is rewriting history so you should really try to do this stuff before you push your code up although technically you can use the force flag if you are in a serious pinch but yeah while developing locally you know i find myself just committing code pretty frequently but as you probably know it's really hard to figure out like the perfect git commit message or exactly what files belong to a specific commit especially if you're actively developing a project right so you could totally be working on a feature where you think you might have committed all the correct stuff right all the right files and whatnot but then two hours later you realize like ah you know what i really need to go back like seven commits ago and just add these two files there so that's the type of use case we're gonna cover here but uh this uh code that we're gonna go over now is the source code to my build the sas app with flask course and it is available on github url i'll leave a link to that one in the description so you know if you're looking at the commit history for everything uh online there then that's what we're going to be looking at here so let me just bring up the history now and by the way i should mention that we're going to do all of this through the command line just using the get client no fancy aliases no fancy tools so this is the get log for the project and about a week and a half ago i made a very very big update to this course where i just re-recorded i don't know like five and a half hours of videos live where uh you know i was doing all these git commits here and i found myself while doing this before i actually pushed it up to the repo you know i had to go back and and then some commits because i forgot to do something because you know that's what happens in real life so you know we're gonna kinda make a silly example for this video here like i'm gonna go back and make some changes to these commits but these are actual changes that i don't want to actually commit so you know that's why i'm in my temp directory here because i'm going to blow out this whole entire project or folder once i'm done with this video but let's start with the most basic case here in the git log right so this previous command like the last commit that i made was like update docs to fix formatting you know one extra order is hanging on the fourth line so this is a pretty good git commit message but let's just say for argument's sake that we wanted to modify this command message to be something else you know maybe uh your project manager looked at your work and before you actually pushed it up and he's like well you can't use the word docs there it needs to be documentation right so update documentation to fix formatting cool so let's do that so that that one's actually very very easy we can just do you know get commit and then we can use the amend flag here saying that we're going to be amending the the last commit there and then we can just put in a message here and type what we want or we can just do this and then have vim open the file here or you know whatever your code editor is for uh studying the git commit editor and then from here you know we can just be like okay cool let's say uh update doc documentation here documentation there we go everything is built correctly and then we can just save that and then there we go we just amended that commit and if i do another get log here we can see that now it says update documentation to fix formatting and everything is good to go notice how there's not like a second commit for this right we amended that one commit but now let's say that maybe you want to amend this commit by actually altering what was commit right so if i copy this git sha here and we do a get show here on that sha we're going to show the sha then we can see exactly what changed right it was just this one little bit of documentation here where you know this was the old paragraph and now this is the new paragraph uh and really i just reworded the paragraph to be slightly shorter because on github for the readme file one extra word was hanging off and i just didn't like that that's not super important for the sake of this video so now i'm going to open up a new window here so we have them open and i'm going to make a very minor change to this readme file right so the changing i'm making are pretty silly now but let's just say for argument's sake that you wanted to add some more documentation to this file and include that as part of the commit so there we go we made our changes and i'm going to go back here and if we run a get status we should see that hey we need to change to this readme file but we need to stage those changes right so we can do a get add p here and you could also do like a get at a or you can do get out of readme but uh you know i had that other video in the past about good ad p and you know that i like that a lot because now we can see exactly what is being changed here we're you know set to be staged so yeah let's agree to this change here because it also gives you a good chance to proof uh read your work right so we did that now we do get status and we can see cool everything is uh ready to be commit here but we don't just want to make an actual commit because then that's going to create a new commit what we actually want to do is amend that previous commit so we can just go back to this amend command that we ran before and uh you know let's just say this time around we are totally happy with the original git commit message that we had so we can just add this other flag here called no edit and that's going to allow us to amend that previous commit and it won't even prompt us to adjust the message at all so you know if we do a get log here again we can see that you know update documentation to fix formatting is there there's no extra commits there and we just amended this one commit but we didn't change the message so that's what the the no edit flag allows you to do so you know takeaway there is git commit and then it's just the amend flag to change the previous commit and then optionally also the no edit flag to not change the messages at the same time but you know if i do another get show on this shot then we can see here that it is what it is right the latest changes that we created and notice too that the shot is actually different than what it was before uh that's not too important of a dtl well i mean it's worth noting that but uh yeah just make make sure to note that when you do make an amendment there the shot is going to change so that is the simple use case right and and by the way we could do all sorts of stuff here too right like if you wanted to touch another file here and just add that and then you can just do an another amend here and then we do a get lag and we can see uh this shot here it changed again this is another example and we do a git show then we can also see here that we touched a new file uh if i go down here somewhere there we go new file foo right so this is not just like modifying the only file that you happen to be modifying in that one commit right you can add files you can remove files you basically have full control over you know staging whatever you want so that's going to take care of you know the quote unquote easy use case right altering the previous commit but now let's take a look here at this git log and you know let's say that you know instead of editing this previous commit what you really want to do is maybe edit this commit here right which is a couple of commits back so we're not going to be able to use the git commit command for that one we're going to have to do an interactive rabies now maybe there are other ways to do this but you know this is the way i'm most comfortable with and you know i've seen this being posted all sorts of different places on the interwebs so here we go right so let's just say we want to change this commit so let me copy this commit message here or sha i should say just so we have an idea of like what this commit looks like right again not very important for the sake of this video like the contents of what's being commit but you know this is a commit that altered you know all sorts of different files we changed health check names we added some tests and whatnot so now let's make that amendment but before that i'm going to run a get status just so we can see here that there is nothing to commit right our tree is totally clean there's nothing to be staged but i understand in real life like maybe you want to go back and make your amendment but right now you have like seven files that aren't commit yet but you don't want to lose all of that work so we are going to tackle that use case in a bit but let's go over doing a very basic interactive rebase before we get into that uh now this command that we're going to be running this interactive rebase command it will expect us to pass in the get sha that we want to amend and there's two ways to get this right we can run it get log and then we can just copy the shot that we like or we can reference it using the head tilde shortcut where you can do head tilde and then you put a number down so for example this commit we can just you know this is reference this one as commit number one so this would be commit number two three four so we could technically do head till day four to get this specific shot or specifically this commit message itself and you know i guess the takeaway here is it's up to you on how you want to do that you know sometimes it's easier if you're on like the first page of output and you just know like it's this one here so you could be yep head to la3 or you can just grab the commit it's up to you but yeah when it starts to get a little bit crazy where you know you're paging down multiple pages like slowly counting them and you're like you know 38 like commits down it's a lot easier just to grab the comment sha because you know exactly what you're getting and also you know when we run this get rebased interactive command you know this is where we're going to pass in uh the thing that we want so this is going to be either the git shot or like the head tilt a4 like this like whatever you want and you know let's just actually do it with head tilt a4 just so we can see both in action you know you can pick and choose which one you want to use but when you run ahead till they 4 there you know we can see here refactor and improve health check you know that is the thing that we want to edit here you know ignore all the stuff for now we're going to get to that in a second here but likewise you know if you want to run it with a shot instead and there's a very important takeaway with this one notice how when we just put the shine and hit enter then that commit actually is not here right it used to be the health check one but it's no longer there when you put in the sha you actually need to put the carrot here because the carrot is going to reference the parent commit so when you put that in we can see the refactor and health check one is here it's back in business so you know just remember the carrot at the end because it's very important but now you know this is the the real takeaway right this is starting the interactive rebase and this command allows you to do all sorts of stuff here right all these different commands that you can do but we're not going to focus on any of them except for this one here where we're going to be editing a specific commit so what we need to do here is we need to change this pick word to be edit where technically like e also works but when it comes to this stuff i like to be super explicit so i just put in the edit and um yeah i mean again there's all sorts of other stuff maybe i'll make future videos about that one but for now you know this is the commit that we want to edit and by the way before i actually pull the trigger on this one if you just close out this right i'm just quitting here and not doing anything then you can just go back and rerun the command so if you screw it up by putting the wrong commit sha or something it's very easy just to get the right one right and there we go and technically you know this doesn't need to be on the top like you can always do an older commit and then you know decide that we want to edit this one cool then you can just do that instead but uh let me just go ahead and do this for now and we'll go to edit and then all we have to do is save the file and now it's really important to pay attention to the branch that we're currently on so this actually says that we're not on a branch we are re-basing master and you know this branch output is part of my dot files i've i don't think i've done a video on that one specifically but it's in my rc file i'll link to that one as well but you know this is the branch that we're on and if you run get branch in case you don't have that notifier there then you know you can see the output of all the branches there and you can see the current branch that you're on with the store there so now we are basically ready to do an interactive rebase and you know if i do a git log now notice the output right uh this actually is pointing now to the head there's one commit that we actually want to modify here so from our point of view at this point it's sort of like we just you know went back in time and this is the state of the get tree and we can start making the changes that we want so if i do a get status here you know we can see that we're currently doing a rebase here you know don't actually you don't need to read all the stuff but you know it's clean basically is the outcome there but now let's go ahead and make some of our changes here i don't know you know it's not super important to follow along like this actual source code but let's just say for argument's sake you know what you wanted to change that file maybe i went to the docker file and i just added like so another comment here just so we have a couple of different files that we wanted to modify as part of this new commit here so now if we're running it status then we can see that we have you know some of these things aren't staged yet for commit so this is still very similar to what you normally would do when you're not rebasing so we could just do get at p again and be like okay cool that's what we want okay great that's what we want to and then i can do another get status here and we can see that these are staged to be commit and you know we can sort of combine what we did before with the amend so now we just do git commit amend and you know we can choose to either possibly edit the message as well or maybe we don't want to you know do it as well so let's do the no edit here because let's just say that we want to keep the message the same we can do that and we can actually see here too in the output of of this command for good status it says uh we can just run a git commit amend and that's basically what we're doing here except we're throwing in no edit there just to not edit the message just like we did before so when we do that then we can see that we've made a change there and then the last thing we need to do here which we can just follow their docs here is to say okay cool so when we're done we can do a get rebase continue and uh since we're satisfied with our changes right and we can do that it's going to rebase successfully hopefully and we can see now that we are back on the master branch and if i run to get lag then you know we can see that we are back to business to where we were before there's the update documentation commit and there's our refactor health check one but notice now that this commit shot changed and if i run to get show on this one uh and then we start scrolling down we can see the new change there we just rewrote history and uh we did a first successful amendment uh excellent and again we could have repeated that process and you know not put this no edit flag and you could change the commit message and uh it would be really really nice but before we go into the other use case of like having files that were modified but you don't necessarily want them to be part of the rebase there is one thing to go over here too when you're doing a rebase so let's go back to get lag and let's just say again for fun like whatever this shot is the one that we're going to rebase because this stuff really comes down to like muscle memory right the more times you do this stuff it's going to become second nature so let's interactively rebase this one put the carrot at the end there and then things are good here yep let's do this one edit that one great right quick cool and now let's just say you know you're back in here and you added some stuff and uh cool everything is good but uh and now when we do a get status you know maybe you think like well uh on second thought maybe i don't want to do this rebase at all you know maybe i'm just going to do something else right because this also comes up all the time so you can actually just want to get rebase abort and that is going to totally uh wipe out the rebase as if you know it's not going to implement anything and we can see here we're back on the master branch and if i do a git log then everything is uh what it was before and i think it was this one that i grabbed the four seven whatever whatever was it yeah so you know if i do get show here on this one then you know we didn't go over this one before but there's no changes basically so yeah that's basically how you can just abort doing that because that's also uh and by the way sometimes you know these branches don't get updated on your prompt in real time you know you can just hit ctrl c or whatever for that to update but yeah that's how you can actually do a rebase abort doing a rebase but now let's do another use case here where you know you might have like a file here you know maybe let me open up thin here we'll do another change to the readme file i don't know whatever more documentation changes here even though this is like an actual markdown file so that's actually a documentation line and then if we do a get status here we can see that you know these changes are all staged right but we don't want to have these as part of our amendment so what we can do is take advantage of the git stash so we can do git stash here and just stash those changes and if we do get status then we can see that you know these are all untracked and you know any changes that we had here for the readme file aren't going to be reflected and if i actually go back here and open this readme file in vim that one line here that we had the one with like the fake readme uh comment there for the markdown you know it's not there because we stashed it so now we are free to do our get log here grab this well actually we're gonna do the head right so get rebase interactive and i can do ahead till day two and then we can just change this to be edit write quit and then uh you know maybe for this specific commit you just want to touch i don't know like another file here or maybe uh well i had him open this one so if i go here like i don't know go to the doc compose override example file cool like target blah blah blah you know this is totally not valid syntax here but you know you made your changes like that and then we can do a get status here we could do get p on the docker file here great that's good and then we can also do get status well technically it's already there visible on the top but you can see also maybe we want to you know get add another file because that's the file but you know the hello file we don't want there right so now we can do a git status once more just on the top of the screen and we can see what we need to do right we can do a git commit and we can do amend but this time let's say we also maybe wanted to change this to be i don't know 2050 right because we're in the future uh for whatever reason and then we made that amend there and then we can just do get rebase continue and it's going to rebate successfully cool so we do a git log and we can see the update copyright to 2050 if i go and do a git show on this one then there's the changes that we had we also have the new file probably somewhere on the bottom no oh there it is another file great but if i do get status here we are back to where this other file isn't here and now remember we did the get stash but now we can just do a get stash pop to pop this out of the stash back into our working tree and uh we can see that we are back to this modified readme file here and if i go back to vim then i'm gonna go into the readme file and we can see that one fee comment is back there also let me get this back to the master branch there we go and uh cool we're back in business right so now it's like all these changes that we did before they're back they're not committed anywhere and we can continue working on them doing you know basically whatever we want so you know that's a very quick rundown or i guess maybe i don't know how long this is going to be like a 20 minute video that's a rundown on amending commits right i feel like this is a really valuable tool to have in your arsenal because as a developer and using version control and yet specifically you know it's really nice to be able to do all the stuff so with that said you know if you like the video please give it a thumbs up because it really helps if you have any comments or maybe improvements on this workflow let me know i'm happy to answer any questions or you know take feedback and whatnot so with that said thanks a lot for watching and i will see you in the next video