DEV Community

Gulshan Zaman Khan
Gulshan Zaman Khan

Posted on

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

Image GIT amend title cover


Git provides you a number of ways to go back to the already commited 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 most easiest one 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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 commit 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 it being a local one or remote. And it not only do changes to the last commit but also changes the commit hash which is almost like getting rid of the last commit and making a new one.

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

https://www.instagram.com/p/CFPuPaLgb-v/?igshid=YmMyMTA2M2Y=

👉 Read more about amend at git-scm.com

Thank you. 👋

Top comments (0)