# Adding a commit to the front of my branch?

7 messages from 2016-01-11 to 2016-01-13. Participants: Robert Dailey, John Keeping, Stefan Haller.
Thread: https://gitlist.dev/t/41160

## Robert Dailey, 2016-01-11 13:36

Subject: Adding a commit to the front of my branch?
Message-ID: <CAHd499B8gRMVWnUBZ0aO2RtokYqTm6fO7+7zfbsJNTnKw4MVsQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAHd499B8gRMVWnUBZ0aO2RtokYqTm6fO7%2B7zfbsJNTnKw4MVsQ%40mail.gmail.com

```
Normally to add commits on my branch, I perform an interactive rebase:

$ git rebase -i origin/master

I mark the commit I want to put the new commit on top of as 'edit'.
However, if I want to add a commit to the front of my branch, I don't
really have a commit to mark as "edit". I tried to be tricky with
this, and did:

$ git rebase -i origin/master^

However this doesn't work if my merge-base is a merge commit. I get a
ton of superfluous commits in my TODO file.

Is there a built-in mechanism I can use, with relative ease, to
accomplish this goal? At the moment I have to run a series of a couple
of commands to do this, namely mark the oldest commit on my branch as
'edit', reset it, stash it, add new commit, pop stash, commit again,
etc.

Normally I'd add a new tip commit and reorder it to accomplish this,
however commits on my branch already alter this code and I don't want
to create unnecessary conflicts during rebase. The patch needs to be
based on merge-base.

Any advice is greatly appreciated. I'm using Git for Windows 2.7.0.

```

## Stefan Haller, 2016-01-11 14:11

Subject: Re: Adding a commit to the front of my branch?
Message-ID: <1mgv8hf.iib6fg1g9uszaM%lists@haller-berlin.de>
URL: https://gitlist.dev/e/1mgv8hf.iib6fg1g9uszaM%25lists%40haller-berlin.de
In-Reply-To: <CAHd499B8gRMVWnUBZ0aO2RtokYqTm6fO7+7zfbsJNTnKw4MVsQ@mail.gmail.com>

```
Robert Dailey <rcdailey.lists@gmail.com> wrote:

> Normally to add commits on my branch, I perform an interactive rebase:
> 
> $ git rebase -i origin/master
> 
> I mark the commit I want to put the new commit on top of as 'edit'.
> However, if I want to add a commit to the front of my branch, I don't
> really have a commit to mark as "edit".

Instead of marking commits as edit, I usually add "x false" at the place
where I want to add a commit. With this I find it easier to see where
the new commit goes, and it also works before the first commit.


-- 
Stefan Haller
Berlin, Germany
http://www.haller-berlin.de/

```

## John Keeping, 2016-01-11 14:16

Subject: Re: Adding a commit to the front of my branch?
Message-ID: <20160111141612.GK14056@serenity.lan>
URL: https://gitlist.dev/e/20160111141612.GK14056%40serenity.lan
In-Reply-To: <CAHd499B8gRMVWnUBZ0aO2RtokYqTm6fO7+7zfbsJNTnKw4MVsQ@mail.gmail.com>

```
On Mon, Jan 11, 2016 at 07:36:25AM -0600, Robert Dailey wrote:
> Normally to add commits on my branch, I perform an interactive rebase:
> 
> $ git rebase -i origin/master
> 
> I mark the commit I want to put the new commit on top of as 'edit'.
> However, if I want to add a commit to the front of my branch, I don't
> really have a commit to mark as "edit". I tried to be tricky with
> this, and did:
> 
> $ git rebase -i origin/master^
> 
> However this doesn't work if my merge-base is a merge commit. I get a
> ton of superfluous commits in my TODO file.
> 
> Is there a built-in mechanism I can use, with relative ease, to
> accomplish this goal? At the moment I have to run a series of a couple
> of commands to do this, namely mark the oldest commit on my branch as
> 'edit', reset it, stash it, add new commit, pop stash, commit again,
> etc.
> 
> Normally I'd add a new tip commit and reorder it to accomplish this,
> however commits on my branch already alter this code and I don't want
> to create unnecessary conflicts during rebase. The patch needs to be
> based on merge-base.

Have you considered creating a new (temporary) branch based on master,
adding the new commit there and inserting "pick $new_commit_sha1" at the
start of the instruction sheet when rebasing the original branch?

```

