Perform an Interactive Rebase

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Git is a powerful version control system that allows developers to manage their codebase efficiently. One of the most useful features of Git is interactive rebase, which allows developers to modify the commit history of a branch. In this challenge, you will learn how to perform an interactive rebase using Git.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/BranchManagementGroup(["`Branch Management`"]) git/BranchManagementGroup -.-> git/rebase("`Reapply Commits`") subgraph Lab Skills git/rebase -.-> lab-12735{{"`Perform an Interactive Rebase`"}} end

Perform an Interactive Rebase

You are working on a project with a team of developers, and you have made several commits to your branch. However, you realize that some of the commits are unnecessary or need to be combined. You want to clean up your commit history and make it more organized.

For this lab, let's use the repository from https://github.com/labex-labs/git-playground. Follow these steps:

  1. Navigate to the directory:
    cd git-playground
  2. Perform an interactive rebase of the last 2 commits:
    git rebase -i HEAD~2
    The interactive rebase file will open in your default text editor. You can modify the order of the commits and the action to perform for each one (pick, squash, drop, reword etc.).
  3. Change "pick" to "squash" in the commit message "Added file2.txt", press Esc and enter the :wq command, then press Enter to save your changes and exit the editor, change the commit message to "Added file1.txt and file2.txt" in the same way and exit.
  4. If there are merge conflicts or you need to make changes, you can continue the rebase when ready using git rebase --continue or abort it using git rebase --abort.

Running git log will give you a result that looks like this:

commit 7575ded485555c28ecb09487c68e90639bebbe9d (HEAD -> master)
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:25 2023 +0800

    Added file1.txt and file2.txt

commit b00b9374a7c549d1af111aa777fdcc868d8a2a01
Author: Hang <[email protected]>
Date:   Wed Apr 26 14:16:00 2023 +0800

    Initial commit

Summary

In this challenge, you learned how to perform an interactive rebase using Git. Interactive rebase is a powerful tool that allows you to modify the commit history of a branch, making it more organized and easier to manage. By completing this challenge, you have gained valuable experience in using Git and can apply this knowledge to your future projects.

Other Git Tutorials you may like