HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Change the last commit's message or contents in Git

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
lastchangegitcommitcontentsthemessage

Problem

Have you ever wanted to change the last commit's message or contents? Maybe you forgot to add a file, or you misspelled something in the commit message. Whatever the reason, Git has you covered with the --amend option for the git commit command.
If you only want to change the last commit's message, you can use --amend and simply add the -m option followed by the new message. This will replace the last commit's message with the new one.
If you want to change the last commit's contents, you can use --amend after staging the changes you want to add to the last commit. This will add any staged changes to the last commit, without changing its message.
If you want to keep the same commit message and only add the staged changes, you can use --no-edit to prevent Git from opening the default editor to change the commit message.

Solution

# Syntax: git commit --amend -m <message>

git add .
git commit -m "Fix the network bug"
# Creates the commit: 3050fc0 Fix network bug

git commit --amend -m "Fix the network bug"
# The last commit's message is now "Fix the network bug"
# This also changes its SHA-1 checksum


If you want to change the last commit's contents, you can use --amend after staging the changes you want to add to the last commit. This will add any staged changes to the last commit, without changing its message.
If you want to keep the same commit message and only add the staged changes, you can use --no-edit to prevent Git from opening the default editor to change the commit message.

Code Snippets

# Syntax: git commit --amend -m <message>

git add .
git commit -m "Fix the network bug"
# Creates the commit: 3050fc0 Fix network bug

git commit --amend -m "Fix the network bug"
# The last commit's message is now "Fix the network bug"
# This also changes its SHA-1 checksum
# Syntax: git commit --amend --no-edit

git add .
git commit -m "Fix the network bug"
# Creates the commit: 3050fc0 Fix network bug

# Edit or add files
git add .
git commit --amend --no-edit
# The last commit includes the edited/added files
# This also changes its SHA-1 checksum

Context

From 30-seconds-of-code: update-commit-message-or-contents

Revisions (0)

No revisions yet.