{"id":5080,"date":"2024-09-02T08:55:35","date_gmt":"2024-09-02T08:55:35","guid":{"rendered":"https:\/\/linuxbuz.com\/?p=5080"},"modified":"2024-09-09T04:22:44","modified_gmt":"2024-09-09T04:22:44","slug":"change-commit-message-in-git","status":"publish","type":"post","link":"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git","title":{"rendered":"How to Change Commit Message Using Git Amend"},"content":{"rendered":"<p style=\"text-align: justify;\">Sometimes, you might need to change a commit message if it contains errors, such as typos, incorrect information, or missing details that are important for understanding the changes made in that commit. For example, if a commit message incorrectly describes the changes or lacks crucial information about why the changes were made, updating it can improve clarity and maintain the accuracy of the project&#8217;s history. This makes it easier for collaborators to understand the purpose and context of the commit.<\/p>\n<p style=\"text-align: justify;\">In this tutorial, you&#8217;ll learn how to change a commit message in Git using the <a href=\"https:\/\/www.atlassian.com\/git\/tutorials\/rewriting-history\" target=\"_blank\" rel=\"noopener\"><strong>git commit &#8211;amend<\/strong><\/a> command.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#What_is_Git_Amend\" >What is Git Amend?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#Changing_the_Most_Recent_Commit\" >Changing the Most Recent Commit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#Reviewing_Changes_Before_Amending\" >Reviewing Changes Before Amending<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#Amending_the_Commit_Message_Without_Opening_an_Editor\" >Amending the Commit Message Without Opening an Editor<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#Pushing_the_Amended_Commit\" >Pushing the Amended Commit<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#Changing_an_Older_or_Multiple_Commits\" >Changing an Older or Multiple Commits<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/linuxbuz.com\/devops\/change-commit-message-in-git\/#FAQs\" >FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>There&#8217;s a lot to cover, so let\u2019s get started!<\/p>\n<h2 id=\"1\"><span class=\"ez-toc-section\" id=\"What_is_Git_Amend\"><\/span>What is Git Amend?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">The <strong>git commit &#8211;amend<\/strong> command allows you modify the most recent commit. For example, changing the commit message, adding new changes, or even removing changes. It\u2019s handy when you notice a typo or forget to include important information. However, be cautious. Amending a commit changes its history, which can cause issues if the commit has already been shared with others.<\/p>\n<h2 id=\"2\"><span class=\"ez-toc-section\" id=\"Changing_the_Most_Recent_Commit\"><\/span>Changing the Most Recent Commit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Before amending a commit, you must identify the commit that needs modification. You can view your commit history using the <strong>git log<\/strong> command.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>git log<\/code><\/pre>\n<p>This command lists recent commits, their messages, author information, and commit hashes.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>commit 1a2b3c4d5e6f7g8h9i0j (HEAD -&gt; main)\r\nAuthor: Your Name hitjethva@gmail.com\r\nDate:   Thu Jul 1 14:12:34 2024 +0000\r\n\r\n    Initial commit with a typo<\/code><\/pre>\n<p>To change the message of your most recent commit, run the <strong>git commit &#8211;amend<\/strong> command:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>git commit --amend<\/code><\/pre>\n<p>This command will open the default text editor with the existing commit message. Change it to something like:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>    Initial commit with the correct message<\/code><\/pre>\n<p>Save and close the editor. You will see the following output.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>[main 1a2b3c4] Initial commit with the correct message\r\n Date: Thu Jul 1 14:12:34 2024 +0000\r\n 1 file changed, 1 insertion(+)<\/code><\/pre>\n<p>This updates the commit message while keeping the changes intact.<\/p>\n<h2 id=\"4\"><span class=\"ez-toc-section\" id=\"Reviewing_Changes_Before_Amending\"><\/span>Reviewing Changes Before Amending<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Before amending, it\u2019s a good practice to review the changes in the commit to ensure you\u2019re modifying the correct one. Use the git show command:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>git show HEAD<\/code><\/pre>\n<p>The above command shows the changes made in the most recent commit.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>commit 1a2b3c4d5e6f7g8h9i0j (HEAD -&gt; main)\r\nAuthor: Your Name hitjethva@gmail.com\r\nDate:   Thu Jul 1 14:12:34 2024 +0000\r\n\r\n    Initial commit with the correct message\r\n\r\ndiff --git a\/file.txt b\/file.txt\r\nnew file mode 100644\r\nindex 0000000..e69de29<\/code><\/pre>\n<h2 id=\"5\"><span class=\"ez-toc-section\" id=\"Amending_the_Commit_Message_Without_Opening_an_Editor\"><\/span>Amending the Commit Message Without Opening an Editor<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">If you prefer to amend the commit message without opening an editor, you can use the <strong>-m<\/strong> option followed by the new message:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>git commit --amend -m \"New commit message\"<\/code><\/pre>\n<p>Output:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>[main 1a2b3c4] New commit message\r\n Date: Thu Jul 1 14:12:34 2024 +0000\r\n 1 file changed, 1 insertion(+)<\/code><\/pre>\n<p>This method is faster and more convenient when you already know the exact message you want to use.<\/p>\n<h2 id=\"6\"><span class=\"ez-toc-section\" id=\"Pushing_the_Amended_Commit\"><\/span>Pushing the Amended Commit<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">Once you&#8217;ve amended the commit, you must push the changes to the remote repository. Since amending a commit changes its hash, you must use the <strong>&#8211;force<\/strong> option to <strong>push<\/strong> the changes:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>git push --force origin branch-name <\/code><\/pre>\n<p>Output:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>Enumerating objects: 5, done.\r\nCounting objects: 100% (5\/5), done.\r\nDelta compression using up to 4 threads\r\nCompressing objects: 100% (3\/3), done.\r\nWriting objects: 100% (3\/3), 321 bytes | 321.00 KiB\/s, done.\r\nTotal 3 (delta 2), reused 0 (delta 0)\r\nTo https:\/\/github.com\/yourusername\/your-repo.git\r\n + 1a2b3c4d5e6f7g8h9i0j...1a2b3c4d5e6f7g8h9i0j HEAD -&gt; main (forced update)<\/code><\/pre>\n<p style=\"text-align: justify;\"><strong>Note:<\/strong> <em>Be cautious with force pushing, especially in shared repositories, as it can overwrite commits in the remote repository.<\/em><\/p>\n<h2 id=\"1\"><span class=\"ez-toc-section\" id=\"Changing_an_Older_or_Multiple_Commits\"><\/span>Changing an Older or Multiple Commits<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To change an older commit or multiple commits, you can use Git\u2019s interactive rebase feature.<\/p>\n<p>Start an interactive <strong>rebase<\/strong> for the last <strong>2 commits<\/strong>:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>git rebase -i HEAD~2<\/code><\/pre>\n<p>Output.<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto;\"><code>pick 1a2b3c4 Initial commit with a typo\r\npick 5f6g7h8 Another commit message\r\n\r\n# Rebase 9f8e7d6..5f6g7h8 onto 9f8e7d6 (2 commands)\r\n#\r\n# Commands:\r\n# p, pick  = use commit\r\n# r, reword  = use the commit, but edit the commit message\r\n# s, squash  = use commit, but meld into previous commit\r\n# f, fixup  = like \"squash\", but discard this commit's log message\r\n# x, exec <command><\/command> = run command (the rest of the line) using shell\r\n# b, break = stop here (continue rebase later with 'git rebase --continue')\r\n# d, drop  = remove commit\r\n# l, label <label> = label current HEAD with a name\r\n# t, reset <label> = reset HEAD to a label\r\n# m, merge [-C  | -c ] <label> [# ]<\/label><\/label><\/label><\/code><\/pre>\n<p style=\"text-align: justify;\">In the above editor, change the <strong>pick<\/strong> to <strong>reword<\/strong> for the commits you want to amend. Then, save the changes and close the editor.<\/p>\n<p style=\"text-align: justify;\">For each selected commit, a new text editor window will open. Change the commit message, save the file, and close the editor.<\/p>\n<p>Push the changes to the remote repository with <strong>force<\/strong>:<\/p>\n<pre style=\"background-color: #262626; color: white; border-radius: 7px; overflow: auto; line-height: 2.4; padding: 7px;\"><code><span style=\"color: #c4c1c0; user-select: none;\"> # <\/span>git push --force origin branch-name<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: justify;\">In this article, you&#8217;ve learned to change a commit message using simple commands like <strong>git commit &#8211;amend<\/strong> for the latest commit or interactive rebase for older commits. Remember to communicate with your team when rewriting public commits to avoid confusion.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h3>1. Can I amend a commit that has already been pushed?<\/h3>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYes, but you need to force-push the amended commit, which can overwrite changes in the remote repository. Be cautious when doing this in shared projects.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h3>2. What happens if I amend a commit without pushing?<\/h3>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tIf you amend a commit but don't push it, the changes are only local. You can still revert or modify the commit before pushing it.                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t        <section class=\"sc_fs_faq sc_card \">\n            <div>\n\t\t\t\t<h3>3. How do I revert an amended commit?<\/h3>                <div>\n\t\t\t\t\t                    <p>\n\t\t\t\t\t\tYou can use git reflog to find the original commit and reset your branch to that commit using <strong>git reset --hard<\/strong> .                    <\/p>\n                <\/div>\n            <\/div>\n        <\/section>\n\t\t\n<script type=\"application\/ld+json\">\n    {\n\t\t\"@context\": \"https:\/\/schema.org\",\n\t\t\"@type\": \"FAQPage\",\n\t\t\"mainEntity\": [\n\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"1. Can I amend a commit that has already been pushed?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"Yes, but you need to force-push the amended commit, which can overwrite changes in the remote repository. Be cautious when doing this in shared projects.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"2. What happens if I amend a commit without pushing?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"If you amend a commit but don't push it, the changes are only local. You can still revert or modify the commit before pushing it.\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t,\t\t\t\t{\n\t\t\t\t\"@type\": \"Question\",\n\t\t\t\t\"name\": \"3. How do I revert an amended commit?\",\n\t\t\t\t\"acceptedAnswer\": {\n\t\t\t\t\t\"@type\": \"Answer\",\n\t\t\t\t\t\"text\": \"You can use git reflog to find the original commit and reset your branch to that commit using <strong>git reset --hard<\/strong> .\"\n\t\t\t\t\t\t\t\t\t}\n\t\t\t}\n\t\t\t\t    ]\n}\n<\/script>\n\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, you might need to change a commit message if it contains errors, such as typos, incorrect information, or missing details that are important for understanding the changes made in &hellip; <\/p>\n","protected":false},"author":1,"featured_media":5081,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"two_page_speed":[],"footnotes":""},"categories":[828],"tags":[829],"class_list":["post-5080","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","tag-git"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/comments?post=5080"}],"version-history":[{"count":5,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5080\/revisions"}],"predecessor-version":[{"id":5230,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/posts\/5080\/revisions\/5230"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media\/5081"}],"wp:attachment":[{"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/media?parent=5080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/categories?post=5080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxbuz.com\/wp-json\/wp\/v2\/tags?post=5080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}