Introduction
Interactive rebasing in Git is a powerful feature that allows you to modify your commit history in various ways. It can be used to change commit messages, reorder commits, squash multiple commits into one, and more. This tutorial will guide you through the process of interactive rebasing using Lazygit, a simple and intuitive Git interface.
Lazygit
Here’s how to perform interactive rebasing in Lazygit:
-
Start the Rebase:
- Open Lazygit and use the
Left/Right
keys to navigate to the Commits panel. - Select the commit you want to rebase from and press
e
to start the rebase, as shown in and .
- Open Lazygit and use the
-
Configure Commit Actions:
- Move up and down through the commits above your selected commit.
- Set their behavior. The default is “pick”. Here are the options:
- pick (
p
): Keep the commit as is. - drop (
d
): Delete this commit. - edit (
e
): Edit the commit. - squash (
s
): Merge the commit into the prior commit and append this commit’s message. - fixup (
f
): Merge the commit into the prior commit and discard this commit’s message. - reword (
r
): Change the commit’s message (currently not functional).
- pick (
-
Moving Order of Commits:
- In the Commits window, press
Ctrl + J
orCtrl + K
to move commits up or down.
- In the Commits window, press
-
Save the Rebase:
- When done, press
m
to pull up the Rebase Options menu as depicted in . - The before and after state of your commit history can be observed in and .
- When done, press
-
Handle Merge Conflicts:
- Be aware that this process might result in merge conflicts that you’ll need to resolve manually.
-
Additional Resources:
- For more insights, refer to the description by the Lazygit creator on interactive rebasing at Opensource.com.
Basic Terminal
To perform interactive rebasing in the terminal:
-
Start the interactive rebase:
Replace
<commit-hash>
with the hash of the commit just before the one you want to start rebasing from. -
In the interactive rebase editor, set the desired action for each commit (pick, squash, fixup, etc.).
-
Save and exit the editor, and follow the prompts to complete the rebase.
-
Resolve any merge conflicts that arise during the process.
Conclusion
Interactive rebasing in Lazygit offers a user-friendly way to alter your commit history, providing a visual and intuitive interface. The terminal approach, while more text-based, offers the same level of control. Interactive rebasing is a powerful tool but should be used with caution, especially on shared branches or after changes have been pushed to a remote repository.