This script provides an interactive way to rewrite Git commit messages using git rebase -i
. It simplifies the process of modifying your commit history, making it easier to adhere to conventions like Conventional Commits.
The main goal of this script is to offer a user-friendly interface for an otherwise complex Git operation: interactively rebasing commits to change their messages. It guides the user through the process, from selecting which commits to reword to handling potential pauses during the rebase.
- Git installed on your system.
- A basic understanding of Git commands.
-h
,--help
: Show the help message and exit.-d
,--default
: Use default Git editor selection (checksGIT_EDITOR
, thenEDITOR
, then falls back tonano
).-e <EDITOR>
,--editor=<EDITOR>
: Specify the Git editor to use (e.g.,nano
,vim
,code --wait
).
The script performs the following key functions:
- Handle Uncommitted Changes: Detects if there are uncommitted changes and prompts the user to either stash them temporarily or exit the script.
- Select Rebase Scope: Allows the user to choose whether to reword commits from:
- The very first commit (root).
- The last N commits.
- A specific commit by its hash.
- Interactive Rebasing: Initiates
git rebase -i
with the chosen scope. - Guidance for Editor: Provides clear instructions on how to use the interactive rebase editor (defaulting to Nano if
GIT_EDITOR
orEDITOR
are not set, or using the specified editor):- How to change
pick
toreword
(orr
). - How to use
edit
(ore
) to pause the rebase at a specific commit for inspection, conflict resolution, or making additional changes. - Instructions for saving and closing editor files.
- How to change
- Conventional Commits Reminder: Reminds users to follow Conventional Commits guidelines when rewriting messages.
- Rebase Pause Handling: Detects if
git rebase
has paused (e.g., due to anedit
command or conflicts) and offers options tocontinue
,abort
, orexit
the script while leaving the rebase in a paused state. - Post-Rebase Instructions: After successful rebase, it reminds the user to force push changes to the remote repository if necessary and to check their Git history.
- Save the Script: Save the script content into a file named
reword_commits.sh
(or any preferred name) in your Git repository. - Make it Executable: Open your terminal, navigate to the script's directory, and make it executable:
chmod +x reword_commits.sh
- Run the Script: Execute the script from your terminal within your Git repository, optionally specifying an editor:
./reword_commits.sh ./reword_commits.sh --editor=vim ./reword_commits.sh -e code --wait ./reword_commits.sh --default
- Handle Uncommitted Changes (if prompted): If you have uncommitted changes, the script will prompt you to
(s) stash
them or(e) exit
. Chooses
to temporarily save your changes and proceed, ore
to exit and handle them manually. - Choose Rebase Option: Follow the on-screen prompts to select how you want to rewrite your history:
- Enter
1
to reword commits from the first commit (root). - Enter
2
to reword the last N commits (you'll be asked for the number). - Enter
3
to reword a specific commit by its full hash (you'll be asked for the commit hash).
- Enter
- Interactive Editor Instructions: Carefully read the on-screen instructions before pressing Enter. These instructions explain how to modify the rebase plan in your default text editor (e.g., Nano, Vim, VS Code).
- To modify a commit message, change
pick
toreword
(orr
) next to the commit hash. - To pause the rebase at a specific commit for inspection, conflict resolution, or making additional changes, change
pick
toedit
(ore
). If you choose this, the script will pause and provide options to continue or abort.
- To modify a commit message, change
- Rewrite Commit Messages: For each commit you marked
reword
, your configured editor will open. Change the commit message as desired, adhering to Conventional Commits standards (e.g.,feat(scope): descriptive message
). Save and close the editor file to proceed to the next commit or complete the rebase. - Handle Pauses (if any): If the rebase pauses (e.g., due to an
edit
command or merge conflicts), the script will prompt you to:- Enter
c
to continue the rebase (after resolving any conflicts manually and staging the changes). - Enter
a
to abort the rebase, discarding all changes made during the rebase process. - Enter
q
to exit the script, leaving the rebase in a paused state for manual intervention.
- Enter
- Restore Stashed Changes (if applicable): If you stashed changes at the beginning of the process, the script will remind you to restore them using
git stash pop
after the rebase is successfully completed. - Force Push (if needed): After the rebase completes, if you have pushed these commits to a remote repository previously, you must force push your changes. Always check your Git history first with
git log --oneline
before force pushing:git push --force-with-lease
- Rebase Conflicts: If
git rebase
encounters conflicts, resolve them manually,git add
the resolved files, and then choosec
(continue) when prompted by the script. - Editor Issues: Ensure your specified editor (with
--editor
) is correctly configured and accessible in your PATH. If usingcode --wait
for VS Code, ensure thecode
command is available. - Script Exited Unexpectedly: If the script exits and leaves Git in a rebase state, you can manually
git rebase --continue
,git rebase --abort
, orgit rebase --quit
to resolve it.
- Caution with History Rewriting: Rewriting Git history can have significant consequences, especially if you are working on a shared branch. Always ensure you understand the implications before proceeding, as it changes the commit IDs for the rewritten commits.
- Backup: Consider backing up your branch or repository before performing extensive history rewriting. You can create a new branch as a backup using
git branch <backup-branch-name>
. - Conventional Commits: It is highly recommended to follow the Conventional Commits specification for clear, consistent, and automated changelog generation.