Git MERGE vs REBASE: The Definitive Guide

Sdílet
Vložit
  • čas přidán 1. 06. 2024
  • The better way to learn Git with live coding and motion graphics:
    learngit.io/
    Newsletter readers get my videos early: newsletter.themoderncoder.com/
    ----
    This is the definitive tutorial for git rebase and git merge. We'll cover everything you'll need to know about how to use rebase and merge to streamline your version control workflow. Start by understanding the fundamentals of git merge: we'll cover 3-way merges, fast-forward merges and more. Then we'll then cover best practices for using git rebase in collaborative situations with other developers, and review common rebase pitfalls to avoid. We'll also explore some pro git rebase and merge workflow tips to help you master the most common git workflow.
    CHAPTERS
    0:00 Intro
    0:18 Merging: 3-way merge
    3:00 Merging: Fast-forward merge
    4:47 Rebase
    6:02 Merge & Rebase Workflow
    6:41 Rebase Pitfalls
    MY TECH
    www.themoderncoder.com/wfh-ny...
    MUSIC
    Music from Uppbeat (free for Creators!):
    uppbeat.io/t/danijel-zambo/st...
    License code: 0Y2YDUPOMXGYYQML
    #git #rebase #merge #github #learngit #gitmerge #gitrebase #development #softwareengineering #programming #coding #versioncontrol #developer #technology #tutorial #stepbystepguide #gitrepository #vcs #programmingtips #codersofinstagram #codingcommunity #learnprogramming #softwaredevelopment #codingtutorial #gitexplained

