It’s not uncommon for a developer to discover that a certain commit message is off the mark and needs revision. Like with most things in Git, this can be achieved in more than one way.
git commit --amend`
The command fires up a message editor with the previous commit’s message as the starting point, instead of an empty one. With the
--amend option, the new commit replaces the tip of the current branch. Care should be taken, since all staged contents will be committed after running this command. If a simple message edit is all one has in mind, the index should be empty. However, the working directory might have changes and they will not be affected.
# fires up message editor git commit --amend # pass message on command line git commit --amend -m "New Message"
If the commit to be fixed is not the most recent one, then one can look to rebase. With rebase :
# get commitId with git reflog git rebase --interactive # alternatively, to edit previous n commits git rebase -i HEAD~n
This brings up an editor listing all the commits since the mentioned one. Change
reword for any of the commits listed. For editing commits beyond the commit message, change to
For each commit that one wants to reword, Git drops into the editor; for every edit, into the shell. After the required changes have been made to a commit :
# to continue editing commits with rebase git rebase --continue # OR git commit --amend # to abort rebase git rebase --abort
Dealing with unpublished changes is easy. A simple
git push will be sufficient.
A force push publishes an amended commit that has already been pushed to the remote repo.
git push --force
Note the two major side-effects of a forced-push :
The remote branch in over-written. All commits to the remote that have not been merged with the local branch will be lost.
The history of the publicly-shared branch will be re-written. This might imply problems for other team-members who have copies of the old commits that have been deleted upstream. Hence, team coordination is important when going through with such a change. See the Recovering from Upstream Rebase section of the