

In this article, we will explore the power of Git amend and its ability to modify commit messages. They provide a historical record of changes made to a repository.Ĭlear and descriptive commit messages help you collaborate better with team members, more easily maintain your code, and understand how the project works.īut there are times when you may need to edit a commit message due to typos, inaccuracies, or insufficient information. Remember that when a branch is pushed (made public) on Github, it's risky to change commits and do a force push, as other developers might rely on it.Commit messages play a crucial role in Git version control. It's important to understand the difference between the two options and the consequences of using them. In summary, changing the commit is the thing that developers do frequently, so knowing the appropriate commands can save you a lot of time you could spend googling the solution. In the previous section, Commit 3 follows the changed Commit 2, so its hash has also been updated. With an interactive rebase, each commit that follows the modified commit receives a new hash, since each commit contains the ID of its parent. However, there is an important difference. Modifying commits with both "Interactive Rebase" and "Amend" will cause old commits to be deleted and new ones to be created with new hashes. Obviously all are changed, although only the ones that were edited should be changed ( Commit 1 and Commit 2). When you're done with the changes, force-push them to Github over the old commits.īefore moving on to the next section to find out the differences between interactive rebase and amend, let's check the commit hashes: Check the terminal for a success message: Replace pick with reword for every commit you want to change message for:Īfter that, you will see a commit file for each changed commit.Ĭhange the message, save and close each of them:Īnd you're done. # r, reword = use commit, but edit the commit message Read the notes under the last commit and note the following: Let's add 3 commits and try to change the messages:Ĭlick "Enter" and you will land on the following screen: Where n is the number of commits to display.

If it is necessary to change the commit message for an old commit or a set of commits, then an interactive rebase can be used: If someone depends on your branch, they would need to fix their history. You can force push it ( git push origin main -f) and it will be overwritten, but be careful with force pushes. If your commit only exists locally, everything is fine and you can push your branch to Github ( git push origin main).īut what if you have already pushed a commit that contains a mistake and changed it with an amend command? Notice a mistake and change the commit (changed commit hash: 55fff7682f0997aeb99751d90c6f3d73081b8fcf)Īs you noticed, the commit hash has changed, which basically means that the old commit has been deleted and a new one has been created.


While this is not any kind of a serious problem, it is nice to keep your commits history clean and understandable. Developers often find themselves in a situation where a commit message was misspelled because they were in a hurry to deliver a new feature.
