Making changes to the most recent git commit (Learning about git amend)

Making changes to the most recent git commit (Learning about git amend)

·

2 min read

Git provides you with a number of ways to go back to the already committed code and edit something which is basically manipulating Git history. Sounds like a tough job but it enables you to do these things in situations that require such actions.

One of the easiest ones that I found is the Git amend option. Which basically allows you to do more changes to the last commit. Now, it will be all that nice and handy tool to use if we intend to use it as described.

git commit --amend -m 'edited message'

It will commit the changes and give a new message to the last commit. However, if the message does not need to be changed then-

git commit --amend --no-edit

Adding the no-edit option will skip that step of updating the message.

💡Git amend comes in handy when:

  • You forgot to add something or need to remove something from that commit

  • When you have some progress to save but it is not ready for commitment and you do not want to get into stashing. It allows you to easily switch branches and work on something else, and you don't need to keep worrying about forgetting the stash that you did recently.

However, that is not all. Git amend can do changes to the last commit regardless of whether it is a local one or remote. And it not only do changes to the last commit but also change the commit hash which is almost like getting rid of the last commit and making a new one.

🚩 This means it could be a trouble if this is performed on a remote commit and a common working branch. In this case, the other developers now have their work based on a commit that does not exist remotely. Ideally, it should be avoided but if it is necessary, it should be communicated to other developers first.

instagram.com/p/CFPuPaLgb-v/?igshid=YmMyMTA..

👉 Read more about amend at git-scm.com

Thank you. 👋