What is Git Rebase Interactive?

Git rebase interactive (git rebase -i) lets you rewrite commit history in a flexible way. With it, you can:

  • Squash multiple commits into one
  • Reword commit messages
  • Reorder commits
  • Drop commits you don’t need

How to Use Git Rebase Interactive

  1. Start Interactive RebaseOpen your terminal and navigate to your repository. To start an interactive rebase,
    • git rebase -i #commitId
  2. Choose Actions for Each CommitAn editor will open, showing a list of commits. Each commit line starts with pick by default. You can change pick to:
    • reword – Edit the commit message
    • edit – Make changes to the commit
    • squash – Combine the commit with the previous one
    • fixup – Like squash but discards the commit message
    • drop – Remove the commit
  3. Reword a CommitTo change a commit message, replace pick with reword. Save and close the editor. Another editor window will open for you to edit the commit message.
  4. Squash CommitsTo combine commits, replace pick with squash or fixup on the commits you want to merge into the previous commit. Save and close the editor.
  5. Complete the RebaseAfter making your changes, save and close the editor. Git will apply the changes. If conflicts arise, resolve them as you normally would and continue the rebase.
    • git rebase --continue

Example Workflow

Here’s a simple workflow example to squash the last three commits into one:

  • Start rebase
git rebase -i #ebffrxc
  • Change pick to squash for the second and third commits.
  • Save and close the editor.
  • Edit the combined commit message if prompted.

Some other similiar commands:

1. git cherry-pick

git cherry-pick allows you to apply the changes introduced by existing commits onto your current branch. It’s useful for selectively applying commits without merging entire branches.

git cherry-pick <commit-hash>

2. git reset

git reset can be used to undo changes in your commit history. It has three modes:

  • --soft: Moves the HEAD pointer to a previous commit, but leaves the working directory and index unchanged.
  • --mixed (default): Moves the HEAD pointer and resets the index, but leaves the working directory unchanged.
  • --hard: Moves the HEAD pointer, resets the index, and changes the working directory to match the specified commit.
git reset --soft <commit-hash>
git reset --mixed <commit-hash>
git reset --hard <commit-hash>

3. git revert

git revert creates a new commit that undoes the changes made by a previous commit. It’s a safer alternative to git reset because it doesn’t change commit history.

git revert <commit-hash>

4. git filter-branch

git filter-branch is a powerful but complex tool for rewriting Git history. It’s useful for tasks like removing sensitive data from history or splitting a repository.

git filter-branch --tree-filter 'rm -f passwords.txt' HEAD

5. git rebase --onto

This variant of git rebase allows you to rebase selected commits onto another base commit. It’s useful for more complex rebase operations.

git rebase --onto <newbase> <upstream> <branch>

6. git amend

git commit --amend lets you modify the most recent commit. You can change the commit message or add changes to the commit.

git commit --amend

What is WooCommerce?

WooCommerce is a popular open-source eCommerce plugin for WordPress. It allows users to create and manage online stores, providing features for selling products, managing inventory, processing payments, and more.

To install WooCommerce on your WordPress site:

  1. Log in to WordPress Admin:
    • Go to your WordPress dashboard.
  2. Navigate to Plugins:
    • Click on “Plugins” in the sidebar.
    • Select “Add New.”
  3. Search for WooCommerce:
    • In the search bar, type “WooCommerce.”
    • Find the WooCommerce plugin and click “Install Now.”
  4. Activate WooCommerce:
    • After installation, click “Activate.”
  5. Setup Wizard:
    • Follow the WooCommerce setup wizard to configure your store settings.

Now you can access various functionalities via wp-admin.

Thank you for reading…
By @leaveitblank (Mayank Tripathi