I am trying to change a commit message in SourceTree but cannot find where the option is. It has not been pushed yet.
How can I amend the message for older commits in SourceTree or command line?
I am trying to change a commit message in SourceTree but cannot find where the option is. It has not been pushed yet.
How can I amend the message for older commits in SourceTree or command line?
There is no feature to do that because how git internally work, a sha1 sealing each commit.
But you could :
do a 'amend' if the message is the one of the last commit.
do a git rebase -i
also named a rebase interactive and choose 'reword' (or 'r') for each commit you want to rewrite the commit message.
use git 'notes' to join a new comment next to the existing one (but handle it is not straightforward because you have to push the note explicitely and query them also to see them... )
Suppose the branch is like A-B-C-D-E
and you want to amend C. Here is one solution I prefer:
git reset C --hard
#do some changes and add
git commit --amend
git cherry-pick C..E
#or git cherry-pick D E
git reflog
and then find its sha-id To go to that commit , use
git reset --hard sha-id
(if you don't want to keep the changes of the current state)
or,
use git reset --soft sha-id
(if you want to keep the changes)
Now do a commit --amend to the commit.....
Now, check if that the commit , you are amending to, has already been pushed or not,
if, YES, then do a rebase and push it...
`git push ` and revert back your head to where it was earlier using its sha-id
If you don't do the above step, your branch will diverge from the remote and you will see that in the git status
else, just revert back to your commit using its sha-id
Please follow the following steps to edit the commit message of previous commits
On the command line, navigate to the repository that contains the commit you want to amend.
Use the git rebase -i HEAD~n
command to display a list of the last n commits in your default text editor.
For example
# Displays a list of the last 3 commits on the current branch
$ git rebase -i HEAD~3
The list will look similar to the following:
pick e499d89 Delete CNAME
pick 0c39034 Better README
pick f7fde4a Change the commit message but push the same commit.
# Rebase 9fdb3bd..f7fde4a onto 9fdb3bd
#
# 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
with reword
before each commit message you want to change.
NOTE: If you are using VIM then press i
to be able to edit the file. Then proceed to replace pick
with reword
and press ESC
once the changes are made.
Save and close the commit list file. (Use :wq
to save and quit vim)
In each resulting commit file, type the new commit message, save the file, and close it.
When you're ready to push your changes to GitHub, use the push --force command to force push over the old commit.
$ git push --force origin EXAMPLE-BRANCH