# Splitting a commit with rebase -i and keeping a commit message

4 messages from 2013-04-17 to 2013-04-17. Participants: Tim Chase, David Aguilar, Johannes Sixt.
Thread: https://gitlist.dev/t/33524

## Tim Chase, 2013-04-17 01:38

Subject: Splitting a commit with rebase -i and keeping a commit message
Message-ID: <20130416203825.3701d98b@bigbox.christie.dr>
URL: https://gitlist.dev/e/20130416203825.3701d98b%40bigbox.christie.dr

```
I asked this on IRC and played with some of their ideas, but struck
out with anything satisfying.  I walked through [1] with the
following setup:

  git init foo
  cd foo
  touch a.txt b.txt
  git add a.txt b.txt
  git commit -m "Initial checkin"
  echo "Modify A" >> a.txt
  git commit -am "Modified A"
  echo "Modify B" >> b.txt
  git commit -am "Modified B"
  echo "Modify A2" >> a.txt
  echo "Modify B2" >> b.txt
  git commit -am "Modified B"
  git commit -am "Long-bodied commit comment about b.txt changes"
  # whoops, just wanted B
  git rebase -i HEAD^^
  # change the "Added b.txt..." commit to "edit"
  git reset HEAD^  # pull the changes out of the pending commit
  git add a.txt
  git commit -m "Tweaked a.txt"
  git add b.txt
  git commit ${MAGIC_HERE}
  git rebase --continue

I haven't been able to figure out a good way to keep the "long-bodied
commit comment" for the final commit where the ${MAGIC_HERE} is.  Is
there a right/easy way to go about pulling in the commit-message from
the commit the rebase is transplanting?

-tkc

[1]
http://git-scm.com/book/en/Git-Tools-Rewriting-History#Splitting-a-Commit

```

## David Aguilar, 2013-04-17 02:29

Subject: Re: Splitting a commit with rebase -i and keeping a commit message
Message-ID: <CAJDDKr64KpF-DP5Axj+vhxRQ1rrig=pm25T3Angp8GaKK_CHMA@mail.gmail.com>
URL: https://gitlist.dev/e/CAJDDKr64KpF-DP5Axj%2BvhxRQ1rrig%3Dpm25T3Angp8GaKK_CHMA%40mail.gmail.com
In-Reply-To: <20130416203825.3701d98b@bigbox.christie.dr>

```
Taking a wild guess here...

On Tue, Apr 16, 2013 at 6:38 PM, Tim Chase <git@tim.thechases.com> wrote:
> I asked this on IRC and played with some of their ideas, but struck
> out with anything satisfying.  I walked through [1] with the
> following setup:
>
>   git init foo
>   cd foo
>   touch a.txt b.txt
>   git add a.txt b.txt
>   git commit -m "Initial checkin"
>   echo "Modify A" >> a.txt
>   git commit -am "Modified A"
>   echo "Modify B" >> b.txt
>   git commit -am "Modified B"
>   echo "Modify A2" >> a.txt
>   echo "Modify B2" >> b.txt
>   git commit -am "Modified B"
>   git commit -am "Long-bodied commit comment about b.txt changes"
>   # whoops, just wanted B

Save the commit's ID here so that we can reuse its message later:

    orig_commit=$(git rev-parse HEAD)

>   git rebase -i HEAD^^
>   # change the "Added b.txt..." commit to "edit"
>   git reset HEAD^  # pull the changes out of the pending commit
>   git add a.txt
>   git commit -m "Tweaked a.txt"
>   git add b.txt
>   git commit ${MAGIC_HERE}

...reuse the commit message by passing the "-c" option to "git commit":

    git commit --reset-author -c $orig_commit

This will give you a chance to further edit the message in your editor.

>   git rebase --continue
>
> I haven't been able to figure out a good way to keep the "long-bodied
> commit comment" for the final commit where the ${MAGIC_HERE} is.  Is
> there a right/easy way to go about pulling in the commit-message from
> the commit the rebase is transplanting?

This is pretty much what the commands above do.
They save the commit ID so that we can reuse the message later.

HTH,
--
David

```

## Tim Chase, 2013-04-17 03:04

Subject: Re: Splitting a commit with rebase -i and keeping a commit message
Message-ID: <20130416220407.105b354e@bigbox.christie.dr>
URL: https://gitlist.dev/e/20130416220407.105b354e%40bigbox.christie.dr
In-Reply-To: <CAJDDKr64KpF-DP5Axj+vhxRQ1rrig=pm25T3Angp8GaKK_CHMA@mail.gmail.com>

```
On 2013-04-16 19:29, David Aguilar wrote:
> On Tue, Apr 16, 2013 at 6:38 PM, Tim Chase <git@tim.thechases.com>
> wrote:
> >   git commit -am "Long-bodied commit comment about b.txt changes"
> >   # whoops, just wanted B
> 
> Save the commit's ID here so that we can reuse its message later:
> 
>     orig_commit=$(git rev-parse HEAD)
> 
> >   git rebase -i HEAD^^
> >   # change the "Added b.txt..." commit to "edit"
> >   git reset HEAD^  # pull the changes out of the pending commit
> >   git add a.txt
> >   git commit -m "Tweaked a.txt"
> >   git add b.txt
> >   git commit ${MAGIC_HERE}
> 
> ...reuse the commit message by passing the "-c" option to "git
> commit":
> 
>     git commit --reset-author -c $orig_commit

Wild guess or not, using -c worked great.  With the appropriate
section of the docs now in hand, I discovered that it could even be
simplified to just

  git commit -c ORIG_HEAD [...]

as rebase stashes that information away for me already as "ORIG_HEAD".

Thanks!

-tkc

```

## Johannes Sixt, 2013-04-17 05:55

Subject: Re: Splitting a commit with rebase -i and keeping a commit message
Message-ID: <516E395D.3050307@viscovery.net>
URL: https://gitlist.dev/e/516E395D.3050307%40viscovery.net
In-Reply-To: <20130416203825.3701d98b@bigbox.christie.dr>

```
Am 4/17/2013 3:38, schrieb Tim Chase:
> I asked this on IRC and played with some of their ideas, but struck
> out with anything satisfying.  I walked through [1] with the
> following setup:
> 
>   git init foo
>   cd foo
>   touch a.txt b.txt
>   git add a.txt b.txt
>   git commit -m "Initial checkin"
>   echo "Modify A" >> a.txt
>   git commit -am "Modified A"
>   echo "Modify B" >> b.txt
>   git commit -am "Modified B"
>   echo "Modify A2" >> a.txt
>   echo "Modify B2" >> b.txt
>   git commit -am "Modified B"
>   git commit -am "Long-bodied commit comment about b.txt changes"
>   # whoops, just wanted B
>   git rebase -i HEAD^^
>   # change the "Added b.txt..." commit to "edit"

    # and duplicate the instruction line
    git checkout HEAD^ b.txt # undo b.txt
    git commit --amend -m "Tweaked a.txt"
    git rebase --continue
    # in real world cases, you are likely to see conflicts here
    # when the commit is applied a second time,
    # but not in this toy example
    git rebase --continue

-- Hannes

```
