Skip to content

Git commit amendment techniques

Git commit amendment refers to the set of techniques used to modify the most recent commit in a repository's history. This can involve changing the commit message, incorporating staged file modifications, or completely undoing the last commit.^[600-developer__tools__git__git-command.md]

Modifying the last commit

The primary command for amending a commit is git commit --amend.^[600-developer__tools__git__git-command.md] This command serves two distinct purposes depending on the arguments provided:

  • Updating the commit message: Running git commit --amend without file arguments allows the user to edit the message of the most recent commit.^[600-developer__tools__git__git-command.md]
  • Including forgotten files: To add files that were staged after the initial commit, one can execute git commit --amend <file1> <file2>....^[600-developer__tools__git__git-command.md] This absorbs the specified staged files into the previous commit rather than creating a new one.

Undoing the last commit

If a user wishes to completely retract the most recent commit, the git reset command is used with specific flags to determine the state of the working directory.^[600-developer__tools__git__git-command.md]

  • Soft reset: The command git reset HEAD^ --soft cancels the last commit but retains all modified files in the staging area, allowing for immediate re-commit or further edits.^[600-developer__tools__git__git-command.md]
  • Hard reset: The command git reset HEAD^ --hard cancels the last commit and discards all changes, reverting the working directory to the clean state of the preceding commit.^[600-developer__tools__git__git-command.md]
  • [[Git Branching]]
  • [[Interactive Rebase]]

Sources

  • 600-developer__tools__git__git-command.md