How to Change Last Git Commit Message After Pushing?

To change the recently pushed git commit message, you would need to do the following:

# 1. checkout the branch on your local
git checkout <branch-name>

# 2. amend the commit message
git commit --amend -m "New message"

# 3. force-push to update remote repository
git push --force-with-lease <remote-name> <branch-name>

In the third step, you could also use the --force flag instead of --force-with-lease. However, when you're working in teams, it is generally a safer option to use --force-with-lease as it will abort if there are any upstream changes to the same branch after your last commit. In contrast to this, --force would destroy any pushed changes (after your last commit) to the same branch.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.