Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. How to use Git
  6. How to manage history in Git
  7. Rebase a commit
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Rebase a commit

Interactive rebasing in Git offers a way to revise and fine-tune your commit history with precision. With interactive rebase, you can modify, rearrange, squash, split, or delete commits to create a more coherent and cleaner commit history according to your specific requirements.

In this part of our tutorial, we will delve into the process of interactive rebase in Git. By mastering this technique, you'll have the flexibility to create a more logical and focused development timeline, making collaboration and code maintenance more streamlined.

Go to the git-tutorial/tutorial5 directory you previously downloaded.

When you examine the history of this repository, it will look like the following:

Current history

We will combine the two commits, “append description of the commit command” and “append description of the pull command,” into a single commit.

To do that, we will use the git rebase -i command.

$ git rebase -i HEAD~~

When your default text editor opens, you will be in rebase-interactive mode. It will show commits from HEAD to HEAD~~, as shown below:

pick 9a54fd4 append description of the commit command
  pick 0d4a808 append description of the pull command

  # Rebase 326fc9f..0d4a808 onto d286baa
  #
  # Commands:
  #  p, pick = use commit
  #  r, reword = use commit, but edit the commit message
  #  e, edit = use commit, but stop for amending
  #  s, squash = use commit, but meld into previous commit
  #  f, fixup = like "squash," but discard this commit log message
  #  x, exec = run command (the rest of the line) using shell
  #
  # If you remove a line here THAT COMMIT WILL BE LOST.
  # However, if you remove everything, the rebase will be aborted.
  #

On the second line, change the word pick to squash, then save and quit.

The editor will then prompt you to edit the commit message of this newly formed commit. Edit the commit message, then save and quit.

The previous two commits are now squashed into a single new commit.

Verify the result by checking the history log using the git log command.

Organize commit

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life