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

Conteúdo do Curso

Git Essentials

Git Essentials

1. Introduction to Git
2. More Advanced Interaction
3. Undoing Changes
4. Working with Branches in Git

book
Amending 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:

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.

Let’s now modify the latest commit message in our repository. First, we will take a look at the latest commit:

The commit message is generally alright, however we can specify that the file we added is a text file. Let’s verify that our working tree and staging area are empty:

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

We can then check that the latest commit was simply overwritten by displaying the 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:

When we run git commit --amend, the changes in the staging area will be committed, and the previous commit will be overwritten with the message specified. Once again, you can omit the -m flag and write the commit message in the text editor.

Let’s add some changes to our latest commit. First, we’ll add a new line to our recipe file with another step and check the status of our working tree and staging area:

Now, let’s add the file to the staging area and overwrite the latest commit with the addition of our new changes:

Finally, let’s take a look at the two latest commits and verify that the latest commit was overwritten:

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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 3
We're sorry to hear that something went wrong. What happened?
some-alt