Kohei Nozaki's blog 

Using rebase


Posted on Sunday Mar 09, 2014 at 08:17PM in Technology


Make multi commits to one commit with rebase

Commit

kyle-no-MacBook:hello kyle$ echo commit1 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'commit1'
[b4 2447554] commit1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ echo commit2 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'commit2'
[b4 50e8dee] commit2
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git log -2
commit 50e8deed471c20abc9b86eb1cb7d6b6af4c9fcfd
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:23 2014 +0900

    commit2

commit 2447554c8495ff5f407b4dffb84278840d7fdba6
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:15 2014 +0900

    commit1

Rebase

  • Exec this (number 2 means how many commits to be one commit):
git rebase -i HEAD~2
  • Editor launches with this text:
pick 2447554 commit1
pick 50e8dee commit2

# Rebase 9ccae64..50e8dee onto 9ccae64
#
# 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
  • Edit to:
pick 2447554 commit1
squash 50e8dee commit2
  • Another editor will launch so that we can edit commit message:
# This is a combination of 2 commits.
# The first commit's message is:

commit1

# This is the 2nd commit message:

commit2

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached at 2447554
# You are currently editing a commit while rebasing branch 'b4' on '9ccae64'.
#
# Changes to be committed:
#   (use "git reset HEAD^1 <file>..." to unstage)
#
#       modified:   README.md
#
  • Gone like this:
kyle-no-MacBook:hello kyle$ git rebase -i HEAD~2
[detached HEAD ada1147] commit1
 1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/b4.
kyle-no-MacBook:hello kyle$ 
  • Log:
kyle-no-MacBook:hello kyle$ git log -2
commit ada114798c5727e8461afc36a10ad0b36e5e214a
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:15 2014 +0900

    commit1

    commit2

commit 9ccae643e8ceee10c5f7bcb4103857bc1a38cdd4
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 19:09:56 2014 +0900

    cancel
kyle-no-MacBook:hello kyle$ 
  • README.md
kyle-no-MacBook:hello kyle$ cat README.md 
b4-local1
CANCEL
commit1
commit2
kyle-no-MacBook:hello kyle$ 



No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed