Rewriting latest commit with git amend
Tags: git, programming, tutorial, beginners
Published: 2020-06-11 13:19:54

---
title: Rewriting latest commit with git amend
published: true
description: How to use git amend to rewrite git history
tags: git, programming, tutorial, beginners
//cover_image: https://direct_url_to_image.jpg
---

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2F0eda55fc-cd3c-4bd1-96d3-209d0a8822ee_480x480.gif)

> #### This post was originally posted in the newsletter [GitBetter](https://gitbetter.substack.com/). If you are interested in leveling up your game in Git, you can subscribe to it.

Sometimes we wish to rewrite history. Unlike in the real-world, Git provides various options to rewrite git history.

In this post, we will cover how to rewrite the latest commit in Git.

### Rewrite latest git commit message

Git provides the option to rewrite the most recent commit message.

```
git commit --amend
```

This command will open the editor with the latest commit message. You can then change the commit message and push it.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fdd94c1d2-fadf-43f4-b0c5-c188be9c6a3f_614x124.png)

In the above example, you can see that the latest commit has a typo error in the message. You can fix that typo with **amend**

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fd9891860-14e4-48a2-9d13-51b0bc6043b5_589x264.png)

The **amend** command will open an editor. Now you can edit the commit message here and save it.

Now let’s see the git log

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb45e695d-36dc-43d5-8c90-d54b257007bf_590x108.png)

The typo is fixed. And also note that the latest commit hash is also changed.

So whenever you are doing amend Git will basically rewrite the entire commit and generates a new hash for it. This is very important to understand.

There is also a shorthand for amend

```
git commit --amend -m "Your rewrite message"
```

You can pass the message directly with -m attribute. And this command will directly commit the change and won’t open the editor to edit.

Now the tricky part. Assume that you have committed some changes with wrong commit message and have pushed to the remote. After realizing the mistake you have changed the commit message with amend and you are trying to push the changes to remote.

Git won’t allow you to push the changes and you will see the error message as below.

![](https://cdn.substack.com/image/fetch/w_1456,c_limit,f_auto,q_auto:good,fl_progressive:steep/https%3A%2F%2Fbucketeer-e05bbc84-baa3-437e-9518-adb32be77984.s3.amazonaws.com%2Fpublic%2Fimages%2Fb0123cf2-0a2f-447c-8086-a986a0fe95f4_679x156.png)

Git is rejecting to push and gives warning that the local branch is behind the remote.

It means that there are new changes in the remote and those changes are not present in the local. So you should probably fix it by pulling the changes from local.

This is happening because we have used amend to change the latest commit and the remote is not aware of that.

So you should tell Git that the local changes are meant to be the updated changes and have to push it anyway.

You can achieve this by

```
git push origin develop --force
```

The above command will force the Git to push the local changes to remote.

#### Beware of —force

It is recommended not to use amend if you have already pushed the changes to remote and other developers have already started using those changes.

Because **—force** is basically rewriting the git history in remote itself. So any changes that were pushed to remote meanwhile will be erased from the history and will be gone permanently.

So it is always advisable to use **amend** when you haven’t pushed the changes to remote or if you are pretty confident that no other developers have started using those changes or no others have pushed any new changes to the current branch.

### Adding more changes to the latest commit using amend

It is also possible to add more changes to the recent commit using amend. So that you don’t need to add a new commit for missed out changes.

Assume that you have missed adding some changes that have to be the part of the last commit you have done. You can try

```
git add file_one.txt
git commit --amend
```

First, you have to add the files and then you can use **—amend** to add those changes to the last commit. This command will open the editor and you can add your updated commit message. Now the changes are added to the recent commit itself.

You can also use

```
git commit --amend -m "Updated commit message"
```

As already discussed, this is a shorthand and will accept the commit message.

Sometimes, you don’t need to update the commit message itself. You just need to add more changes to the latest commit.

This is possible by

```
git commit --amend --no-edit
```

This command will add the staged changes and leave the commit message unchanged.

**But remember, amend will always rewrite the entire commit and so it generates a new commit hash for it.**

As discussed earlier, it’s not wise to add changes to the latest commit if the changes are already pushed to the remote.

Make sure that you won’t affect other’s work by using amend. Be careful about using it.

There are also ways of rewriting old commits and also rewriting multiple commits in Git. Those topics will be covered in upcoming posts.

Thank you for reading :) :)

#### *If you have come this much, then I think you are much interested in Git. You can subscribe to my newsletter [GitBetter](https://gitbetter.substack.com/) to get tricks, tips, and advanced topics of Git.*