Tutorial: Organize your commits through git rebasing and Jetbrains IDEs

Rafael De Leon
7 min readAug 17, 2016

--

Who is this for?

This tutorial is for users of IntelliJ or other subset Jetbrains IDEs with Git Integration, such as Webstorm, and to organize their commit history before merging into master.

How can rebasing help me?

  • Using `git rebase`, you can organize your commits to highlight certain sets of changes.
  • Organizing your commits will help expedite git bisect process.
  • In the Files tab of a Pull Request, reviewers can narrow in on a single commit:
This commit has a focus on writing test, click “Next”
This commit has a focus on updating code that will affect the test.

Before rebasing

You will want to enable the following through command line:

git config — global rerere.enabled true

in order to let git remember what the last conflict resolution will be if the same conflict comes up again. The link provided with the command line goes into detail on what it will help you with.

Next, you might want to push your branch to your repo in case you have to start completely over by deleting your local version to checkout the version you previously pushed to the repo.

Git to it

Getting to the menu

will bring up the Rebase Branches Dialog

Most of the time you will rebase onto master.

this is an example of the history I want on top of master

on top of master. Make sure you have the Interactive checkbox checked.

Click the Rebase button, and it will bring up the Interactive Rebase Dialog.

Cancel rebasing

You can abort rebasing during squash, reword, conflict resolutions, and edit. Doing so will revert back to before you started rebasing.

Interactive mode options

  • skip : marking a commit with skip will erase the commit from the active branch history. Use this if you know the commit does not provide anything useful to the history, such as a commit with only white space fixing.
  • reword : Allows you to change selected commit comment.

Good if you made a typo or add more information about the commit.

  • pick : This option will leave the commit as is. The commit you mark with pick must be placed before commits marked with fixup or squash if you are planning to use those options.
  • fixup : Lets you combine multiple commits into a picked commit. It discards the commit and comment history for the commit marked as a fixup. Can be used with other fixuped or squashing commits as long as the commit before it has been picked.

Example : I want to combine the highlighted commit (5425d73) into the commit above it (96411f7).

This is good for correcting a commit.

You can re-order commits through this interface, by drag and drop, in case you want to preserve the message of the commit you want combine into.

Demo drag and drop

Example : The previous order listed (96411f7) above (5425d73). Now I want re-order them so (5425d73) is above (96411f7) and combine them.

Compared to the example: (5425d73) is above (96411f7)
  • squash : Works like fixup with the added bonus of allowing you to combine commit comments into one comment along with combining commits. This will bring up the same dialog that reword uses before resuming rebasing.
  • edit : Has the reword feature where you can change your commit message, and lets you change files while in the commit. Any change you make must be added to the history.

If you have several file changes and you want to and them to the history all at once , you can use

Good for adding in the staging area

See https://git-scm.com/docs/git-add for more info.
When you are ready to finish or move on to the next commit you can click on “continue”

This appears when you are in edit mode

You can also access continue from the menu.

Either way will bring up the “Additional Rebase Input” dialog, in case you want to add more comments about your changes.

There are more scenarios you can use edit for:

Taking changes from other commits

While in edit mode you can look at the “Version Control” View and see changes from future commits which you want to include in the commit you’re currently editing.

Copying changes

You can click a commit and see what files were changed

The left side are changes from the other commit. The right side are the changes for the commit you are editing. Clicking on

will copy changes from the left to the right. When you are done copying changes over, add them to the history.

Copying files that don’t exist yet

To add files that don’t exist in the HEAD yet:

HEAD is where I am in history. Now I want MYNEWGREATFILE.md in the history I am editing.

Since the file doesn’t exist in the history I am editing, see changes from future commits which you want to include in the commit you’re currently editing.

This will let me see the file’s contents through the IDE edit window.

Copy the file you just opened to a directory of your choosing.

then continue the rebase.

Replace and splitting up commits

I got this from http://stackoverflow.com/questions/6217156/break-a-previous-commit-into-multiple-commits and applied it through the IDE.

Through rebasing, select edit on the commit you want to split. Go to the “Version Control” view and select the commit before the one you are editing and Reset Current Branch to Here

Make sure you know what type of reset to us.
Select the commit you were editing.

For this example, I am going to split each file into its own commit by applying Copying changes and Copying files that don’t exist yet but I won’t continue for this case. Instead, I am going to commit the changes.

Notice that I only checked the on-attach.spec.js file. The other file, I will commit separately.

Clicking “Commit” will bring up this warning (which you will ignore.)

“may lead to the commit loss” is what we want to happen.

Click “Commit” again. Repeat for the other file.

When you are finish, continue.

The final result is

When splitting up a commit, keep in mind that the changes you don’t copy from the commit you are splitting will be gone when you continue. Make sure you copy everything you want to keep in the history.

If you lost changes that you wanted to keep, the IDE has it’s own history of changes you can reference through the context (right click) dropdown menu.

--

--