简体   繁体   中英

How to amend a specific commit message in Git?

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 ABCDE 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
  1. Find that commit, using git reflog and then find its sha-id
  2. 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)

  3. Now do a commit --amend to the commit.....

  4. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM