How to Correct Past Commit Messages

Asked 2 years ago, Updated 2 years ago, 43 views

For example, if you want to modify three previous commit messages,

  • git log
  • git rebase-i commit ID
  • r
  • Rewrite and Save

It is stated thatIs there any easier way to do it?

git

2022-09-30 19:34

3 Answers

Git rebase-i<commit-id> and save effort.
The following assumptions assume that bash is running.

 git log --pretty=oneline | cat -- number-nonblank; select id in$( git log --pretty=oneline | cut --delimiter='--fields=1); do gate rebase-i "$id"; break; done

Unfortunately, I couldn't find a way to make it a git alias, but I was able to write it down in bashrc as a function.

selrb(){ 
    git log --pretty=oneline | cat -- number-nonblank & & select id in$ (git log --pretty=oneline | cut --delimiter='--fields=1);
    do
        git rebase-i "$id";
        break;
    done
}


2022-09-30 19:34

You can edit commit messages directly with git commit --amend, but I think the only way to rewrite two or more previous commit messages is by rebase.


2022-09-30 19:34

rewrite-history.sh

#!/bin/bash

refspec = "$1"
export MSG = "$2"
export REWRITE_REF=$(git log-n1 --format=%H"$refspec")
git filter-branch --msg-filter'
      if [$GIT_COMMIT=$REWRITE_REF]
      then
        printf "%s\n" "$MSG"
      else
        cat
      fi
    ' "$refspec"^..HEAD

How-to

path/to/rewrite-history.sh master^3 "commit message"

Notes

  • Rewrite the commit in filter-branch, so the rewrite commit transition, all sha, will be replaced.


2022-09-30 19:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.