Skip to content

Command: git reword

kain88-de edited this page May 4, 2024 · 59 revisions

Description

Available since v0.3.10.

git reword is used to edit commit messages of commits anywhere in your commit graph, without having to check out that commit first. You can specify replacement messages directly on the command line, or you can update or replace messages with the editor of your choice. Think of it as a faster, more focused replacement for the reword command available during interactive rebasing (i.e. git rebase -i).

Usage

git reword has several different modes of action, depending on if a message is provided on the command line and on the number of commits specified on the command line (e.g. none, one, or multiple).

Command Commit(s) affected Message
git reword HEAD Update existing message in $EDITOR
git reword <commit> <commit> Update existing message in $EDITOR
git reword <multiple commits> <multiple commits> Create new message in $EDITOR 1, 2
git reword -m <message> HEAD Replace existing message with <message>
git reword <commit> -m <message> <commit> Replace existing message with <message>
git reword <multiple commits> -m <message> <multiple commits> Replace existing message with <message> 1

1 When <multiple commits> are specified on the command line, the same message will be applied to all of them.

2 This mode will be changed in the future to allow updating multiple commit messages individually.

Example

Consider the following state:

$ git smartlog
⋮
◇ abc123 2h (remote origin/master) docs: update CHANGELOG.md
┃
◯ def456 54m `bug-report` command
┃
● ghi789 0s (ᐅ bug-report) use `bugreport` lib

To update the message of the current commit, run:

$ git reword -m 'feat(bug_report): use `bugreport` library to collect extra information'
Attempting rebase in-memory...
[1/1] Committed as: jkl012 feat(bug_report): use `bugreport` library to collect extra information
branchless: processing 1 update: branch bug-report
branchless: processing 1 rewritten commit
branchless: running command: git checkout bug-report
Previous HEAD position was ghi789 use `bugreport` lib
Switched to branch 'bug-report'
branchless: processing checkout
In-memory rebase succeeded.
Reworded commit ghi789 as jkl012 feat(bug_report): use `bugreport` library to collect extra information

$ git smartlog
⋮
◇ abc123 2h (remote origin/master) docs: update CHANGELOG.md
┃
◯ def456 54m `bug-report` command
┃
● jkl012 0s (ᐅ bug-report) feat(bug_report): use `bugreport` library to collect extra information

To update the message of a previous commit, run:

$ git reword def456 -m 'feat(bug_report): create `bug-report` command'
Attempting rebase in-memory...
[1/2] Committed as: mno345 feat(bug_report): create `bug-report` command
[2/2] Committed as: pqr678 feat(bug_report): use `bugreport` library to collect extra information
branchless: processing 1 update: branch bug-report
branchless: processing 1 rewritten commit
branchless: running command: git checkout bug-report
Previous HEAD position was jkl012 feat(bug_report): use `bugreport` library to collect extra information
Switched to branch 'bug-report'
branchless: processing checkout
In-memory rebase succeeded.
Reworded commit def456 as mno345 feat(bug_report): create `bug-report` command

$ git smartlog
⋮
◇ abc123 2h (remote origin/master) docs: update CHANGELOG.md
┃
◯ mno345 0s feat(bug_report): create `bug-report` command
┃
● pqr678 0s (ᐅ bug-report) feat(bug_report): use `bugreport` library to collect extra information

Using git reword to simplify squashing of branches

Using the "apply one message to many commits" mode, you can simplify the process of squashing a branch into a single commit. Consider the following state:

$ git smartlog
⋮
◇ b4f4652 4m (main) solve the things
┃
◯ 43546c8 2m wip
┃
◯ 6497856 2m more wip
┃
◯ e3e629b 1m whoops
┃
● 96acdf5 5s (ᐅ new-feature) it works!

Congratulations, you got your feature working! Now lets use reword and rebase to clean up those intermediate commits before pushing your change.

First, let's turn all of your working commits into fixup commits with git rev-list, xargs and git reword. Note that 43546c8 is the first commit on your feature branch, so git rev-list 43546c8.. will list the SHA hashes of all commits between 43546c8 (exclusive) and HEAD (inclusive).

$ git rev-list 43546c8.. | xargs git reword -m 'fixup! wip'
Attempting rebase in-memory...
[1/3] Committed as: c1a0298 fixup! wip
[2/3] Committed as: 52fcc0b fixup! wip
[3/3] Committed as: 1945c18 fixup! wip
branchless: processing 1 update: branch new-feature
branchless: processing 3 rewritten commits
branchless: running command: git checkout new-feature
Previous HEAD position was 96acdf5 it's working!
Switched to branch 'new-feature'
branchless: processing checkout
In-memory rebase succeeded.
Reworded commit 96acdf5 as 1945c18 fixup! wip
Reworded commit e3e629b as 52fcc0b fixup! wip
Reworded commit 6497856 as c1a0298 fixup! wip
Reworded 3 commits with same message. If this was unintentional, run: git undo

Next, let's run rebase to squash everything down into a single commit. Here, we use --autosquash so that git rebase will automatically apply the fixup command to your newly-reworded fixup! commits, and we use GIT_SEQUENCE_EDITOR=: to tell git to run the rebase plan (with fixup commands) without opening the editor.

$ GIT_SEQUENCE_EDITOR=: git rebase --interactive --autosquash main
branchless: processing 1 update: ref HEAD
branchless: processed commit: 7d38c0b # This is a combination of 2 commits. # This is the 1st commit message:
branchless: processing 1 update: ref HEAD
branchless: processed commit: 8ab4ee5 # This is a combination of 3 commits. # This is the 1st commit message:
branchless: processing 1 update: ref HEAD
branchless: processed commit: 78c4af4 wip
branchless: processing 1 update: branch new-feature
branchless: processing 4 rewritten commits
Successfully rebased and updated refs/heads/new-feature.

Finally, we use git reword one more time to apply a more useful commit message to our newly-squashed commit and git smartlog to review our progress.

$ git reword -m 'feat(my new feature)'
Attempting rebase in-memory...
[1/1] Committed as: d5f70ba feat(my new feature)
branchless: processing 1 update: branch new-feature
branchless: processing 1 rewritten commit
branchless: running command: git checkout new-feature
Previous HEAD position was 78c4af4 wip
Switched to branch 'new-feature'
branchless: processing checkout
In-memory rebase succeeded.
Reworded commit 78c4af4 as d5f70ba feat(my new feature)

$ git smartlog
⋮
◇ b4f4652 29m (main) solve the things
┃
● d5f70ba 2s (ᐅ new-feature) feat(my new feature)
Clone this wiki locally