DEV Community


One Command to Change the Last Git Commit Message

mrahmadawais profile image Ahmad Awais ⚡️ Updated on ・1 min read

🔥 Hot-tip: Do you mess up git commit messages quite often? I DO!


Most of the time I have to amend the last git commit message. So, I made a small bash function out of it.

# Amend the last commit message.
# Push the changes to remote by force.
# USAGE: gamend "Your New Commit Msg"
function gamend() {
    git commit --amend -m "$@"
    git push --force-with-lease
Enter fullscreen mode Exit fullscreen mode

⚠️ Avoid --force unless it is absolutely necessary and you can be sure that nobody else is syncing your project at the same time.

ℹ️ Why git push --force-with-lease! If someone else pushed changes to the same branch, you probably want to avoid destroying those changes. The --force-with-lease option is the safest, because it will abort if there are any upstream changes.

✅ Amend git commit in one go
🤖 Put it in .bashrc/.zshrc etc files
👌 Sharing this quick-tip is a fun thing to do

Discussion (10)

Editor guide
flexdinesh profile image
Dinesh Pandiyan

I amend my commits all the time too but I don't push my commits to remote after every commit. I push them only when I'm sure that I have done what's necessary and have no second thoughts (mostly at the end of my day or only twice a day). This way I mostly never have to force push as I only have to amend my local commit.

Force push is a bad practice as it can mess up everyone's work if you're working in a big team.

I would suggest to create two different gamend and gamendfp so you force push only when you have to and you're absolutely sure that you're not gonna break someone else's work.

mrahmadawais profile image
madeindjs profile image
Alexandre Rousseau

I think this function is too dangerous... You should not use --force param

mrahmadawais profile image
Ahmad Awais ⚡️ Author

Can you explain if there is a better way to do this once you have committed and pushed to remote the wrong commit message? You cannot do it without --force. I'm all ears for a better solution. :)

bigray profile image

I think you can revert your commit with the wrong message.

Now, cherry-pick your commit with the wrong message, amend it, replace with the right message and push without --force.

Ok, there are a little bit more manipulation but you avoid the wrath of your team. :)

sqlrob profile image
Robert Myers

You live with the wrong commit message for that commit. Unless you are the only one on that branch, NEVER(*) use force. History rewriting is bad, and you can make others lose their work.

(*) For almost never values of never. The use cases are rare enough that it should never be scripted.

Thread Thread
mrahmadawais profile image
Ahmad Awais ⚡️ Author

You folks are right!

You're right. After sharing this, what was meant to be a personal workflow, I ended up in long debates on Fb/Twitter about how this can be improved.

Here's what we end with git push --force-with-lease as shared here above (updated the post)

But I am still not satisfied, so I am building a git hooks based workflow to make sure no bad git commit message gets committed in the first place.

Thanks for mentioning that though. Peace! ✌️

Thread Thread
sqlrob profile image
Robert Myers

If you are using a development environment that is integrated with git, --force-with-lease can have the exact same problems as --force

tux0r profile image

Hot-tip: Avoid --force unless it is absolutely necessary and you can be sure that nobody else is working on your project right now.

mrahmadawais profile image
Ahmad Awais ⚡️ Author

Forgot to mention that — mostly I ask my team to resync after this.