How to Add Files to the Last Commit in Git: A Comprehensive Guide to git commit --amend

Nov 21, 2025 · Programming · 12 views · 7.8

Keywords: Git | Commit Amendment | Version Control

Abstract: This article provides a detailed explanation of the correct method to add omitted files to the last commit in Git. By using the git commit --amend command, developers can avoid creating unnecessary additional commits and maintain a clean commit history. The article delves into the working principles, use cases, specific operational steps, and important considerations of --amend, including warnings about public commits and alternative solutions. Complete code examples and best practice recommendations are provided to help developers efficiently manage Git commits.

Problem Background and Common Misconceptions

In daily development work, developers often encounter situations where, after making a commit, they realize that a file that should have been included in that commit was omitted. Many developers resort to making another commit to address this issue:

git add the_left_out_file
git commit "include the file which should be added in the last commit"

While this approach solves the problem, it results in unnecessary additional commits in the commit history, making the records lengthy and less clear. The ideal approach is to directly add the omitted file to the previous commit without creating a new commit record.

Core Solution: git commit --amend

Git provides the git commit --amend command to amend the last commit. This command allows developers to modify the most recent commit, including adding omitted files, modifying commit messages, and more.

Basic Usage

To add an omitted file to the last commit, follow these steps:

git add the_left_out_file
git commit --amend --no-edit

Here, the --no-edit flag ensures that the original commit message remains unchanged. If you wish to modify the commit message as well, omit this flag, and Git will open an editor allowing you to edit the commit message.

In-depth Working Principles

git commit --amend does not simply modify the existing commit; instead, it creates a new commit to replace the original last commit. The specific process is as follows:

  1. Git merges the contents of the current staging area with the contents of the last commit
  2. Creates a new commit object containing the merged content
  3. Moves the branch pointer to the new commit
  4. The original commit, if no other references point to it, will eventually be garbage collected

Detailed Operation Examples

Adding a Single File

Suppose a developer forgot to include the config.json file in the last commit:

git add config.json
git commit --amend --no-edit

Adding Multiple Files

If multiple omitted files need to be added:

git add file1.html file2.css file3.js
git commit --amend --no-edit

Adding All Unstaged Files

To add all unstaged files to the last commit:

git add .
git commit --amend --no-edit

Important Considerations and Best Practices

Warning About Public Commits

Never use --amend on commits that have already been pushed to a public repository. Because the amend operation rewrites commit history, if other developers have already based their work on the original commit, this can cause severe version conflicts.

Suitable Scenarios

Unsuitable Scenarios

Extended Application: Modifying Commit Messages

In addition to adding files, git commit --amend can also be used to modify commit messages. If you find typos in the commit message or need to improve the description:

git commit --amend

After executing this command, Git will open the default editor, allowing you to modify the commit message. After saving and closing the editor, the commit message is updated.

Alternative Solutions Comparison

Pros and Cons of Creating a New Commit

Pros:

Cons:

Pros and Cons of Using --amend

Pros:

Cons:

Best Practices Summary

  1. During local development, prioritize using git commit --amend to perfect commits
  2. Before pushing to a remote repository, ensure all commits are complete
  3. In team collaboration, establish clear commit standards
  4. For important historical commits, consider using git revert instead of rewriting history

By appropriately using git commit --amend, developers can maintain a clearer and more professional commit history, improving the efficiency and quality of code management.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.