Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Maxime Bréhin
Maxime Bréhin

Posted on

     

Git protip: quickly rewrite an old commit message

Just as I sometimesforget files in a commit,I also sometimes botch commit messages only to find out about it a while later (after a few more commits). My most common oversight is probably forgetting tolink the issue the commit refers to.

Later, but not too late!

Don't tell me you're thinking: *“Never mind, I'll pop open the UI and fill it in by hand!” or that you're giving up! Maybe that's because you don't know aboutinteractive rebasing, or are afraid to use it 😨. Honestly, you shouldn't be, witha little learning this command proves to be a super valuable ally!

Apart from the rebase thing, do you know that you can create a commit that expresses the intent to change the message? I bet you don't! Fortunately, I'm here 😁 to help. Let me introduce thegit commit --fixup reword:<commit-ref-to-fix> command. This is a bit of a mouthful but who cares, we're going to wrap it with a nice alias that looks kind of like a magic incantation 🔮. Introducingautoreword:

git config--global alias.autoreword'!git commit --fixup reword:$1 && git rebase --autosquash --interactive --rebase-merges $1~1 && echo "autoreword finished"'
Enter fullscreen modeExit fullscreen mode

What happens if you run this command?

🌀 An evil wormhole opens, releasing the flames of hell that reduce this world to ashes 🔥!(Actually no, we don’t even need to use magic for that 😭)

But seriously, Git will open your editor with a message starting withamend! … followed by the first line of the commit message you want to fix, then a line break and again the (full) commit message.

amend! chore(dx): setup ESLint and Prettierchore(dx): setup ESLint and Prettier# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## On branch chore/automation
Enter fullscreen modeExit fullscreen mode

You can then change the message (but keep the very first line as-is).

amend! chore(dx): setup ESLint and Prettierchore(dx): setup ESLint and Prettier# Here is the reference to my issue!Closes#42# Please enter the commit message for your changes. Lines starting# with '#' will be ignored, and an empty message aborts the commit.## On branch chore/automation
Enter fullscreen modeExit fullscreen mode

Now you can save and close the file.

Git will then create thefixup commit andour alias will automatically run the interactive rebase, starting from the faulty commit’s direct ancestor. It opens your editor one last time with the list of actions tu run so you can see thatthe fixup commit is already at the right spot in the list prefixed byfixup -C.

pick 3f0714b chore(dx): setup ESLint and Prettierfixup-C 61eecda amend! chore(dx): setup ESLint and Prettierpick 9a7cf39 chore(dx): setup Husky, lint-staged, precommit-checks, commitlint
Enter fullscreen modeExit fullscreen mode

You’ve got nothing more to do but verify, save and close the file. The rebase then runs (without conflict) andyou can verify your log to see that the commit message was updated!

You may see in the actions list some keys likelabel onto andreset onto. This is a because the rebase is called with a--rebase-merges option that preserves your local merges while rebasing. You don’t need to worry about it 😌.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I am a strong advocate of good practices and the use of technology for virtuous purposes. I strive to encourage local resilience (see transitionnetwork.org), low tech and agro-forestry.
  • Location
    France
  • Work
    Git, JS, React teacher and developer @ Delicious Insights
  • Joined

More fromMaxime Bréhin

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp