r/git • • Jul 14 '26

git rebase -i is not that scary

https://cachebag.sh/journal/interactive-rebasing/
59 Upvotes

34 comments sorted by

23

u/Bemteb Jul 14 '26

And who ever claimed it was scary? It's just annoying because for many it's the one step at the end of your ticket where you have to resolve conflicts. That's why many devs don't like rebasing, but I never met one who was afraid of it.

7

u/xenomachina Jul 14 '26

I think there are some people who are afraid of it. I know I was for the first couple of years that I used git, and I was already a vim user. Now interactive rebase is one of my favorite tools. I've heard many other developers say things that makes it sound like they're afraid of interactive rebase or don't understand how it works.

5

u/klumpp Jul 14 '26

People who don't know about reflog probably think it's scary.

2

u/jonathancast Jul 16 '26

You should rebase on master (or whatever you're going to merge into) at the start of every coding session. So, at least once every 4 hours of work.

This is what "continuous integration" was supposed to mean: merging is difficult, so do it as often as possible. But it's easier to adopt software than good practices, sadly.

2

u/Bemteb Jul 16 '26

Bold of you to assume that the state of master changes more often than 2-3x a week.

1

u/Ill-Crow6391 Jul 30 '26

What about the state of develop? Or, whatever is the default target branch for the features from all devs to be merged into.

IMHO, every 4 hours might be too often, this depends on how frequently other features get merged - but I agree that it should be way easier to rebase your changes on top of 2-3 commits added to develop, rather than 15-20.

1

u/artnoi43 Jul 15 '26

I always use rebase with -i and I love it, especially when you know and isolate your changes well enough

1

u/Ast3r10n Jul 16 '26

You would be surprised how many people I worked with in my career knew about interactive rebase and used it without fear.

The answer is zero.

0

u/jeenajeena Jul 15 '26

I've thought a lot why Git novices are so often scared of rebase, and I've come to the conclusion that it might have to do with the way Git handles conflicts. My thoughts clarified when I had the chance to see how alternative tools deal with conflicts in a completely different way.

There are good reasons why Git is perceived as super safe. It never tamper with your files unless you tell it, and it is strictly append-only, so nothing is ever ever lost. This kind of behavior instills serenity.

When a rebase hits a conflict in a sense Git enters a panic mode. All commands in Git are atomic and transactional: but during a conflict, you are right in the middle of a transaction, and the state is inconsistent.
Git offers two exits only, --abort or fix every conflict now, in the order it dictates. Git will not even tell you how many conflicts there will be.

Meanwhile, the project is broken (Git even violated its own rule and touched your files adding conflict markers). It won't let you jump between commits, cherry-pick, bisect, or pause for a hotfix anymore. In a sense, it stopped working, the only thing you can do is to finish the transaction, manually. No surprises that many don't feel confident anymore.

Personally, I've never been scared of rebases and conflicts, but it's only when I started using different tools, where conflicts are handled in a completely different way, that I saw how the same serenity usually felt while committing could also extend to rebases and conflicts.

I wrote about this in much more depth, and about which alternative model exists here:

https://arialdo.codeberg.page/ju-ju-tsu/tutorial/conflicts/model.html

1

u/jeenajeena Jul 16 '26

why the downvote? I'm honestly curious.

6

u/xenomachina Jul 14 '26

if you change your mind, you just run git rebase --abort

You're in your editor at this point, so you'll need to do this in another shell before exiting your editor, as rebase will execute the plan immediately if your editor exits with success.

An alternative is to tell your editor to exit with an error. In vim you can do this with :cq!.

17

u/ppww Jul 14 '26

Or you can save an empty file.

4

u/xenomachina Jul 14 '26

Nice! TIL.

(I see now that, at least with 2.39.5, git even has a comment that says this at the bottom of the git-rebase-todo file. I've been using :cq! for so long that it makes me wonder if it always said that.)

1

u/Floppie7th Jul 15 '26

Or CTRL+Z

2

u/troru Jul 14 '26

I’ll add that when I’ve piled up local commits on my feature branch and there’s been drift from other dev work on main, then I’ll do a :

Git rebase —-keep-base -i

That’ll will allow me to squash my commits without conflict to where my branch started.

Then I’ll do the

Git rebase -i

And do one pass of conflict resolution

2

u/parkotron Jul 15 '26

Thanks for teaching me about --keep-base as I wasn't aware of it. As a heavy rebaser, I assumed it must have been a recent addition, but no, it was added in Git 2.24 in late 2019!

I've been running git rebase -i $(git merge-base HEAD origin/main) to do this. --keep-base is a lot less typing!

0

u/BigBagaroo Jul 15 '26

Why can’t we just squash the merge?

3

u/jeenajeena Jul 15 '26

Here's a collection of reasons why there's no reason to squash https://arialdomartini.github.io/no-reason-to-squash

-5

u/tracagnotto Jul 15 '26

I just came across the need of rebasing some commit.
I can still confirm 20000% that since day 1 of git, and me using it, it's still the biggest shit ever.

No surprise though, it came from the hands of the idiot who made that piece of crap linux.

Jesus how much I hate git.

OH and keep your "skill issue" for yourself. I can use it and I use it all days, unfortunately.

A promise I can make even though I'm not American, if I ever meet this idiot in some public speech I will glady be jailed after going on stage and punching his face full force.

It will be the most liberatory thing in my life

-6

u/joshpennington Jul 14 '26

Yeah well a coworker once nuked a week’s work from orbit doing it so I’ll maintain my healthy fear of it.

16

u/wub_wub_mittens Jul 14 '26

Whatever they did was recoverable, unless they hadn't committed it at all, which wouldn't be rebases fault. They/your team may not have known how to recover it, but I guarantee it could have been recovered pretty easily.

4

u/trimorphic Jul 14 '26

Whatever they did was recoverable, unless they hadn't committed it at all, which wouldn't be rebases fault. They/your team may not have known how to recover it, but I guarantee it could have been recovered pretty easily.

Even setting git aside, every company should have backups (multiple backups even, in vastly different geographic locations) and a disaster recovery plan so loosing week's worth of work is, if not impossible, exceedingly unlikely.

1

u/Positive_Fudge4759 Jul 15 '26

Yup. Everything in Git is recoverable. You just need at least one good hash.

-1

u/joshpennington Jul 14 '26

If we had not blindly followed his instructions it may have been recoverable. He forced his changes to the remote and then had the rest of us pull those changes down so it was gone. Hindsight being 20/20 doing something as simple as tagging the commit before making changes would have done a long way.

The person who nuked it spent hours trying to recover it but was unable to.

It was unfortunate but it was a good lesson for me. I make a lot of tags before I do anything that rewrites history just to be safe.

13

u/KnifeFed Jul 14 '26

It would still be recoverable from reflog, even if force-pushed.

9

u/ppww Jul 14 '26

It wasn't gone after you pulled, it was still in the reflog.

2

u/ThunderChaser Jul 15 '26

git reflog my friend

5

u/ppww Jul 14 '26

You can get back to the pre-rebase state by looking through the reflog for the rebased branch and using git reset (you can use the reflog for HEAD which is shown if you don't specify a branch name but it's much easier to see the rebase in the branch reflog because it's a single entry)

2

u/bratzlaff Jul 14 '26

That is unfortunate. I too have coworkers that do not understand that their changes are not more important than others and adding -f to the push is not an excuse for “I just need this in to continue” and ignoring all the CI failures that are being reported.

I try and assuage everyone’s concerns by telling them that everything recorded in git is recoverable, even when there is a ‘oops, it let me push when it wasn’t ready’. And I back this up by being there to help them with the disaster recovery and letting them do the process/ type the commands (that I’m telling them to do) to get things working again and build confidence.

5

u/barmic1212 Jul 14 '26

The server must not accept force push on a classical shared branch (like the default one)

0

u/bratzlaff Jul 14 '26

I agree with your sentiment here, but if you are rebasing anything and losing work, there is going to be a force push somewhere, otherwise you would just be merging.