How Change git commit message after push
Modify the most recent commit
To modify the most recent commit, use the following command
git commit --amend
This opens an editor with the previous commit message, allowing you to update it.
If you prefer to completely replace the old message, you can use the -m
flag with a new message:
git commit --amend -m "New commit message"
For example, if your last commit message was “Fixed a bug” and you want to update it to “Fixed a critical bug in the login feature,” you can use:
git commit --amend -m "Fixed a critical bug in the login feature"
Pushing Changes
After updating your commit, you can push your changes using:
git push --force-with-lease <repository> <branch>
Example
git push --force-with-lease origin master
This command ensures that your push will fail if there are upstream changes, making it the safest option to avoid overwriting others’ work.
Alternatively, you can push changes with force:
git push --force <repository> <branch>
Example
git push --force origin master
Or use the plus sign to push forcefully:
git push <repository> +<branch>
Example
git push origin +master
Please exercise caution with these commands.
If someone else has pushed changes to the same branch, you risk erasing their contributions. Thus, using --force-with-lease
is advisable, as it will prevent you from pushing if there have been upstream changes.
If you do not specify a branch, Git uses the default push settings. Be aware that if the default push setting is “matching,” it could result in unintentional changes across multiple branches.