Table of Contents
Advanced Git Series:
Reshaping Your Git History
The Interactive Rebase Process
Modifying a Commit Message
Combining Two Commits
Deleting a Commit
Recovering from Mistakes
Expanding Your Git Expertise
Home Web Front-end CSS Tutorial Interactive Rebase: Clean up your Commit History

Interactive Rebase: Clean up your Commit History

Mar 17, 2025 am 10:03 AM

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:

  1. Part 1: Crafting the Ideal Git Commit
  2. Part 2: Mastering Git Branching Strategies
  3. Part 3: Enhanced Collaboration with Pull Requests
  4. Part 4: Resolving Merge Conflicts
  5. Part 5: Rebase vs. Merge: A Detailed Comparison
  6. Part 6: Interactive Rebase (You're Here!)
  7. Part 7: Cherry-Picking Commits: A Targeted Approach
  8. 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:

  1. Identify the Commit Range: Determine how far back in your history you need to go.
  2. Initiate Interactive Rebase: Start the session to begin editing your history.
  3. Manipulate Commits: Reorder, delete, combine, or edit as needed.
  4. 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
Copy after login
Copy after login

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
Copy after login
Copy after login

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
Copy after login

Recovering from Mistakes

If you need to undo an interactive rebase, use:

git rebase --abort
Copy after login

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:

  1. Part 1: Crafting the Ideal Git Commit
  2. Part 2: Mastering Git Branching Strategies
  3. Part 3: Enhanced Collaboration with Pull Requests
  4. Part 4: Resolving Merge Conflicts
  5. Part 5: Rebase vs. Merge: A Detailed Comparison
  6. Part 6: Interactive Rebase (You're Here!)
  7. Part 7: Cherry-Picking Commits: A Targeted Approach
  8. 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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Vue 3 Vue 3 Apr 02, 2025 pm 06:32 PM

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.

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

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

Can you get valid CSS property values from the browser? Can you get valid CSS property values from the browser? Apr 02, 2025 pm 06:17 PM

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.

Stacked Cards with Sticky Positioning and a Dash of Sass Stacked Cards with Sticky Positioning and a Dash of Sass Apr 03, 2025 am 10:30 AM

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.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

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

Using Markdown and Localization in the WordPress Block Editor Using Markdown and Localization in the WordPress Block Editor Apr 02, 2025 am 04:27 AM

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

Comparing Browsers for Responsive Design Comparing Browsers for Responsive Design Apr 02, 2025 pm 06:25 PM

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

Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Why are the purple slashed areas in the Flex layout mistakenly considered 'overflow space'? Apr 05, 2025 pm 05:51 PM

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...

See all articles