## Robert Dailey, 2016-01-13 16:18

Subject: Re: Adding a commit to the front of my branch?
Message-ID: <CAHd499Adtc3qMgvXywONpgK0Z5NzGMnZ-=5akbLo+i6m2oJpYA@mail.gmail.com>
URL: https://gitlist.dev/e/CAHd499Adtc3qMgvXywONpgK0Z5NzGMnZ-%3D5akbLo%2Bi6m2oJpYA%40mail.gmail.com
In-Reply-To: <1mgv8hf.iib6fg1g9uszaM%lists@haller-berlin.de>

```
On Mon, Jan 11, 2016 at 8:11 AM, Stefan Haller <lists@haller-berlin.de> wrote:
> Robert Dailey <rcdailey.lists@gmail.com> wrote:
>
>> Normally to add commits on my branch, I perform an interactive rebase:
>>
>> $ git rebase -i origin/master
>>
>> I mark the commit I want to put the new commit on top of as 'edit'.
>> However, if I want to add a commit to the front of my branch, I don't
>> really have a commit to mark as "edit".
>
> Instead of marking commits as edit, I usually add "x false" at the place
> where I want to add a commit. With this I find it easier to see where
> the new commit goes, and it also works before the first commit.

What do you mean "x false"? I'm not familiar with this. Can you explain?

```

## Robert Dailey, 2016-01-13 16:21

Subject: Re: Adding a commit to the front of my branch?
Message-ID: <CAHd499Ay7cCANQavNpYN7EQDmeHgvZvVS2FP=udEVxf8KoBKcg@mail.gmail.com>
URL: https://gitlist.dev/e/CAHd499Ay7cCANQavNpYN7EQDmeHgvZvVS2FP%3DudEVxf8KoBKcg%40mail.gmail.com
In-Reply-To: <20160111141612.GK14056@serenity.lan>

```
On Mon, Jan 11, 2016 at 8:16 AM, John Keeping <john@keeping.me.uk> wrote:
> On Mon, Jan 11, 2016 at 07:36:25AM -0600, Robert Dailey wrote:
>> Normally to add commits on my branch, I perform an interactive rebase:
>>
>> $ git rebase -i origin/master
>>
>> I mark the commit I want to put the new commit on top of as 'edit'.
>> However, if I want to add a commit to the front of my branch, I don't
>> really have a commit to mark as "edit". I tried to be tricky with
>> this, and did:
>>
>> $ git rebase -i origin/master^
>>
>> However this doesn't work if my merge-base is a merge commit. I get a
>> ton of superfluous commits in my TODO file.
>>
>> Is there a built-in mechanism I can use, with relative ease, to
>> accomplish this goal? At the moment I have to run a series of a couple
>> of commands to do this, namely mark the oldest commit on my branch as
>> 'edit', reset it, stash it, add new commit, pop stash, commit again,
>> etc.
>>
>> Normally I'd add a new tip commit and reorder it to accomplish this,
>> however commits on my branch already alter this code and I don't want
>> to create unnecessary conflicts during rebase. The patch needs to be
>> based on merge-base.
>
> Have you considered creating a new (temporary) branch based on master,
> adding the new commit there and inserting "pick $new_commit_sha1" at the
> start of the instruction sheet when rebasing the original branch?

I think this functions but it lacks the simplicity I'm looking for. I
mentioned I already had workarounds, what I'm really looking for is a
built-in mechanism to do this.

With your solution, I'd be doing this (best I can guess):

1. $ git checkout -b temp [merge-base]
2. hack hack hack
3. $ git commit -am 'hacking'
4. $ git checkout -
5. $ git rebase -i [merge-base]
6. Add the pick you mentioned here, at the top

Still a bit messy and lots of steps. More steps might be necessary
than I've shown, to establish the [merge-base] SHA1 to use.

```

