Git

How to Change Git Commit by Using Amend Option

The `git commit` command is used to confirm any changes done in the repository, and the git keeps the history of each committed task. Sometimes the user needs to change the committed message before or after publishing the repository. The user requires to rewrite the git history to change the older or newer commit of the repository. The –amend option is used with the `git commit` command to rewrite the git history. This tutorial explains how this option can be applied to the local and remote git repository to change the git commit or git commit message.

Prerequisites:

Install GitHub Desktop.

GitHub Desktop helps the git user to perform the git-related tasks graphically. You can easily download the latest installer of this application for Ubuntu from github.com. You have to install and configure this application after download to use it. You can also check the tutorial for installing GitHub Desktop on Ubuntu to know the installation process properly.

Create a GitHub account

You will require to create a GitHub account to check the output of the commands in the remote server.

Create a local and remote repository

You have to create a local repository and publish the repository in the remote server to test the commands used in this tutorial. Go to the local repository folder to check the commands used in this tutorial.

Change the last commit:

You have to check the current status of the local repository and do some commit tasks before applying any change to the last commit.  A local repository named book-store has been used to test the commands used in this tutorial. Run the following command from the repository location to check the current commit status of the repository.

$ git commit

The following output shows that there are two untracked files named booklist.php and booktype.php.

Run the following commands to add two files in the repository with the commit message and check the status of the repository after commit.

$ git add booklist.php

$ git commit -m "booklist file has added."

$ git add booktype.php

$ git commit -m "booktype file has added."

$ git commit

The following output shows that booklist.php and booktype.php files are added with commit messages. Next, when the output of the commit command shows that the working tree is clean.

You can change the last commit message by using the editor or executing the terminal’s command. Run the following `git commit` command with –amend option to change the last commit message using a text editor.

$ git commit --amend

The following editor will open after executing the above command to modify the last commit message.

Run the following command to change the last commit message from the terminal.

$ git commit --amend -m "The booktype file has changed again."

The output shows that the last commit message has changed to “The booktype file has changed again.

Change the old commit:

The older commit or the multiple commits can be changed using the `git rebase` and the `git commit –amend` commands. The rebase command is used to rewrite the commit history, but it is not recommended to use it for those commits published already on the remote server. The rebase command is used with the number of commits to change the multiple git commits. Run the following command to change the last two commit messages. The editor will be opened with the last two commit messages after executing the command.

$ git rebase -i HEAD~2

The following output shows the last two commit messages of the repository with commit SHA values.

Write reword in place of pick on those lines of the commit message that you want to change. In the following image, the modification has been done for one commit only. This commit message will be opened for editing on another editor after saving the current file.

The commit message can be changed from the editor now. Here, the message, “The booklist file has added.” has changed to “The booklist file has changed.

If you run the rebase command again, you will see the changed commit message in the editor like the following image.

Add changes to the last commit:

Suppose the booklist.php file has been modified after committing to the repository. Now, run the following command to re-add the file again and change the commit message for this addition by using the `git commit` command with the –amend option.

$ git add booklist.php

$ git commit --amend -m "The booklist file has updated."

The following output will appear after executing the above commands.

If you don’t want to change the previous commit message after adding the file again, you have to use the –no-edit option with the –amend option with the `git commit` command. The following command will add the modified file to the repository without changing the commit message.

$ git commit --amend --no-edit

After completing all changes in the local repository, you can push the local content to the remote server by executing the push command from the terminal or by opening the repository in the GitHub Desktop application. Suppose you open the repository on the GitHub desktop and click on the Publish repository option. In that case, the following dialog box will appear to publish the modified content of the local repository to the remote server.

Conclusion:

The uses of the –amend option for changing one or more git commits have been described in this tutorial by using a demo repository. The way to change the commit with or without changing the commit message has been shown here to help the git user to understand the purpose of using the –amend option and apply it in the git repository to change the commit.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.