home | list info | list archive | date index | thread index

Re: [OCLUG-Tech] git question: how can i drop the n'th last commit?

  • Subject: Re: [OCLUG-Tech] git question: how can i drop the n'th last commit?
  • From: Alex Pilon <alp [ at ] alexpilon [ dot ] ca>
  • Date: Tue, 10 May 2016 07:11:01 -0400
On Tue, May 10, 2016 at 06:14:21AM -0400, Robert P. J. Day wrote:
> i can remove the first line and save to get:
>
>     ... X <--- B' <--- C' <--- D' <--- E' (HEAD)
>
> is there a way to do that without having to fire up an interactive
> rebase session?

Provided you never have rebase/merge/cherry-pick conflicts, yes.

I could also be cheap and suggest using ed as your $VISUAL/$EDITOR, and
providing an ed script!

>   oh, wait, can't i just rebase B onto X? effectively, i want to
> reproduce the work from B to E as if it originated at X; isn't that
> just a regular rebase? thoughts?

Yes it is. A tad roundabout since you're being *indirect* about it,
instead of explicit about the commits to drop.

I presume you mean to use --onto (git-rebase(1)). Right?

Not sure if you could achieve the same with --root or --fork-point
(git-rebase(1)). Wouldn't be a long shot from a quick scan of the man.

Were you thinking of something like this?

    $ cd /tmp
    $ mkdir foo
    $ cd foo
    $ git init
    Initialized empty Git repository in /tmp/foo/.git/
    $ for c in x a b c d e; do echo $c > f; git add f; git commit -m $c; done
    [master (root-commit) a204b8b] x
    1 file changed, 1 insertion(+)
    create mode 100644 f
    [master 1f1383e] a
    1 file changed, 1 insertion(+), 1 deletion(-)
    [master e70bbdd] b
    1 file changed, 1 insertion(+), 1 deletion(-)
    [master 38787d9] c
    1 file changed, 1 insertion(+), 1 deletion(-)
    [master 7df5863] d
    1 file changed, 1 insertion(+), 1 deletion(-)
    [master bc3c996] e
    1 file changed, 1 insertion(+), 1 deletion(-)
    $ git rebase --onto :/x :/a :/e
    First, rewinding head to replay your work on top of it...
    Applying: b
    Using index info to reconstruct a base tree...
    M       f
    Falling back to patching base and 3-way merge...
    Auto-merging f
    CONFLICT (content): Merge conflict in f
    error: Failed to merge in the changes.
    Patch failed at 0001 b
    The copy of the patch that failed is found in: .git/rebase-apply/patch
    When you have resolved this problem, run "git rebase --continue".
    If you prefer to skip this patch, run "git rebase --skip" instead.
    To check out the original branch and stop rebasing, run "git rebase --abort".

    $ echo $?
    128
    $ git diff
    diff --cc f
    index 587be6b,6178079..0000000
    --- a/f
    +++ b/f
    @@@ -1,1 -1,1 +1,5 @@@
    ++<<<<<<< a204b8bb4f2f12157648532a598b330e92d0f706
    +x
    ++=======
    + b
    ++>>>>>>> b
    $ git mergetool
    Merging:
    f

    Normal merge conflict for 'f':
    {local}: modified file
    {remote}: modified file
    $ git rebase --continue
    Applying: b
    Applying: c
    Applying: d
    Applying: e
    $ git log --oneline --graph --decorate --all
    * 75ab185 (HEAD) e
    * 8226637 d
    * 8b40930 c
    * deb4067 b
    | * bc3c996 (master) e
    | * 7df5863 d
    | * 38787d9 c
    | * e70bbdd b
    | * 1f1383e a
    |/
    * a204b8b x

Who the heck said you needed tig? I never got it, especially not the
colours.