## Stefan Haller, 2016-01-13 16:40

Subject: Re: Adding a commit to the front of my branch?
Message-ID: <1mgz4yx.94yt5g12q9hidM%lists@haller-berlin.de>
URL: https://gitlist.dev/e/1mgz4yx.94yt5g12q9hidM%25lists%40haller-berlin.de
In-Reply-To: <CAHd499Adtc3qMgvXywONpgK0Z5NzGMnZ-=5akbLo+i6m2oJpYA@mail.gmail.com>

```
Robert Dailey <rcdailey.lists@gmail.com> wrote:

> On Mon, Jan 11, 2016 at 8:11 AM, Stefan Haller <lists@haller-berlin.de> wrote:
> > Robert Dailey <rcdailey.lists@gmail.com> wrote:
> >
> >> Normally to add commits on my branch, I perform an interactive rebase:
> >>
> >> $ git rebase -i origin/master
> >>
> >> I mark the commit I want to put the new commit on top of as 'edit'.
> >> However, if I want to add a commit to the front of my branch, I don't
> >> really have a commit to mark as "edit".
> >
> > Instead of marking commits as edit, I usually add "x false" at the place
> > where I want to add a commit. With this I find it easier to see where
> > the new commit goes, and it also works before the first commit.
> 
> What do you mean "x false"? I'm not familiar with this. Can you explain?

"x" is a shorthand for "exec" (just like "p" is a shorthand for "pick",
for instance). "exec" will execute an arbitrary shell command, and stop
when that shell command fails with an exit code not equal to zero.
"false" is a shell command that does nothing except return a non-zero
exit code, so "x false" just stops and waits for you to "fix" something.
At that point you can make additional commits, and then "git rebase
--continue" to go on.


-- 
Stefan Haller
Berlin, Germany
http://www.haller-berlin.de/

```

## Robert Dailey, 2016-01-13 17:19

Subject: Re: Adding a commit to the front of my branch?
Message-ID: <CAHd499A67diDmBB3cO_SYXNaZnohcxNtaQpmDAomwToG+MMOvw@mail.gmail.com>
URL: https://gitlist.dev/e/CAHd499A67diDmBB3cO_SYXNaZnohcxNtaQpmDAomwToG%2BMMOvw%40mail.gmail.com
In-Reply-To: <1mgz4yx.94yt5g12q9hidM%lists@haller-berlin.de>

```
On Wed, Jan 13, 2016 at 10:40 AM, Stefan Haller <lists@haller-berlin.de> wrote:
> Robert Dailey <rcdailey.lists@gmail.com> wrote:
>
>> On Mon, Jan 11, 2016 at 8:11 AM, Stefan Haller <lists@haller-berlin.de> wrote:
>> > Robert Dailey <rcdailey.lists@gmail.com> wrote:
>> >
>> >> Normally to add commits on my branch, I perform an interactive rebase:
>> >>
>> >> $ git rebase -i origin/master
>> >>
>> >> I mark the commit I want to put the new commit on top of as 'edit'.
>> >> However, if I want to add a commit to the front of my branch, I don't
>> >> really have a commit to mark as "edit".
>> >
>> > Instead of marking commits as edit, I usually add "x false" at the place
>> > where I want to add a commit. With this I find it easier to see where
>> > the new commit goes, and it also works before the first commit.
>>
>> What do you mean "x false"? I'm not familiar with this. Can you explain?
>
> "x" is a shorthand for "exec" (just like "p" is a shorthand for "pick",
> for instance). "exec" will execute an arbitrary shell command, and stop
> when that shell command fails with an exit code not equal to zero.
> "false" is a shell command that does nothing except return a non-zero
> exit code, so "x false" just stops and waits for you to "fix" something.
> At that point you can make additional commits, and then "git rebase
> --continue" to go on.

Oh, this is pretty neat! So I assume it will stop BEFORE a patch is
applied, unlike edit? I will try this next time. However it would be
great to see a new 'insert' command for the TODO file, that will let
you insert a commit at that point (i.e. it stops before the marked
commit is applied).

Thanks for the help.

```
