How to change any commit message after push in Git?

  




Commits on Git are along with a commit message that defines the changes in the code in a git respiratory. However, a situation may arise where we may have to change the git message when Git commit is unclear or sometimes spelling mistakes and other problems. In this case, you've to amend commits locally and push with edited commit messages.

Amending the last commit

To change the last commit, and you haven't pushed it to online then you can commit again using the  --amend flag.

git commit --amend

This will overwrite the last commit with a new commit message. You will get a message editor, edit the text then press Esc and save the commit by typing:wq .

If the message has to change to the latest commit then first, navigate to the respiratory in which contains that commit you to have to amend then

git commit --amend -m "This is the new message"
git commit --force repositoryName branchName


Changing the older or multiple commits

If you wanted to change the multiple or older commits then you can use the interactive rebase and fix/change the commits.
First, navigate to your respiratory that contains commits you wanted to change in the command line then,
git rebase -i HEAD~n

It will display the last n commits. For example to display last 5 commits in the current branch then,

git rebase -i HEAD~5

This will look like this,

pick 04f9d19 Added README
pick f0c8644 Added DeviceInformation
pick f895424 Fixed Some Issues

# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Replace pick word with replace word like this :

reword 04f9d19 Added README
reword f0c8644 Added DeviceInformation
pick f895424 Fixed Some Issues

Then save commit (Press Esc and type :wq )

Then you'll get a text editor, edit each commit message then force push the changed commits by 

git push --force

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.