Introduction to git commit amend function
Git commit amend is a function in git that enables you to change or modify the last commit to a new commit. The git amend commit –m
option permits you to modify a commit message either written wrongly or has an unclear message. The git amend function also allows you to include the new file in a repository in case you omitted the file. When you use the amend function in git, it will create a new commit with a unique id. It's, therefore, not recommended to use this method to modify commits in a public repo.
In this tutorial about git commit amend, we shall learn how to use the amend command in git with examples.
Git commit amend workflow
Git amend commit command only applies to your last commit (git HEAD). In case you want to modify the entire repository history then you can apply other methods like git rebase and git reset. See the diagram below which illustrates the git commit amend
workflow.
After running git commit --amend C
, the commit C was updated instead of creating a new commit ID as illustrated by the colour changes.
Setting up the lab environment
To begin practising how to work with git commit --amend
, you will require to set up the lab environment. First, clone the remote repository git-amend
to your local workstation to use throughout this experiment. I will be working with windows 10 pro and git version 2.32.0.windows.2 for all the examples used in this tutorial.
Below is the expected output after running the git clone
command.
$ git clone https://github.com/Josephine-Techie/git-amend.git
Cloning into 'git-amend'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.
git commit amend syntax
Following syntax is used to replace the tip of the current branch by creating a new commit.
$ git commit --amend
Example-1: How to use the git amend operator to edit the latest commit message
You can use amend function to make edits to a past committed git message as follows:
In this example, we shall run several changes to the master branch
in the git-amend
local repository as follows;
$ touch myfile.css
$ git add .
$ git commit -m "afile.css"
[master 2d243aa] afile.css
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 myfile.css
While at this point, you realize that you committed a wrong message afile.css
instead of myfile.css
. So that you don't become confused moving forward, you shall have to amend the message using the git commit --amend -m
function. –m
option is added because we working to change a commit message.
$ git commit --amend -m "myfile.css edited commit message"
[master 7a40618] myfile.css edited commit message
Date: Fri Sep 3 21:05:03 2021 +0300
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 myfile.css
Applying the git amend function has enabled you to update the wrong message to the desired one. If you use the git commit amend
function without t –m
, git opens the editor. You can then add the new message and save it before exiting the editor. –m
helps to shorten that process thus a faster approach.
To see the newly amended commit we shall run the git log --oneline
command.
$ git log --oneline
7a40618 (HEAD -> master) myfile.css edited commit message
2eb2ee7 (origin/master, origin/HEAD) Initial commit
Example-2: How to modify a commit using the git commit amend function
To demonstrate how to use the git amend operator to modify a commit we shall check out a new branch my-branch
in the local repo git-amend. We will also commit some changes to it as follows;
$ git add .
$ git commit -m "newfile.py"
[my-branch 36f11b5] newfile.py
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 newfile.py
While at this stage of development, you realize that you forgot to add the feat.py
which is crucial as the two files will function together. The git amend function will be the best option to include the missed file. Let’s put that in practice using the git commit --amend --no-edit
command as follows;
$ touch feat.py
$ git add .
$ git commit --amend --no-edit
[my-branch d1f8f71] newfile.py
Date: Fri Sep 3 21:38:35 2021 +0300
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 feat.py
create mode 100644 newfile.py
The above results show that two files have been both added and the feat.py
file omission corrected.
The modified amend function with –no-edit
pointer enables you to retain the same commit message despite the changes.
You should observe caution when using the git commit amend
operator not to apply to a collaborated repository. The changes caused by the amend function are permanent and could hinder merges from other team members or cause confusion.
Using the last commit in example two above, we will use the git commit amend function to change the author from Josephine-Techie to Maureen-M.
You can change the author of a commit by running the git commit --amend --author="Author < email@email.com >"
command as follows;
$ git commit --amend --author="Author <mmulombi@email.com>" newfile.py Author: Author <mmulombi@gmail.com> Date: Fri Sep 3 21:38:35 2021 +0300 On branch my-branch Changes to be committed: new file: feat.py new file: newfile.py
You will now apply git log
command to visualize the author changed;
$ git log
commit 7a40618f90f6e42753efe37c401eb7d83e4f6690 (master)
Author: Maureen-Mulombi <mmulombi@gmail.com>
Date: Fri Sep 3 21:05:03 2021 +0300
myfile.css edited commit message
commit 2eb2ee7c2c2d40c2a1cfe1aaebf56c8a6a163b43 (origin/master, origin/HEAD)
Author: Josephine-Techie <79441151+Josephine-Techie@users.noreply.github.com>
Date: Fri Sep 3 16:10:45 2021 +0300
Initial commit
Example-4: Apply git amend function to change the commit date timestamp
Using the git amend operator can help reverse the old timestamp of a commit to the current timestamp.
Using the last commit in example 3 , we shall run git commit --amend --reset-author --no-edit
command as shown:
$ git commit --amend --reset-author --no-edit
[my-branch d06be04] newfile.py
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 feat.py
create mode 100644 newfile.py
To view the timestamp reversal you will run the git log
command as shown in the sample output below:
$ git log
commit d06be047a97e1404e8b1417573e2fa3f11e38692 (HEAD -> my-branch)
Author: Maureen-Mulombi <mmulombi@gmail.com>
Date: Fri Sep 3 25:52:39 2021 +0300
newfile.py
commit 7a40618f90f6e42753efe37c401eb7d83e4f6690 (master)
Author: Maureen-Mulombi <mmulombi@gmail.com>
Date: Fri Sep 3 21:05:03 2021 +0300
myfile.css edited commit message
commit 2eb2ee7c2c2d40c2a1cfe1aaebf56c8a6a163b43 (origin/master, origin/HEAD)
Author: Josephine-Techie <79441151+Josephine-Techie@users.noreply.github.com>
Date: Fri Sep 3 16:10:45 2021 +0300
Initial commit
You will notice the reversal of the last commit timestamp 21:05:03 2021
to the time you ran the reversal.
Summary
We have covered the following topics about the git commit amend function:
- Introduction to git commit amend
- Git amend function working examples
Further Reading
How can one change the timestamp of an old commit in Git?
How to modify existing, unpushed commit messages?