Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Amending Commits | Undoing Changes
Git Essentials

bookAmending Commits

In the course of development, it's not uncommon to realize that a committed change needs modification or additional information. Git provides powerful tools to amend commits, allowing developers to do the following things:

  • refine commit messages;
  • add more changes, or even split and squash commits.

Modifying the Last Commit Message

The simplest form of amending a commit is changing its message. If you want to adjust the most recent commit message, use the following command:

git commit --amend -m "New and improved commit message"

The latest commit will then be overwritten with the message changed to a new one.

Alternatively, you can simply run the git commit --amend command without the -m flag. This command opens the default text editor with the previous commit message. Modify the message, save, and close the editor to amend the commit.

Now modify the latest commit message in the repository. First, view the latest commit:

Checking the latest commit

The commit message is acceptable, but it can be improved by specifying that the added file is a text file. Verify that the working tree and staging area are empty:

Checking status

As you can see, the working tree is clean and the staging area is empty, so now modify the message of the latest commit:

Modifying commit message

Then confirm that the latest commit was overwritten by displaying the two most recent commits:

Two latest commits

There is only one commit with the addition of the recipe.txt file, so the latest commit was indeed overwritten.

Adding Changes to the Last Commit

To add changes to the last commit, first, stage the additional changes using git add. Then, run the git commit --amend command:

git commit --amend -m "New commit message"

When you run git commit --amend, the staged changes are committed, and the previous commit is overwritten with the new message. You can also omit the -m flag to edit the message in a text editor.

Now add some changes to the latest commit. First, append a new line to the recipe file with the next step, then check the status of the working tree and staging area:

Modifying the file and checking status

Now add the file to the staging area and overwrite the latest commit with the new changes:

Staging changes and amending the commit

Finally, view the two most recent commits to verify that the latest commit was overwritten:

Two latest commits

There is only one commit with the addition of the recipe.txt file, so the latest commit was indeed overwritten.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 3.57

bookAmending Commits

Swipe to show menu

In the course of development, it's not uncommon to realize that a committed change needs modification or additional information. Git provides powerful tools to amend commits, allowing developers to do the following things:

  • refine commit messages;
  • add more changes, or even split and squash commits.

Modifying the Last Commit Message

The simplest form of amending a commit is changing its message. If you want to adjust the most recent commit message, use the following command:

git commit --amend -m "New and improved commit message"

The latest commit will then be overwritten with the message changed to a new one.

Alternatively, you can simply run the git commit --amend command without the -m flag. This command opens the default text editor with the previous commit message. Modify the message, save, and close the editor to amend the commit.

Now modify the latest commit message in the repository. First, view the latest commit:

Checking the latest commit

The commit message is acceptable, but it can be improved by specifying that the added file is a text file. Verify that the working tree and staging area are empty:

Checking status

As you can see, the working tree is clean and the staging area is empty, so now modify the message of the latest commit:

Modifying commit message

Then confirm that the latest commit was overwritten by displaying the two most recent commits:

Two latest commits

There is only one commit with the addition of the recipe.txt file, so the latest commit was indeed overwritten.

Adding Changes to the Last Commit

To add changes to the last commit, first, stage the additional changes using git add. Then, run the git commit --amend command:

git commit --amend -m "New commit message"

When you run git commit --amend, the staged changes are committed, and the previous commit is overwritten with the new message. You can also omit the -m flag to edit the message in a text editor.

Now add some changes to the latest commit. First, append a new line to the recipe file with the next step, then check the status of the working tree and staging area:

Modifying the file and checking status

Now add the file to the staging area and overwrite the latest commit with the new changes:

Staging changes and amending the commit

Finally, view the two most recent commits to verify that the latest commit was overwritten:

Two latest commits

There is only one commit with the addition of the recipe.txt file, so the latest commit was indeed overwritten.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 3
some-alt