Komentáře • 101

  • @themoderncoder
    @themoderncoder  Před 9 měsíci +3

    MORE GIT VIDEOS: czcams.com/play/PLfU9XN7w4tFwKwh_xPSQ_X1-hROQEpHnM.html

  • @JustSkillGG
    @JustSkillGG Před 9 měsíci +17

    BY FAR, the best channel on git

  • @mariocalderon2148
    @mariocalderon2148 Před 20 dny +2

    Thanks a lot!
    At last after several years using Git, I (think) I finally grasped it.

  • @johndibling8091
    @johndibling8091 Před 4 měsíci +7

    Great video. I've been using Git forever, and this is probably the best explanation I've ever seen of merge, rebase and fast-forward.

  • @rpf23543
    @rpf23543 Před 2 měsíci +1

    Probably the best videos about git, out there. The animations are a huge help. Thank you so much!

  • @matk2283
    @matk2283 Před 2 měsíci

    Amazing tutorial !! really appreciate posting this tutorial explaining "git merge" and "git rebase"

  • @celikvolkan
    @celikvolkan Před 20 dny +1

    Best explanation I have ever seen on CZcams about merge vs rebase. Thank you for sharing.😺

  • @BI-Rahul
    @BI-Rahul Před 9 měsíci +3

    Best git videos on youtube!!

  • @growingisgood
    @growingisgood Před 8 měsíci

    Fantastic. I've subscribed !!

  • @HTownHero15
    @HTownHero15 Před 3 měsíci

    Great explanation. Well done!!

  • @ogreeni
    @ogreeni Před 5 měsíci +4

    Great video! You seriously deserve more recognition.

    • @themoderncoder
      @themoderncoder  Před 5 měsíci

      Thanks for that compliment. If you ever have a chance to share one of my videos to someone else, or on Reddit or something, I’d really appreciate it!

  • @ariyaneghbal
    @ariyaneghbal Před 3 měsíci

    Your videos are the most clear I ever seen on this topic and still contains enough details

  • @gustavorocha6592
    @gustavorocha6592 Před 5 měsíci

    Great video! Thanks

  • @knightcodes7681
    @knightcodes7681 Před 27 dny

    This video is fantastic. Thanks for sharing

  • @ad2894
    @ad2894 Před 2 měsíci

    Excellent content, don't stop

  • @nanonkay5669
    @nanonkay5669 Před 8 měsíci +8

    I'd like to think of both as merging, but done differently. Here's how:
    "Git merge" from a current branch into the target branch is when Git takes the latest changes of the current branch, then take the latest changes of the target branch and combine them. The new combination is made into a new commit and placed as the latest commit of the target branch. Git will still keep around the history of all the commits that had been made up until the merge of either branch.
    "Git Rebase" from current branch into target branch is when Git first takes all the commits from the current branch, lines them up in the order they were made and puts them aside for now. Then Git takes the oldest commit from current branch and "merges" it with the latest commit on the target branch by doing it like a regular merge: combing the changes, make a new commit that represents those combined changes then places that new commit as the latest commit on the target branch. Then Git moves on the second oldest commit on the current branch and repeats the process. All the way until the latest commit of the current branch is merged. But remember, the current branch is still set aside, what will Git do with those commits that have effectively become duplicates? Git will just delete those commits, hence why history has been rewritten.
    Hope this helps

    • @themoderncoder
      @themoderncoder  Před 8 měsíci +1

      That’s a good mental model for rebase - thanks for taking the time to write that up 👍

    • @DhavalAhir10
      @DhavalAhir10 Před 3 měsíci

      Actually rebase removed those commits, using a delete word is not appropriate here.
      Once a commit has been made you can't delete them.
      But you can remove it from the your branch or anywhere in the history.

  • @mgrm7031
    @mgrm7031 Před 9 měsíci +2

    great stuff

  • @niravpatel1239
    @niravpatel1239 Před 8 měsíci

    First time i have seen such an attractive videos regarding git, captivating !!

  • @budivoogt491
    @budivoogt491 Před 21 dnem

    Great video, thanks for the effort.

  • @erdemkosk
    @erdemkosk Před 8 měsíci

    This is the best git videos on youtube. This is amazing work. I am begging to you please move whole git with kind of videos

    • @themoderncoder
      @themoderncoder  Před 8 měsíci

      Appreciate it! I'm trying to make this my full time gig, if you ever come across a chance to share this video with someone, please do!

  • @levaryazan
    @levaryazan Před 7 měsíci

    Just amazing!! Thank you so much for your work! Nothing even close to your explanations of the work of git does not exist.

  • @MarkW2711
    @MarkW2711 Před 2 měsíci

    Excellent video, I've always been worried about using rebase, this helps massively

  • @khangpiano549
    @khangpiano549 Před 9 měsíci

    Thank you!

  • @ShivamSharma7
    @ShivamSharma7 Před 9 měsíci +4

    Amazing 😮

  • @ekchills6948
    @ekchills6948 Před 6 dny

    Best explanation

  • @robwalker4653
    @robwalker4653 Před 22 dny

    I quite like a semi linear approach. If I have a release/feature branch that multiple people are working on, rebase to keep in linear. Then when then when you want to merge to master, do a rebase and then merge with no fast forward. This way if you use gitversion, your main branch only has one commit per release but you have full history of commits for each release without a bird nest.

  • @tambuidev
    @tambuidev Před 4 měsíci +1

    Great video and explanation! I honestly can't think of a scenario where I'd want to rebase, versus just using merge. The risk outweighs the benefit, and honestly the overhead of a merge process on your local repository is moot. Just my two cents.

  • @adityakumarseth8934
    @adityakumarseth8934 Před měsícem

    Really Good Video

  • @nileshchavan8012
    @nileshchavan8012 Před 7 měsíci

    How to handle the case when while doing rebase and we encountered conflicts ? Best video i found for my confusion on rebase and merge ❤

    • @themoderncoder
      @themoderncoder  Před 7 měsíci +1

      I'm literally working on a video about merge conflicts right now! In the meantime the process is pretty similar to cherry pick conflicts if you want to check out my video on that: czcams.com/video/aUeNbpSkY8k/video.htmlsi=EWbgwrKwbulROvID&t=131

  • @theoceandragongaming
    @theoceandragongaming Před 4 měsíci

    Excellent

  • @cs80211
    @cs80211 Před 8 měsíci

    Thanks

  • @yashpandey9550
    @yashpandey9550 Před měsícem +1

    Hey man, amazing video with great illustrative animations out there.
    There's just one thing which I found confusing. So in most of the scenarios that I have worked in, the git workflow looks something like this:
    1) Create a feature_branch from main: `git checkout -b feature_branch`.
    2) Makes changes to your feature branch: `git add -A && git commit -m "Your commit message"`.
    3) During the development process some changes were pushed and merged to the REMOTE main branch. So you need to sync your LOCAL main with the REMOTE main:
    `git checkout main && git pull origin main`
    Once that's done...
    4) You checkout your feature_branch and merge the updated local main with your feature_branch: `git checkout feature_branch && git merge main`
    5) You push your local feature_branch to remote and create a pull request: `git push origin feature_branch`
    In this workflow we can probably replace the merge thing in step 4 with a rebase thing, if this feature branch is being worked on by a single contributor.
    What confuses me in the video is that at 5:40 : when we are on the feature branch we run `git rebase main` and then we checkout to main and run `git merge feature_branch`.
    Maybe that's done to illustrate fast forwarding merge here but in a production workflow isn't it discouraged to merge directly onto the main branch?
    Let me know if I missed a point or something.

    • @themoderncoder
      @themoderncoder  Před měsícem +4

      Your absolutely right: in many production workflows you'd let the PR process do the work of integrating of your changes into main instead of doing it yourself like I demonstrated in the video. I didn't want to make the video too complicated by trying to introduce PRs into the mix, but I feel like creating a video specifically covering PRs and code review could be a good idea.

    • @ShriramRavichandran
      @ShriramRavichandran Před 7 dny

      @@themoderncoder can you pls create a video on this? it will be really helpful

  • @sanjusaju4049
    @sanjusaju4049 Před 6 měsíci

    You are the best

  • @angelmacias2885
    @angelmacias2885 Před 5 měsíci

    This was great. My current job, devs keep telling me to rebase and I ask myself "why not merge". This explains it lol. Thx!!

    • @michaelm2704
      @michaelm2704 Před 4 měsíci

      there is even better merge --squash which gives same linear history, but not commit automatically, not ruin your feature branch and safe to play with.

  • @GintsPolis
    @GintsPolis Před 2 měsíci

    In flow, when we have master branch and release branch for patches, when developers have merged master changes into they feature/fix branch, it is hard to rebase it to patch release branch.

  • @alessandrodestefano3823
    @alessandrodestefano3823 Před 3 měsíci

    I absolutely agree with what you said towards the end and that's what I like to do: rebase on personal feature_branch, merge the feature_branch on main.
    But how can we do this when doing PRs? I mean...you can do rebase and merge, but if there are conflicts, who should deal with them? How to organise with the team?

    • @themoderncoder
      @themoderncoder  Před 3 měsíci +1

      I’d say (personally) since you’re the one merging into existing work, then you’re on the hook for making sure it’s compatible (I.e. resolving conflicts). That said, I don’t believe you should be entirely responsible for figuring out how to resolve if the original authors are more authoritative on the files that are in conflict. Still, even if others help you figure out what to do, you’d still have to author it IMO

  • @MobiusCoin
    @MobiusCoin Před 8 měsíci +14

    Okay, now that I actually know what Rebase is, number one, the name makes a lot of sense. You're "rebasing" where your branch starts. But two, this doesn't seem to be an accurate history of work anymore. We're pretending that I started my work after whoever got their changes into main, somehow that feels wrong.

    • @themoderncoder
      @themoderncoder  Před 8 měsíci +6

      Sounds like you understand rebase pretty well - the good and the bad ;) But yeah in all seriousness, pretending you started your work somewhere else is a caveat you need to account for especially when working with others.

    • @tothestars3958
      @tothestars3958 Před 7 měsíci +9

      @MobiusCoin Sure it's not an accurate history in the sense that it doesn't show who did what work when. But the question is, does that matter? Chances are your company has that kind of history available in other places, like Jira or a similar software.
      The real point of source control is to document the history of when the code changed, not when the code was actually typed into a file. If you think about it like that, no real "history" is lost in a rebase vs a merge. If I merge my branch, main will have my code from that point on. If I rebase and fast-forward merge, the same thing will happen.

    • @joshuagauss8179
      @joshuagauss8179 Před 15 dny +1

      I thought that commit timestamps do not change on rebase.

  • @tarsala1995
    @tarsala1995 Před 6 dny

    I always squash and never allow teammates to work on my branch. This way you maintain main branch clean with only feature commits and avoid conflicts with teammate locally. The only use case I had was when I was introducing a big feature >100 files and split my PR into 6, then I was rebasing branches in order of building block of the feature, where last branch was rebased into main feature branch after reviews of my peers.

    • @themoderncoder
      @themoderncoder  Před 5 dny

      This a good anecdote for folks - in the real world sharing branches is not very common and workflows like you describe are more the norm.

  • @joaomendoncayt
    @joaomendoncayt Před 19 dny

    Amazing video.. Was struggling a bit with conflicts and the log and decided to dedicate an hour for a couple videos to once and for all kill all the missing pieces in my mental model even though I use git for years...
    One thing that wasn't 100% clear is at the end of the video the "recommended workflow" tells you to rebase your branch and then merge it when ready, but that doesn't apply if the branch is on remote and has more people working on it correct? That would mean that remote branches should never get the latest changes from their parent branch unless absolutely necessary, and in that case you'd need to merge from the parent branch.

    • @themoderncoder
      @themoderncoder  Před 17 dny

      Right, that would necessarily apply if multiple people are working on the same remote branch. Like you said though, in those cases you can merge main INTO the remote branch - you’ll just end up with a merge commit which isn’t too bad.

  • @ravitejanekkalapu
    @ravitejanekkalapu Před 6 měsíci +1

    There is a main branch and a feature branch which is based on main branch commit MBC1. Now my other developer added MBC2 and MBC3 commits in main branch. I commited FBC1 and FBC2 into my feature branch. When i want to incorporate those two commits of master into my feature branch, i will do a rebase with master.
    After rebasing, i end up with same two branches master and my feature branch where master history is not changed but my feature branch will contain MBC2, MBC3 commits added and then after these two commits, my individual commits will be added in the history of my branch wvich are FBC1 and FBC2 . Is my understanding correct ?

    • @themoderncoder
      @themoderncoder  Před 6 měsíci

      Yeah, you got it. The only part I’d phrase slightly differently is your second paragraph. Instead of your feature branch “containing” MBC2 and MBC3, it’s more like your feature branch is now “based on” MBC2 and MBC3 since those commits are still on main. But ultimately your understanding is functionally correct.
      If actually did want to incorporate MBC2 and MBC3 into your feature branch AFTER FBC1 and FBC2, you could actually merge main INTO your feature branch. You’d end up with a merge commit on your feature branch, and you could keep committing on top of that merge commit on your feature branch.

  • @MrVipulLal
    @MrVipulLal Před měsícem

    Great video. Thanks. What software fif you use to animate?

  • @user-ht6vy4gh7h
    @user-ht6vy4gh7h Před 4 měsíci

    😮 Excellent video! Btw, what is the cool font used in the videos? JetBrainssMono Nerd Fonts isn’t it?

    • @themoderncoder
      @themoderncoder  Před 4 měsíci

      Iosevka’s nerd font! I use it in my ide too. Love it

  • @Tony.Nguyen137
    @Tony.Nguyen137 Před 7 měsíci

    Hi I have a question. I have a parentfolder which contains 4 Javascript project subfolders. I have git on the parent folder so I could upload all the subfolders to one folder on github . I also have git on every subfolder to manage each project. In total I have 5 gits, is it legit to use it like that or is there a better way? I just want to group the projects in one folder on github.

    • @themoderncoder
      @themoderncoder  Před 7 měsíci

      That’s technically possible (I’ve never personally done it, but it’s in the official docs: see Git Submodules git-scm.com/book/en/v2/Git-Tools-Submodules ). As for if it’s legit or not, I would say that you should have a pretty good reason to do it like that. GitHub repos are free, so if the repos are not related to one another you should probably break them out into their our repositories. If you let me know more about your use-case I could maybe point you in the right direction

  • @alinaeemchaudhry2262
    @alinaeemchaudhry2262 Před 2 měsíci

    Great explanation.
    I have a question: you mentioned using rebase is not recommended when there are multiple contributors working on the same branch because the remote and local version will diverge. But when same branch is shared by multiple people at the same time, doesn't it diverge anyway because of different commits by the contributors? If yes, which specific aspect you are talking about?

    • @needmoreprivacy4947
      @needmoreprivacy4947 Před 2 měsíci +1

      Those are different kind of "diverge". When the coworker pushes their changes to the branch, you will pull those changes down and merge if necessary, but the commit hashes will not have changed so everything will be ok because the shared history matches up to that point. If you had rebased then now the commit hashes don't match (C' and D' instead of C and D) so the branch history won't match and everything will be a mess. I am learning it too but hope this helps...

    • @themoderncoder
      @themoderncoder  Před měsícem +2

      @needmoreprivacy4947 got it right. When multiple collaborators are simply ADDING commits to an existing branch, Git is able to use standard merge protocols to combine those new commits together. But if one person rebases, it's as if they made a copy, and continued working on top of an entirely new branch. Now, when the time comes for those two collaborators to combine their changes, Git assumes you're trying to merge two distinct and unrelated branches (which is a much more difficult operation than merging the same branch with new changes). I hope that makes sense. It's a little confusing to try and explain verbally.

    • @alinaeemchaudhry2262
      @alinaeemchaudhry2262 Před měsícem

      @@themoderncoder thank you

  • @lightsupmedia739
    @lightsupmedia739 Před 8 měsíci

    Can you do a video of How to revert already merged branch from a main branch.

    • @themoderncoder
      @themoderncoder  Před 7 měsíci

      Like undo the merge commit? Or undo the fast-forward?

  • @siddharthnayak4448
    @siddharthnayak4448 Před 15 dny

    I had faced an issue while uptaking merge branch additional commits into feature branch using git merge in gitlab UI where merge conflict was arising. When I resolved then conflict on gitlab UI itself and merge request was granted, both the main and feature branches got merged into each other and main branch had the commits of feature branch😠. Any reason for this weird behaviour?

  • @ivan07z583
    @ivan07z583 Před 3 měsíci +1

    The quality of this video is 10x the other ones that you can find around

    • @themoderncoder
      @themoderncoder  Před 3 měsíci

      Thank you! I’m trying to build up my reputation for making super high quality tech videos. If you have a chance to share this video with someone, or on Reddit etc. I’d really appreciate it!

  • @patrickwerz4608
    @patrickwerz4608 Před 9 měsíci

    I my case rebasing onto main creates way more conflicts than merge main into feature. Why is that happening? Is there a way to prevent this?

    • @themoderncoder
      @themoderncoder  Před 9 měsíci +5

      It probably has to do with the direction in which rebase/merge integrates the changes. Merge looks at the tips of both branches (the most recent commits from both main and your branch) whereas you can think of rebase as rewinding and replaying. So rebase would instead look to merge the earliest commit on your branch into main first (instead of the latest commit). You can try squashing all your branch changes first into a single commit, then the merge and rebase would likely result in the same set of merge conflicts.

    • @teenytinyteetee
      @teenytinyteetee Před 4 měsíci

      Great explanation!

  • @timothyshiu2263
    @timothyshiu2263 Před 3 měsíci

    What about rebase one-author feature branch to multi-author main branch?

    • @themoderncoder
      @themoderncoder  Před 3 měsíci

      Yeah that’s a pretty safe workflow since the only commits you’re touching are the ones from your feature branch. Just keep in mind that sometimes development teams or open source projects don’t like using rebase as a method of moving work onto shared main branches. But that’s a separate issue.

  • @EricMuranoAU
    @EricMuranoAU Před 10 dny

    If you push your feature branch to remote, but no one touches your branch, is rebase still safe?

    • @themoderncoder
      @themoderncoder  Před 10 dny +1

      Yeah, rebase should be pretty safe in that scenario.

  • @hiteshupreti4250
    @hiteshupreti4250 Před 8 měsíci

    @TheModernCoder pls make video on git stash too

    • @themoderncoder
      @themoderncoder  Před 8 měsíci

      Gotchu:
      czcams.com/video/BSLzA8oCT7g/video.html

  • @oskarzarembadev
    @oskarzarembadev Před 2 měsíci

    Nice video but it would be even nicer if you could demonstrate it with the github and merging options of pull requests so not locally but remotely too ;)

    • @themoderncoder
      @themoderncoder  Před 2 měsíci

      I should do a video on pull requests - that’s a good suggestion

  • @diegoramos27
    @diegoramos27 Před 3 měsíci

    Will rebase delete the original commit authors?

    • @themoderncoder
      @themoderncoder  Před 3 měsíci

      I'm not 100% sure on that one. Maybe someone here can weight in? Theoretically it's bad practice to rebase someone else's commits in the first place, but assuming you had a good reason to do so anyways I'm guessing you'll be updating the committer field, but not the original author. Again, not 100% on that answer, but here is a relevant post about author vs committer stackoverflow.com/questions/18750808/difference-between-author-and-committer-in-git

  • @shobhitsrivastava9112
    @shobhitsrivastava9112 Před 9 měsíci

    wow

  • @whitegroyper
    @whitegroyper Před měsícem

    gogo gaga

  • @ShootingUtah
    @ShootingUtah Před 3 měsíci

    Why would multiple people ever work on the same branch? Isn't the entire point of branching to allow each individual a branch and thus deal with version control? If 2 people start working on a feature, that feature should be split into 2, or 3, or 1000 branches for each individual. Then each person should be merging or rebasing on that feature branch, resolving conflicts before again merging the feature history onto main. You'd have to be crazy to have multiple people working on a single branch! IMO

    • @themoderncoder
      @themoderncoder  Před 3 měsíci +1

      Some workflows maintain multiple shared long running branches: having a “main” branch and a “develop” branch is an example (git-scm.com/book/en/v2/Git-Branching-Branching-Workflows ).

    • @ultimathule9841
      @ultimathule9841 Před 2 měsíci

      ⁠@@themoderncoderWould be interested to watch a video about git workflows. Your visualizations are great

  • @stallman3738
    @stallman3738 Před 6 měsíci +1

    Very perfect but you speak too fast. slow it down a little bit

    • @themoderncoder
      @themoderncoder  Před 6 měsíci

      Thanks for the feedback, and for watching even if I was a little fast!

  • @odebroqueville
    @odebroqueville Před 3 měsíci

    czcams.com/video/zOnwgxiC0OA/video.htmlsi=NcGWivMhokq4XNCM&t=326 What do you mean by "replaying" each commit? Are they actually getting merged with the latest commit from the main branch?

    • @themoderncoder
      @themoderncoder  Před 3 měsíci

      When those commits are "replayed" all that means is that their parent commit pointer is updated. Everything else about the commit remains the same (metadata, file diffs, etc), but the pointer to the parent gets updated to the latest commit on main. No merging occurs here, they're still on that separate feature branch.

  • @jacakopl
    @jacakopl Před měsícem

    Nice but too abstract 😢

  • @satishmeghavvarnam1269
    @satishmeghavvarnam1269 Před 3 měsíci +1

    You have great content. Following you is hard though. Why? You aren't pacing yourself when you speak. Slow down ! You are speaking for others to absorb what you are sharing.

    • @themoderncoder
      @themoderncoder  Před 3 měsíci

      Thanks, and I'll keep that in mind the next time I'm filming a video. You're not the first to mention this so I'll be more aware!