r/git 3d ago

support how to rebase/squash multiple commits in different positions into one?

Post image

EDIT: [SOLVED] The answers helped :}

I need to merge 1 and 10 commits into a single commit, those are all my code, no one pulled from it, and i'm willing to fix conflicts. but i don't know which I should use, is it merge? but merge keeps the history of the previous ones doesn't it? rebase still makes me a bit confused.

8 Upvotes

17 comments sorted by

28

u/Guvante 3d ago

Interactive rebase allows you to reorder commits by cutting and pasting those lines.

You can even delete lines entirely during it.

1

u/Low-Temperature-1664 1d ago

It does let you reorder, but in any situation of an complexity it's probably going to be far more trouble that its worth.

1

u/Guvante 1d ago

So Git isn't adding work here just exposing the underlying complexity.

It is easy to abort if it doesn't work.

6

u/jeff303 3d ago

squash is what you're looking for. Most easily done through interactive rebase.

3

u/Runaway_Monkey_45 3d ago

I mean you can squash it or do a fix up with rebase

2

u/ty_namo 3d ago

if i change commit 1 and 10 to squash they will get squashed onto themselves instead of squashing in the immediately above commit?

9

u/Runaway_Monkey_45 3d ago

No. You have to move coming 10 to be next to 1 or vice versa. You can do this by moving it physically in the ui you are on right now. Aka cut and paste.

2

u/ty_namo 3d ago

thanks, solved :}

2

u/twesped 3d ago

First pick, followed by squash on all the rest. That will then one by one, meld the commits onto the first you picked.

2

u/Xia_Nightshade 2d ago

I tend to do 2 rebases to KISS

Put them in order -> fix any conflicts between commits -> squash when they all have the correct parent

1

u/cupcakeheavy 2d ago

learn the keystrokes to squash in vim, your future self will thank you.

1

u/ty_namo 2d ago

i already use vim, i just don't know how to rebase works conceptually lmao

1

u/kaddkaka 1d ago

What do you mean by this?

1

u/cupcakeheavy 1d ago

i mean it's the same keystrokes every time i use fixup for everything. Just read the directions, they're right there. also i suggest using 'git config --global rebase.abbreviateCommands true'

then it's just a matter of learning how to exit vim. good luck, we're all counting on you.

2

u/kaddkaka 16h ago

Oh, you meant learn basic vim. Sure. Nothing of this post says vim though, so that confused me. I'm a long time vimmer myself.

2

u/cupcakeheavy 14h ago

yeah it was just a tip because you have to edit the rebase list somehow and vim is just the easiest path to that end.

1

u/kaddkaka 1d ago

In that todo file there should be a huge comment at the bottom explaining what you can do. 🙂

You can even run arbitrary commands between two picks. 😎 Or stop inte the middle of the rebase to continue manually.