| by Arround The Web | No comments

How to Change Old Commit Message Using `git rebase`

When a user modifies or adds new source code files in the Git staging index. Then, they need to update the current repositories with newly added changes through the committing. Moreover, developers are allowed to view the commit history whenever they want as well as they can change any commit message if needed.

This write-up will discuss the way of modifying the old commit messages using the “git rebase”.

How to Change Old Commit Messages Using “git rebase”?

Check the following instructions to modify the old commit message using the “git rebase”:

  • Redirect to the local Git repository.
  • View the Git commit history.
  • Choose the old commit that we are required to be modified.
  • Run the “git rebase -i HEAD~2” command and change the status of the selected commit message.
  • Use the “git commit” command with the “–amend” option and specify a new commit message.

Step 1: Redirect to Particular Repository

At first, move to the local Git repository by running the “cd” command along with its path:

$ cd "C:\Users\nazma\Git\demo4"

Step 2: Check Log History

Then, execute the “git log” command to view the short reference log history

$ git log --oneline -5

Here, we have specified the range as “-5” which means we want to display the most recent five commit reference logs. As you can see, we have highlighted “bee2…” SHA-hash for changing their commit message:

Step 3: Move HEAD Pointer

Use the “git rebase” command along with the “-i” for performing rebase operation recursively and HEAD position as “2”:

$ git rebase -i HEAD~2

When the above-stated command is executed, it will open the text file with the default editor. Now, search for the selected commit and place the “e” for edit beside it. Save changes and close the window:

When the default editor closes, it will show the following instructions:

Step 4: Change Commit Message

Finally, execute the “git commit“ commit with the “–amend” option:

$ git commit --amend

Now, add a new commit message in the default text editor. For instance, we have typed “it’s my new text file” as a new commit message:

As you can see, the target old message has been changed successfully:

Step 5: Verification

Lastly, to ensure the modified commit message, run the “git log” command:

$ git log --oneline -5

According to the below-given output, the target old message is modified successfully:

We have compiled the way of changing old commit messages by utilizing the “git rebase”.

Conclusion

To modify the old commit message by utilizing the “git rebase”, first, redirect to the local Git repository. Then, display the Git commit log history and select the old commit which needs to be modified. After that, run the “git rebase -i HEAD~2” command, change the status of the selected commit message and execute the “git commit –amend” command and add a new commit message. This write-up illustrated the way of changing the old commit messages by utilizing the “git rebase”.

Share Button

Source: linuxhint.com

Leave a Reply