Automated Interactive Rebase for AI Agents and Scripts
When an AI agent or automated script needs to modify a specific commit in Git history, it typically cannot interact with terminal text editors like vim or nano that git rebase -i spawns by default.
To work around this, you can use a combination of Git's --fixup, --autosquash, and a temporary environment variable to perform a fully automated "interactive" rebase.
Step-by-Step Guide
1. Make your changes
Make the necessary modifications to the files in your workspace and stage them using git add:
git add path/to/modified/file.c
2. Create a fixup commit
Create a new commit using the --fixup flag, pointing it to the hash of the commit you want to amend.
git commit --fixup <target-commit-hash>
What this does: This creates a standard commit, but prefixes the commit message with fixup! followed by the subject line of the target commit.
3. Run the automated rebase
Execute the interactive rebase targeting the parent (or any older ancestor) of the commit you are modifying, using --autosquash and bypassing the editor.
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash <target-commit-hash>^
What this does:
--autosquash: Tells Git to automatically look forfixup!commits and rearrange the interactive rebase "todo" list so they immediately follow their target commits with the action set tofixup.GIT_SEQUENCE_EDITOR=true: Git normally halts and opens a text editor (likevim) to let you review the rebase todo list. By setting the sequence editor to the shell commandtrue(which immediately exits with a0success code), Git behaves as if the user opened the editor, made no manual changes, saved, and closed it instantly. The rebase then proceeds automatically.
Conflict Handling
The rebase may fail with merge conflicts. Check the exit code:
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash <target-commit-hash>^
if [ $? -ne 0 ]; then
# Conflicts detected — abort and report
git rebase --abort
echo "Rebase failed due to conflicts. Manual resolution needed."
fi
If the rebase conflicts:
- Abort with
git rebase --abortto restore the original state - Report the conflict to the user — do not attempt to resolve automatically
- The fixup commit still exists and can be retried after the user resolves the underlying issue
NEVER use --no-verify to bypass hook failures during the rebase. If pre-commit hooks fail, fix the underlying issue and retry.
Summary
# 1. Stage changes
git add <files>
# 2. Create fixup commit
git commit --fixup <commit-to-change>
# 3. Rebase and squash automatically
GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash <commit-to-change>^