Interactive Rebasing
Interactive rebasing is a powerful tool that allows you to modify a series of commits. You can reorder, edit, squash, or even delete commits. This is incredibly useful for cleaning up your commit history before merging or pushing your changes.
Let's create a series of commits to work with:
echo "First change" >> hello.txt
git commit -am "First change"
echo "Second change" >> hello.txt
git commit -am "Second change"
echo "Third change" >> hello.txt
git commit -am "Third change"
These commands create three new commits, each adding a line to our "hello.txt" file.
Now, let's start an interactive rebase:
git rebase -i HEAD~3
This command tells Git to start an interactive rebase on the last three commits. The ~3 means "three commits before the current one".
This will open your default text editor with a list of the last three commits. You'll see something like this:
pick abc1234 First change
pick def5678 Second change
pick ghi9101 Third change
You can change the word 'pick' to:
- 'r' or 'reword' to change the commit message
- 's' or 'squash' to combine the commit with the previous commit
- 'e' or 'edit' to pause the rebase and allow you to make changes
- 'd' or 'drop' to remove the commit entirely
Let's squash the second commit into the first and reword the third. Change your file to look like this:
pick abc1234 First change
s def5678 Second change
r ghi9101 Third change
Tip: If you're using Vim:
- Press
i
to enter insert mode
- Make your changes
- Press
Esc
to exit insert mode
- Type
:wq
and press Enter
to save and quit
Save and close the file. Git will first combine the first two commits and prompt you to edit the commit message. When the editor opens for this:
- You'll see both commit messages. Edit them to create a single, coherent message.
- For example, you might change it to: "First and second changes combined"
- Save and close this file.
Next, Git will pause and allow you to reword the third commit message. When the editor opens again:
- Change the commit message to something like: "Third change - revised message"
- Save and close this file.
After the rebase is complete, check your log:
git log --oneline
The output should look something like this:
86439a7 (HEAD -> master) Third change - revised message
a060d57 First and second changes combined
e35ef24 Add new feature
126b976 Revert "Add line to be reverted"
a2dfaa6 Add line to be reverted
9e4dd71 Initial commit with important note
You should now see fewer commits than before, with the first one combining the first and second changes, and the second one having the revised message for the third change.
Interactive rebasing is a powerful tool, but remember: just like with amending commits, you should avoid rebasing commits that have been pushed to a shared repository, as it rewrites history and can cause problems for other developers.
If you encounter any issues during the rebase, don't panic! You can always use git rebase --abort
to cancel the rebase and return to your previous state.