Git: how cleanup commit history using interactive rebase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
why is it so unfriendly to new users is it because the concept of a distributed version control system is too complex for US models or is it because the inner workings of git as impressive as they may be are necessarily exposed to its command line interface perhaps all we really need is the ability to make commits and push them Upstream but no we're also forced to understand the nuances of fetch versus pool merge versus rebase reflog head and even blame why why learners you that's why welcome back everybody today we're going to talk about the git interactive rebase it's really not complicated but a lot of developers get intimidated by it because of the fear of losing code things like that but it just sounds fancy but in reality it's just editing commits all you do is just select how many commits you want to edit and then it pops up the simple log or a simple file that you edit and in that file you specify what you want to do with a commit by default every commit will be picked and will be included as is but you can change it to reword the commit which means change the commit message you could change the files in the commit you could change the commit order or you can combine a couple of commits together using the command squash you could do a whole bunch of interesting things with Git interactive rebase but typically people use it for three use cases editing the commit messages squashing commits together or combining them together or changing file in the commit history the goal here is to deliver the code to your team the cleanest way possible please be sure to check out my first two videos about the git rebase Basics and how to resolve conflicts in git rebase in those two videos I explained why rebase is called rebase be helpful to you to build some mental models that you can use to understand how git Works internally so let's take a look here I have a feature Branch at configuration dashboard in my repository and if I look at the history git log there's a bunch of commit messages by the way there's a there's a better way to look at the history you can do git log Dash all dash dash decorate on one line and graph and that shows you the commit messages like that and you could tweak this to your liking so for example like this I also configured an alias for this command so instead of typing all this I type git dog and it gives me this so I have my feature branch that started off of Master branch and it has three commits in it I've been working on a feature I've been hastily adding commits and I made some typos in this one for example add config dashboard main class in its tests but I forgot to add tests to this one and then describe test scenarios and I edit the tests and I described tested area so it's my team will have a hard time following what's going on in this setup so I want to I want to fix it up so what I'm going to do is I'm going to start editing my commits from commit just before before commits I made so from this hash code I could do git rebase uh Dash I for interactive and I can say I want to edit five last commits or four last commits like this and it pops up the editor don't look at it yet starts with the commit just before my commit and then all my commits or I could do git rebase and I can specify the hash I want to start editing from and so oops I want to do git interactive rebase for that and it starts that I usually use a number of commits I usually look at my history and I decide how many comments back I want to go and I do rebase Dash I had tilde character and 4 is the number of commits this opens your text editor of choice and this is by default is going to be the vi but if you use VI you could have problem with your dating life you could configure it to be your vs code as well so it can pop up the VS code and a separate window where you can see the same exact text file so this text file is just simply the list of instructions for git to do to perform if you don't make any changes to it it'll simply pick your commits and apply them we'll apply them and essentially nothing happens successfully based nothing happened so everything remained the way it is also you may notice that the commits are displayed in the reverse order from the log so when you do the git log it shows you the last commit first but in this case it shows you the commits in their chronological order this is the oldest commit and then the first commit in my feature branch and then the next commit Etc so this is you see note this is the commit with the typos and let's fix the typos git makes it really easy because every time it pops up that git interactively based editor it shows you everything you could do with it and it shows you some instructions and it's pretty basic it's nothing nothing to it so all you have to do to edit this commit message is to use the command reward or R so what I'm going to do is I'm going to just change this pick with the r and I'm going to reward this commit and let's just do that and just save it and when I save this file git will just go one by line and we'll apply everything that you've specified so I only changed one commit so it's just going to pick the previous commit we'll keep it unchanged it'll reward this commit and it'll pick these other two commits unchanged so First Command I want to reward this so I'm just gonna fix this oops dashboard and I'm gonna save it and that's it so if I do get dog now you'll see that it fixed my commit message but now let's say I wanted to actually add tests in this commit in the second commit so I'm adding the config dashboard first and then I'm adding main class and tests I didn't add tests in this commit intentionally so what I'm going to do is I'm going to do rebase with last three commits and I'm going to edit this commit for example so to do that I'm going to refer to documentation select edit so I'm going to do edit save this and now notice notice what happened so I'm currently on this commit so git rebase stopped at this point so and it tells me what to do this is the commit message I'm editing you can amend commit now so I opened it basically opened it up for editing and I'm going to open my config dashboard so this is config dashboard and I'm going to add tests so I'm going to add config dashboard test RB I will place all test code here momentarily so I saved that I'm just going to do git add it gets and so see I don't have to commit it because this I don't want to create a new commit I want to edit this existing commit so to do that I want to do continue I want to do git rebase continue but it's important to Stage your changes for commit just like you you would normally do before you're committing but don't commit so if you look at the status get status it shows you that you're currently in the interactive rebase if you read that and it says the changes to be committed in this file is this one new file so I'm going to do a git rebase dash dash continue and that prompts me to also edit the commit message which I don't need to do I'm just going to save it again and my rebase is complete no actually no it's not complete because there's there is now conflicts oh interesting I did that intentionally so in my it turns out in my earlier commit describe test scenarios I already added that file so if I modified the prior commit add main tasks and I added the same exact file this will conflict with the subsequent commit so get interactive rebase automatically prompted me with the conflicts and there's no need to be afraid of this it just first it stopped on that commit that had conflicts and it just dumped everything that's uh conflict conflicting into the working directory so now I have both added config dashboard test and if I edit that file uh so it just shows me what was there before and what I'm adding uh so what I'm adding so this is this is what I put in while previous rebase and this is what uh was there already in this file so I'm just going to remove the markup I'm going to save the file I'm gonna stage this file for commit and I'm just gonna get rebase continue and again it pops up the another Editor to let me edit the commit message I don't need to do that describe test scenarios and I'm going to save this so if you look at the git dog now you'll see the spelling mistakes are fixed or typos this now actually adds the tests and now this actually doesn't do anything because it's this commit just doesn't do anything now so uh this is still not very clean uh if I look at the last commit at this one git show it shows me that I'm simply changing the content of the config dashboard test from this to this it's not useful to anybody like why not change it in one go and so if I go get dog again so this commit and this commits are essentially doing the same thing so I want to squash them together so to do that I'm going to do another git rebase interactive with going three commits back and look what happens so these are the two commits I want to combine together and the way to do that is to use the squash command and if you read the comment it says use commit but meld into previous commit as I said before git goes from top to bottom it picks this commit and applies it unchanged then it picks this commit but then I want to squash this commit on top or together with the previous one so to do that I'm just going to change this command to squash and now this commit will be combined with the previous one and if you have multiple commits to squash you could say uh squash squash and so these two commits will be squashed into this one or will be added combined together so you could squash multiple commits in one go just like that but I'm not going to do it in this example I'm just going to save the file as is and so now this is a combination of two commits git prompts me with another text editor and in this text editor it shows me what I'm changing and there's two commits that are squashed there's one commit message there's another commit message and I could decide what's my resulting commit message will be so what I'm going to do is I'm just going to delete this and I'm just going to keep this admin class and its tests save this and now if you look at the history git dog it shows you there's now only two commits one commit where I'm adding a dashboard and another commit is I'm adding main class and its tests and so I'm kind of ready to push it up to my team but really why the why should there be two commits smaller the pull requests are the better because it's easier to review them meaningfully funny thing is when you have a hundred files in your merge request it tends to be reviewed really quickly so you'll get two approvals like in no time when you push up one file in your pull request then uh the whole team is going to scrutinize it it'll that one file will have 100 comments in it and uh so for that reason it is better to keep your pull requests really small and simple and incremental so that the team can do a meaningful code review for you you don't want to push multiple commits in the pull request is what I'm saying so usually you want to make an incremental change a change that brings value or adds value to the project and then push it up as a pull request so in this case I have two commits still I'm just going to squash them together again I'm going to do rebase um and I'm going to squash this into that and I'm just going to do end tests so I'm just doing it real quickly add configuration dashboard and tests save it and now if I look at my history this Branch only has one commit add config dashboard and tests and if I look at the git show for that commit it shows me that I've added the config dashboard file with this content and I added the config dashboard test with just this line okay hope this makes sense it's not intimidating at all if you're not comfortable with using VI feel free to configure it I'll let you Google how to configure your favorite editor of choice to be git commit editor hope this is helpful I'll see you in the next one let me read a few comments from the last video did team programmer become an HR associate or something there is not a next video maybe he fell in the lake in the Salt Lake City rip team programmer love thank you James that's a very nice of you I'm still here unless I'm gonna fall into this one thanks I lost manab Roy thank you for that this is the best video thank you so much LOL every time I have some conflicts to resolve I come for this video thanks a bunch you're welcome great stuff thanks thank you so much exclamation finally I understand this very helpful thank you helped me a lot thanks man you're welcome man the sketch really crept me up more please I'm trying a Channel with this level of production value has only 38 subscribers this video has been has the best explanation I've seen so far of rebase conflict resolution make me number 39. thank you so much that means a lot to me thank you a lot thanks for watching and I hope I'll make a few more videos and uh I'm planning to make videos about other subjects that I run across at my job for example kubernetes running continuous integration you know stuff like that all right stay tuned see you later
Info
Channel: Team Programmer
Views: 3,730
Rating: undefined out of 5
Keywords: git, rebase, interactive, version contol, software development, team, collaboration, github, gitlab, pull request, merge request, workflow, tutorial, lesson, explanation
Id: rt9ZOVciJm8
Channel Id: undefined
Length: 15min 27sec (927 seconds)
Published: Mon Feb 06 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.