Interactive Rebase: Clean up your Commit History
This article is part of our ongoing "Advanced Git" series. Stay tuned for future installments by following Tower on Twitter or subscribing to their newsletter.
Interactive Rebase: Your Git History's Best Friend
Interactive rebase is a powerful Git tool, offering a wide range of options for refining your local commit history before sharing changes with your team. Think of it as the Swiss Army knife of Git commands. Let's explore its capabilities and practical applications.
Advanced Git Series:
- Part 1: Crafting the Ideal Git Commit
- Part 2: Mastering Git Branching Strategies
- Part 3: Enhanced Collaboration with Pull Requests
- Part 4: Resolving Merge Conflicts
- Part 5: Rebase vs. Merge: A Detailed Comparison
- Part 6: Interactive Rebase (You're Here!)
- Part 7: Cherry-Picking Commits: A Targeted Approach
- Part 8: Git Reflog: Recovering Lost Commits
Reshaping Your Git History
Interactive rebase empowers you to modify your commit history for improved organization and clarity. Key actions include:
- Adjusting commit messages
- Merging multiple commits
- Splitting and editing existing commits
- Reordering commits
- Removing commits
Important Note: Interactive rebase rewrites your commit history, assigning new hash IDs to affected commits. Since commit IDs are crucial identifiers (SHA-1 checksums), this creates entirely new commits. Therefore, never use interactive rebase on commits already pushed to a shared remote repository. Doing so could disrupt your colleagues' work. Use it to clean up your local history before merging and pushing to a shared branch.
The Interactive Rebase Process
Regardless of the specific operation (deleting, messaging, combining, etc.), the workflow remains consistent:
- Identify the Commit Range: Determine how far back in your history you need to go.
- Initiate Interactive Rebase: Start the session to begin editing your history.
- Manipulate Commits: Reorder, delete, combine, or edit as needed.
-
Review History (Optional): Use
git log
to examine your project's history before starting.
Let's illustrate with examples:
- Modifying a commit message
- Combining commits
- Splitting a commit
- Deleting a commit
Modifying a Commit Message
For the most recent commit, git commit --amend
offers a simpler solution. This opens your default editor to modify the message and content. However, avoid amending pushed commits.
For older commits, use interactive rebase:
git rebase -i HEAD~3
This opens an editor showing the three commits. Change pick
to reword
to modify the message. Save, close, and edit the message again before saving and exiting.
Combining Two Commits
To combine commits (e.g., "7b2317cf Change the page structure" and "6bcf266 Optimize markup"), determine the base commit and use:
git rebase -i HEAD~3
Change pick
to squash
on the second commit (combining it with the one above it). Save and close. A new editor window appears to create a combined commit message.
Tower Tip: In Tower, drag and drop commits to squash, or right-click to edit commit messages.
Deleting a Commit
Use the drop
keyword to remove a commit:
drop 0023cdd Add simple robots.txt pick 2b504be Change headlines for about and imprint pick 6bcf266 Optimizes markup structure in index page
Recovering from Mistakes
If you need to undo an interactive rebase, use:
git rebase --abort
Expanding Your Git Expertise
This covers just a fraction of interactive rebase's capabilities. Explore its full potential and other advanced Git techniques with our free "Advanced Git Kit" (a collection of short videos).
Happy rebasing! Join us next time for more "Advanced Git" insights!
Advanced Git Series:
- Part 1: Crafting the Ideal Git Commit
- Part 2: Mastering Git Branching Strategies
- Part 3: Enhanced Collaboration with Pull Requests
- Part 4: Resolving Merge Conflicts
- Part 5: Rebase vs. Merge: A Detailed Comparison
- Part 6: Interactive Rebase (You're Here!)
- Part 7: Cherry-Picking Commits: A Targeted Approach
- Part 8: Git Reflog: Recovering Lost Commits
The above is the detailed content of Interactive Rebase: Clean up your Commit History. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

If we need to show documentation to the user directly in the WordPress editor, what is the best way to do it?

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

Questions about purple slash areas in Flex layouts When using Flex layouts, you may encounter some confusing phenomena, such as in the developer tools (d...
