Change Git Commit Messages Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody today I'm going to talk to you about rewording commit messages in git I'll talk through the commands and I'm going to show live animations on the screen of what's Happening under the hood while I'm issuing those actual commands let's get started git provides two ways to update commit messages however there's one big caveat that you need to know before utilizing this process but let's start with an example and I'll get into those later on I'll run git logs you can see what my repository looks like at the moment and I'm also going to pop out those first few characters of the commit hashes into my animation they'll be relevant later anyways I'm working on my master branch and as you can see I've gotten four commits but if you look a little bit closer I made some pretty egregious spelling errors on that second commit back from my head commit before we go any farther I'll point out that if you wanted to update the commit message of only the latest commit the process is quite straightforward I just issue the get commit dash dash amend Dash M and specify a new commit message this command will update the latest commits message and you're done now I have a whole video on git amend and I'll link that in the description or in the pop-up that just appeared in the top right corner of your screen but back to this example I want to update that second commit back from head so unfortunately amend only works on the latest commit so to manipulate those historical commits we need to reach for a different tool rebase everybody's favorite git command and we'll use what's called interactive rebase to reword those commit messages so to get started I'll type git rebase Dash I and that Dash I means interactive mode and I'm going to pass the argument head tilde 2. what this does is it tells it that I want to operate upon the last two commits and of course you can pass any number to this you could do head tilde 4 for example if you wanted to manipulate the last four commits instead of two but I think you get what I'm saying I'll just hit enter and when this runs git pops into our default terminal text editor in my case that's vim and probably yours as well now at the top of this file you can see it grab my latest two commits and added them there right at the top the rest of this file is just one big comment that acts like some documentation about what commands are available to you during interactive rebase for now we're just going to focus on that reword command but if you want to learn more about interactive rebases other powerful commands I've got dedicated videos you can find that link that just popped up in the right corner of your screen this may look confusing but it's pretty straightforward once you understand what's going on you can think of interactive rebase like a list of commands that will run in sequence on each of the commits specified at the top so once we save and close this file git will execute each one of those commands in order now each line contains first what command is going to run followed by which commit that command will run on so because I want to update the commit message I'm going to change the pick command to reword in front of the commit that's misspelled this will tell git to stop at that commit during interactive rebases execution and give me a chance to reword that message and I'll leave pick in front of my latest commitment because I don't want to change that commit at all you can think of pick a sort of like a no op command for rebase now that I have that I'll just save and close the file to start this process so in Vim that's escape colonwq and once that file is saved and closed git will start executing interactive rebase and pop me into another text window that's going to allow me to edit my commits message so I'll just press I to enter insert mode in vim and I'll go ahead and fix those spelling errors by the way this file also has a big comment that tells me what step in the interactive rebase process we're currently on it also going to tell us what files are in the commit that it's currently operating on so once I've gone ahead and updated that commit message I'll save and close the file again and in this case it's Vim so it's Escape colon WQ and now as you can see git has gone ahead and finished that interactive rebase process and updated my commit message and I can just double check this if I run git log one line my current message is now updated but wait one big caveat that you need to know about interactive rebates is that it essentially goes back and replays the changes introduced by each of those commits back onto your branch so because we operate on those last two commits each of those commits was essentially Rewritten and replaced by new commit with a new reference in our case even though we change the commit message of that second commit we still went back and reboot the last two commits which results in two new commits and two new references to those commits as you can see in the before and after the hash identifiers are different meaning those commits are replaced what all this means is that when you use interactive rebase you want to do it only on your local changes but after changes are pushed into a shared repository whereas the chance that others could base work off of those commits you want to stick away from using these interactive rebasing tools because you can cause confusing situations for your other collaborators if you want to learn more about interactive rebase and trust me you will want to learn more because it's an incredibly useful tool I've got a dedicated video diving into interactive rebases mechanics as well as each of those interactive rebase commands that we saw earlier there's a written article on my blog that explains everything we talked about today but as for me that's all I've got for this topic and I will see you later
Info
Channel: The Modern Coder
Views: 2,952
Rating: undefined out of 5
Keywords: programming, linux, unix, terminal, git, github, git rebase, git interactive rebase, git commit --amend, git rebase -i, git reword
Id: gz26pPCK2x4
Channel Id: undefined
Length: 4min 48sec (288 seconds)
Published: Tue Feb 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.