git — Interactive Rebase

Filiz Senyuzluler
3 min readAug 8, 2018

--

You can rewrite commits that already happened so they look like they happened in a different way by making interactive rebase.

Interactive rebase offers you the possibility to:

  • edit your commit messages
  • change the order of the commits
  • remove commits entirely
  • squash commits
git rebase -i HEAD~3  // will let you to modify last three commits

When you run this command, the last three commits you are working with will be opened in a text editor. Notice that the commits are in the opposite order of git log. The most recent commit is on bottom, whereas it is on top on git log.

pick 7aff6e1 title is changed
pick 2fa134e button is disabled
pick f301e7a domain redirection is done

# Rebase 41650a1..f301e7a onto 41650a1
#
# 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's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

Choose the command according to how you want to change the commits. After you made your changes, save the file. Your commit history will be changed.

Edit your commit messages

Change the word ‘pick’ to the word ‘reword’ for each of the commit message you want to modify, on the opened text file.

pick 7aff6e1 title is changed
reword 2fa134e button is disabled
reword f301e7a domain redirection is done

Save and close the commit list file. For each commit, type the new commit message in the opened commit file, save the file, and close it. Then force-push (git push -f) the changes.

Change the order of the commits

pick 7aff6e1 title is changed
pick 2fa134e button is disabled
pick f301e7a domain redirection is done

Given the list above, you can easily change the order of the commits, such as:

pick f301e7a domain redirection is done
pick 2fa134e button is disabled
pick 7aff6e1 title is changed

When you save and close the commit list file, the order of the commits will be changed.

Remove commits entirely

pick 7aff6e1 title is changed
pick 2fa134e button is disabled
pick f301e7a domain redirection is done

Given the list above, you can delete the line of the commit (pick f301e7a domain redirection is done) that you want to remove and save the file:

pick 7aff6e1 title is changed
pick 2fa134e button is disabled

Squash commits

When you squash commits you combine them in a single commit, keeping its commit log message. If you choose to fix, it will do the same process but only will discard the commit log message.

pick 7aff6e1 title is changed
squash 2fa134e button is disabled
pick f301e7a domain redirection is done

When you save the text file, two commits will be combined 7aff6e1 and 2fa134e . Squashing works in upwards direction.

--

--