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

Project and code management together.

Combine commits

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