Git MERGE vs REBASE: The Definitive Guide

  Переглядів 45,646

The Modern Coder

The Modern Coder

День тому

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

КОМЕНТАРІ: 85
@themoderncoder
@themoderncoder 8 місяців тому
MORE GIT VIDEOS: ukposts.info/slow/PLfU9XN7w4tFwKwh_xPSQ_X1-hROQEpHnM
@JustSkillGG
@JustSkillGG 8 місяців тому
BY FAR, the best channel on git
@themoderncoder
@themoderncoder 8 місяців тому
Thanks for saying that!
@johndibling8091
@johndibling8091 3 місяці тому
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 Місяць тому
Probably the best videos about git, out there. The animations are a huge help. Thank you so much!
@matk2283
@matk2283 Місяць тому
Amazing tutorial !! really appreciate posting this tutorial explaining "git merge" and "git rebase"
@BI-Rahul
@BI-Rahul 8 місяців тому
Best git videos on youtube!!
@ariyaneghbal
@ariyaneghbal 2 місяці тому
Your videos are the most clear I ever seen on this topic and still contains enough details
@themoderncoder
@themoderncoder 2 місяці тому
Appreciate you saying that
@growingisgood
@growingisgood 7 місяців тому
Fantastic. I've subscribed !!
@mgrm7031
@mgrm7031 8 місяців тому
great stuff
@HTownHero15
@HTownHero15 2 місяці тому
Great explanation. Well done!!
@erdemkosk
@erdemkosk 7 місяців тому
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 7 місяців тому
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!
@khangpiano549
@khangpiano549 7 місяців тому
Thank you!
@nanonkay5669
@nanonkay5669 7 місяців тому
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 7 місяців тому
That’s a good mental model for rebase - thanks for taking the time to write that up 👍
@DhavalAhir10
@DhavalAhir10 2 місяці тому
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.
@ad2894
@ad2894 Місяць тому
Excellent content, don't stop
@ogreeni
@ogreeni 4 місяці тому
Great video! You seriously deserve more recognition.
@themoderncoder
@themoderncoder 3 місяці тому
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!
@gustavorocha6592
@gustavorocha6592 4 місяці тому
Great video! Thanks
@niravpatel1239
@niravpatel1239 7 місяців тому
First time i have seen such an attractive videos regarding git, captivating !!
@levaryazan
@levaryazan 6 місяців тому
Just amazing!! Thank you so much for your work! Nothing even close to your explanations of the work of git does not exist.
@themoderncoder
@themoderncoder 6 місяців тому
Appreciate you saying that! I really try
@MarkW2711
@MarkW2711 28 днів тому
Excellent video, I've always been worried about using rebase, this helps massively
@themoderncoder
@themoderncoder 28 днів тому
That’s so nice to hear. Glad it helped
@ShivamSharma7
@ShivamSharma7 8 місяців тому
Amazing 😮
@themoderncoder
@themoderncoder 8 місяців тому
Thanks Shivam!
@cs80211
@cs80211 7 місяців тому
Thanks
@tambuidev
@tambuidev 3 місяці тому
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.
@nileshchavan8012
@nileshchavan8012 6 місяців тому
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 6 місяців тому
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: ukposts.info/have/v-deo/mYWWgpiniqGKmZ8.htmlsi=EWbgwrKwbulROvID&t=131
@theoceandragongaming
@theoceandragongaming 3 місяці тому
Excellent
@yashpandey9550
@yashpandey9550 22 дні тому
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 22 дні тому
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.
@angelmacias2885
@angelmacias2885 4 місяці тому
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 2 місяці тому
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.
@sanjusaju4049
@sanjusaju4049 5 місяців тому
You are the best
@GintsPolis
@GintsPolis Місяць тому
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 2 місяці тому
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 2 місяці тому
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
@lightsupmedia739
@lightsupmedia739 7 місяців тому
Can you do a video of How to revert already merged branch from a main branch.
@themoderncoder
@themoderncoder 6 місяців тому
Like undo the merge commit? Or undo the fast-forward?
@ravitejanekkalapu
@ravitejanekkalapu 5 місяців тому
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 5 місяців тому
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.
@user-ht6vy4gh7h
@user-ht6vy4gh7h 3 місяці тому
😮 Excellent video! Btw, what is the cool font used in the videos? JetBrainssMono Nerd Fonts isn’t it?
@themoderncoder
@themoderncoder 3 місяці тому
Iosevka’s nerd font! I use it in my ide too. Love it
@alinaeemchaudhry2262
@alinaeemchaudhry2262 Місяць тому
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 Місяць тому
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 22 дні тому
@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 22 дні тому
@@themoderncoder thank you
@Tony.Nguyen137
@Tony.Nguyen137 6 місяців тому
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 6 місяців тому
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
@hiteshupreti4250
@hiteshupreti4250 7 місяців тому
@TheModernCoder pls make video on git stash too
@themoderncoder
@themoderncoder 7 місяців тому
Gotchu: ukposts.info/have/v-deo/eoN9rndvpnmFmJs.html
@MrVipulLal
@MrVipulLal 4 дні тому
Great video. Thanks. What software fif you use to animate?
@themoderncoder
@themoderncoder 4 дні тому
I use Final Cut Pro and Apple Motion
@ivan07z583
@ivan07z583 2 місяці тому
The quality of this video is 10x the other ones that you can find around
@themoderncoder
@themoderncoder 2 місяці тому
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!
@shobhitsrivastava9112
@shobhitsrivastava9112 8 місяців тому
wow
@patrickwerz4608
@patrickwerz4608 8 місяців тому
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 8 місяців тому
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 2 місяці тому
Great explanation!
@oskarzarembadev
@oskarzarembadev Місяць тому
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 Місяць тому
I should do a video on pull requests - that’s a good suggestion
@timothyshiu2263
@timothyshiu2263 2 місяці тому
What about rebase one-author feature branch to multi-author main branch?
@themoderncoder
@themoderncoder 2 місяці тому
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.
@MobiusCoin
@MobiusCoin 7 місяців тому
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 7 місяців тому
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 6 місяців тому
@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.
@diegoramos27
@diegoramos27 2 місяці тому
Will rebase delete the original commit authors?
@themoderncoder
@themoderncoder 2 місяці тому
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
@whitegroyper
@whitegroyper 18 днів тому
gogo gaga
@ShootingUtah
@ShootingUtah 2 місяці тому
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 2 місяці тому
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 Місяць тому
⁠@@themoderncoderWould be interested to watch a video about git workflows. Your visualizations are great
@odebroqueville
@odebroqueville 2 місяці тому
ukposts.info/have/v-deo/sn-fq52voHlhsHU.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 2 місяці тому
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.
@stallman3738
@stallman3738 5 місяців тому
Very perfect but you speak too fast. slow it down a little bit
@themoderncoder
@themoderncoder 5 місяців тому
Thanks for the feedback, and for watching even if I was a little fast!
@jacakopl
@jacakopl 2 дні тому
Nice but too abstract 😢
@satishmeghavvarnam1269
@satishmeghavvarnam1269 2 місяці тому
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 2 місяці тому
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!
Resolve Git MERGE CONFLICTS: The Definitive Guide
8:02
The Modern Coder
Переглядів 17 тис.
Git MERGE vs REBASE
16:12
Academind
Переглядів 1 млн
Мама и дневник Зомби (часть 1)🧟 #shorts
00:47
Bro smelt it & passed out 😂 #comedy
00:10
MrTalalaa
Переглядів 5 млн
Git REBASE Explained: Unveiling Rebase's Hidden Hazard
4:01
The Modern Coder
Переглядів 7 тис.
GIT: Merge or Rebase? What's the difference?
10:47
Front-end Science із Сергієм Пузанковим
Переглядів 128 тис.
Create your own CUSTOMIZED Llama 3 model using Ollama
12:55
DevTechBytes
Переглядів 9 тис.
I Stopped Using GitHub (Kind Of)
17:19
Theo - t3․gg
Переглядів 56 тис.
My Forever Dev Workflow
16:02
typecraft
Переглядів 47 тис.
Git MERGE vs REBASE: Everything You Need to Know
4:34
ByteByteGo
Переглядів 300 тис.
Git Branching and Merging - Detailed Tutorial
54:28
SuperSimpleDev
Переглядів 171 тис.
How to be a git expert
46:26
Floating Little Leaves of Code
Переглядів 139 тис.
Git Stash Tutorial
6:59
The Modern Coder
Переглядів 17 тис.
What does larger scale software development look like?
24:15
Web Dev Cody
Переглядів 1,2 млн