Register to get access to 15+ free programming courses with interactive exercises

Changing the last commit Git fundamentals

It is very common for developers to commit and immediately realize that they forgot to add some files using git add. The rest of the changes can be sent with the next commit, or, if the changes have not yet been sent to an external system, you can add the changes to the current commit. To do this, add the --amend flag while committing:

echo 'experiment with amend' >> INFO.md
echo 'experiment with amend' >> README.md
git add INFO.md
# You forgot to prepare README.md for the commit
git commit -m 'add content to INFO.md and README.md'

[main 256de25] add content to INFO.md and README.md
 1 file changed, 1 insertion(+)

git status

On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   README.md

# You saw that you forgot to add a file. Add

git add README.md
git commit --amend
# After this command, the editor will open, waiting to enter the commit description
# Here you can change the message or quit the editor, leaving the old one behind

[main d96151a] add content to INFO.md and README.md
 Date: Sat Sep 26 16:02:07 2020 -0400
 2 files changed, 2 insertions(+)

git status

On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

In reality --amend does not add changes to an existing commit, this flag causes a commit to be rolled back (via reset) and a new commit with new data to be executed. This is why we see exactly one commit, even though the git commit command was run twice (first one was a commit we then fix).

The --no-edit option can be added to the git commit --amend command to avoid opening an editor to enter a commit description. In this case, the commit description will not change.


Do it yourself

  1. Follow all of the steps in the lesson
  2. Upload the changes to GitHub

Recommended materials

  1. Rewriting History

Аватары экспертов Хекслета

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From zero to a developer. Refunds in case you won't get a job

Frontend Developer icon
Profession
New
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions

Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.