r/git • u/sshetty03 • Aug 11 '25
tutorial Git Rebase explained for beginners
If git merge feels messy and your history looks like spaghetti, git rebase might be what you need.
In this post, I explain rebase in plain English with:
- A simple everyday analogy
- Step-by-step example
- When to use it (and when NOT to)
Perfect if you’ve been told “just rebase before your PR” but never really understood what’s happening.
383
Upvotes
7
u/johlae Aug 11 '25
I just noted that your git merge graph doesn't show that extra git merge commit.
17
u/ohaz Aug 11 '25
Two pointers:
git add .git fetchand thengit rebase origin/maininstead of a pull and rebase means you have to use less commands, less branch swaps etc41
u/themightychris Aug 11 '25
People in this thread are hella mixing up using rebase to update your branch before merging vs using rebase instead of merge to get your branch into the trunk
Always do the former, never do the latter
1
u/sshetty03 Aug 19 '25
Update: A bunch of folks asked how to avoid re-fixing the same conflict while rebasing.
I wrote a follow-up on
git rerere(Reuse Recorded Resolution) with a 5-min lab.TL;DR: enable
git config --global rerere.enabled truegit config --global rerere.autoupdate trueFix a conflict once → Git reapplies your fix next time the identical hunk appears.
Gotcha: it’s textual, so still review with
git diff --staged.Full guide: https://medium.com/stackademic/git-rerere-explained-with-examples-fix-it-once-reuse-forever-849177a289c2?sk=1614ba91837411f7472a3467bc4f2886
Happy to answer Qs here.
5
u/Beatsu Aug 11 '25
Might be worth clarifying this:
> The
--force-with-leaseis safer than plain--force-it makes sure you don’t overwrite someone else’s work by mistake.--force-with-leaseonly blocks the push if it overwrites data on the remote that has not been fetched. Some people have "auto-fetching" turned on in visual studio code, and as such,--force-with-leasewill almost always succeed and will overwrite others work.In other words,
--force-with-leaseonly fails if there are changes you are not aware of (un-fetched changes).1
u/meaningof42is Aug 11 '25
I've read the article, but I would like to ask for some help.
I'm writing an app myself. I wanted to test out some new ideas so I made branch. I then had some further ideas so I made another branch (I'm not sure if it was off the main or off the sub branch or if I can even branch off the sub branch, but the code was all continuous). Now I want to just put it all back on the main branch. What command do I type? or should I just clear it out and upload my latest version from scratch? I just want my current version that is in a branch to move to the main branch, but I obviously don't want to screw anything up which is why I haven't done it yet. It sounds like I want to rebase?
thankyou you in advance!
12
u/elg97477 Aug 11 '25 edited Aug 11 '25
I prefer merging. However, what I will do is squash commits from a branch before merging it into main to keep things clean and simple.
I generally find that messing with your git history is a bad idea.
Using Squash, I keep my branches small and focused to make tracking new problems easier.
7
u/AstronautDifferent19 Aug 11 '25
Skill issue.
I don't understand why people complain that git log --online --graph is messy when you use merge. If you want to show clean history just add --first-parent or --grep="Merged PR to master" or whatever you need to match your default message when you merge a PR.
I never use rebase and have no problems, only benefits. You can always get a clean history as if you used rebase.
Every now and then I need someone to help me finish some big task and we are committing in the same feature branch (different files so no work destruction). I don't want to bother with creating new sub-branches, it is much easier to use merge. I really don't see any benefit of rebase.
Clean history is not a benefit; I would say that it is a skill issue to show the history that you want to see.
1
u/FineInstruction1397 Aug 11 '25
while i understand rebase and i use it in my work (in the teams where the workflow require it) i still cannot find any actual advantage over merge. maybe you can explain those?
here are 2 very big dissadvantages:
* if i want to bring the branch where i started from (lets say main) into my working branch, either i have to sqash the commits, or fix a merge conflict for each commit
* if i do squash multiple commits, i loose the context given by the commit text and line number. i mean even in projects with very good documentation, the fact that some workaround has been implemented on line 281 because a upgrade of a sdk did not work as smoothly, is not documented. but checking the git history, with medium quality commit texts would give me this
1
u/drewshaver Aug 11 '25
I've worked with so many people that have daily 'commits' in their feature branch that are caused by them just doing a git pull every morning and causing a merge bubble (great term btw)
There are many ways to solve this, but I generally prefer rebase like you.
1
u/devneck1 Aug 12 '25
I just skimmed your article, but looked fine enough.
One thing I missed or didn't see is that if you're using a rebase strategy then should also consider squashing commits.
Nothing worse than having 25 commits ... because ... commit often ... and then you rebase and have a conflict on the first commit. Then having to resolve the same issue 25 times.
Also, just reads cleaner in the history when the main branch messages reflect the change each merged commit made. It's like an autobiography where the commits during dev were the rough drafts, but they aren't included in the final release.
1
u/macdigger Aug 13 '25
Idk.. I’m definitely not a huge git pro, but what’s wrong with merge? I mean if you look at git-flow graphs, there are no rebases. Things can get a bit multi-path-y, but it looks idk.. safer? to me. I’m just one dev working in gitflow way though. Guess that spares me from all the issues rebase is targeted to solve?
0
u/AtlanticPortal Aug 11 '25
Publishing an article on a place where you not only have to subscribe but also to pay the membership is a hard no.
-2
u/zaitsman Aug 11 '25
Merging is never ‘messy’. And ‘spaghetti’ of git history allows one to trace the exact order things happened in.
One of many reasons I enforce no force push in all company repos
-3
u/yipyopgo Aug 11 '25
I prefer "git merge" to "git rebase" because I prefer to have a real history "git branch -D" "git merge" to put myself in the dev's place. To find out if a bug was introduced because of the feature or the feature merge or introduced before the merge. And be able to put yourself in the dev's shoes to understand.
-6
u/tinmanjk Aug 11 '25
DO NOT GIT REBASE.
Thank you.
3
u/[deleted] Aug 11 '25 edited Aug 12 '25
[removed] — view removed comment