Posted on 2024-07-19 by Matt.
git rebase --interactive
docs or git rebase --help
Rebase the current branch onto master
, interactively
# long flag
$ git rebase --interactive master
# short flag
$ git rebase -i master
Commands will be run top to bottom.
One way to make changes to a commit that's not the last commit. Make your changes, then stage them. Then make a fixup commit:
$ git commit --fixup <target commit>
Rebase onto master
, automatically moving the fixup commit right after the target commit and targeting it for fixup
, which will update the target commit.
$ git rebase -i --autosquash master
Another way to make changes to a commit that's not the last commit. If you get too many conflicts when using the Fixup Workflow, this can be an easier way.
Start the interactive rebase, and mark the target commit as amend
pick 8681b76 Fix table
amend 597e8f8 Update profile header
pick dbde947 Add contact link
The rebase will stop after the target commit. Make your changes, then stage them. Then amend the commit and continue:
$ git commit --amend --no-edit
$ git rebase --continue
For times where you have one commit in the history that you'd like to split up.
Start the interactive rebase, and add break
on the line after the target commit
pick 8681b76 Fix table
pick 597e8f8 Update profile header
break
pick dbde947 Add contact link
The rebase will stop at the break
.
Reset to the previous commit, leaving your changes in the working dir.
It will be as if you made all these changes and just haven't made the commit yet.
$ git reset HEAD~
Then stage and commit the pieces you'd like to, making as many commits as you want. When finished, continue the rebase
$ git rebase --continue
Start the interactive rebase, and add exec <command>
on the line after the commit(s) you want to run the command after.
pick 8681b76 Fix table
pick 597e8f8 Update profile header
exec go test ./...
pick dbde947 Add contact link
The rebase will stop at the first non-zero exit code. Make any amends or continue with
$ git rebase --continue
Run go test ./...
after every commit
$ git rebase master --exec "go test ./..."