Skip to content

Instantly share code, notes, and snippets.

@todgru
Last active December 18, 2023 02:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todgru/7cff3962e76e1360cc2c to your computer and use it in GitHub Desktop.
Save todgru/7cff3962e76e1360cc2c to your computer and use it in GitHub Desktop.
git interactive rebase

#Git Interactive Rebase

Scenario: We have a topic branch we've been working on. We want to combine the last few commits into one single commit. Our intention is to merge this topic branch, with one clean commit, into our main dev branch.

Solution: Git Rebase Interactive

Checkout your topic branch and look at your commit history:

$ git checkout topic-branch
$ git log --oneline
cd88db5 declared platform as property
be65972 most recent submodule
5e242e0 removed unused routes
a47c3af checking for ent users
e4cd085 check for ent user
7a73058 Merge branch 'development' into v2.0

We want to rebase the last 5 commits into one commit. We know from our work that commit 7a73058 is where we started our topic branch. We are going to rebase from 7a73058. This commit is JUST BEFORE we want to start rebasing.

Run git rebase -i 7a73058. My editor is vim. Vim opens to:

$ git rebase -i 7a73058
     1 pick e4cd085 check for ent user
     2 pick a47c3af checking for ent users
     3 pick 5e242e0 removed unused routes
     4 pick be65972 most recent submodule
     5 pick cd88db5 declared platform as property
     6
     7 # Rebase 7a73058..cd88db5 onto 7a73058
     8 #
     9 # Commands:
    10 #  p, pick = use commit
    11 #  r, reword = use commit, but edit the commit message
    12 #  e, edit = use commit, but stop for amending
    13 #  s, squash = use commit, but meld into previous commit
    14 #  f, fixup = like "squash", but discard this commit's log message
    15 #  x, exec = run command (the rest of the line) using shell
    16 #
    17 # These lines can be re-ordered; they are executed from top to bottom.
    18 #
    19 # If you remove a line here THAT COMMIT WILL BE LOST.
    20 # However, if you remove everything, the rebase will be aborted.
    21 #
    22 # Note that empty commits are commented out

The order of commits is reversed...oldest on the top of the list. Keep the first one, squash the rest. Like so:

     1 pick e4cd085 check for ent user
     2 squash a47c3af checking for ent users
     3 squash 5e242e0 removed unused routes
     4 squash be65972 most recent submodule
     5 squash cd88db5 declared platform as property
:wq

Write and quit. The commits will rebase. A new vim session will re-open for you to write your commit message.

After saving your new commit message, you should see something similar to:

[detached HEAD 3910213] Rebase topic-branch. This is a combination of 5 commits. check for ent user
 Author: todgru <todgru@gmail.com>
 4 files changed, 26 insertions(+), 210 deletions(-)
 delete mode 100644 app/file1.php
 delete mode 100644 app/file2.php
Successfully rebased and updated refs/heads/topic-branch.

Lets look at the git log:

$ git log --oneline
3910213 Rebase topic-branch. This is a combination of 5 commits. check for ent user
7a73058 Merge branch 'development' into v2.0

Nice and clean! We are done rebasing. If you are using Git Hub, you'll need to force push you newly rebased topic branch.

$ git push --force origin topic-branch

Thanks to @GreenMindPDX https://github.com/greenmindpdx for the inspiration to document how we do things 'round here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment