Git MERGE vs REBASE: The Definitive Guide

  Рет қаралды 47,444

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

Пікірлер: 87
@themoderncoder
@themoderncoder 8 ай бұрын
MORE GIT VIDEOS: kzbin.info/aero/PLfU9XN7w4tFwKwh_xPSQ_X1-hROQEpHnM
@johndibling8091
@johndibling8091 4 ай бұрын
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 2 ай бұрын
Amazing tutorial !! really appreciate posting this tutorial explaining "git merge" and "git rebase"
@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 !!
@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.
@JustSkillGG
@JustSkillGG 8 ай бұрын
BY FAR, the best channel on git
@themoderncoder
@themoderncoder 8 ай бұрын
Thanks for saying that!
@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!
@BI-Rahul
@BI-Rahul 8 ай бұрын
Best git videos on youtube!!
@ogreeni
@ogreeni 4 ай бұрын
Great video! You seriously deserve more recognition.
@themoderncoder
@themoderncoder 4 ай бұрын
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!
@HTownHero15
@HTownHero15 2 ай бұрын
Great explanation. Well done!!
@MarkW2711
@MarkW2711 Ай бұрын
Excellent video, I've always been worried about using rebase, this helps massively
@themoderncoder
@themoderncoder Ай бұрын
That’s so nice to hear. Glad it helped
@khangpiano549
@khangpiano549 8 ай бұрын
Thank you!
@mgrm7031
@mgrm7031 8 ай бұрын
great stuff
@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: kzbin.info/www/bejne/l4bIf5WmiNCMbs0si=EWbgwrKwbulROvID&t=131
@yashpandey9550
@yashpandey9550 Ай бұрын
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 Ай бұрын
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.
@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
@ad2894
@ad2894 Ай бұрын
Excellent content, don't stop
@knightcodes7681
@knightcodes7681 4 күн бұрын
This video is fantastic. Thanks for sharing
@ShivamSharma7
@ShivamSharma7 8 ай бұрын
Amazing 😮
@themoderncoder
@themoderncoder 8 ай бұрын
Thanks Shivam!
@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.
@gustavorocha6592
@gustavorocha6592 4 ай бұрын
Great video! Thanks
@angelmacias2885
@angelmacias2885 5 ай бұрын
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 3 ай бұрын
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.
@cs80211
@cs80211 7 ай бұрын
Thanks
@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?
@theoceandragongaming
@theoceandragongaming 3 ай бұрын
Excellent
@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
@MrVipulLal
@MrVipulLal 12 күн бұрын
Great video. Thanks. What software fif you use to animate?
@themoderncoder
@themoderncoder 12 күн бұрын
I use Final Cut Pro and Apple Motion
@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.
@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
@adityakumarseth8934
@adityakumarseth8934 7 күн бұрын
Really Good Video
@sanjusaju4049
@sanjusaju4049 6 ай бұрын
You are the best
@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 Ай бұрын
@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 Ай бұрын
@@themoderncoder thank you
@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.
@ivan07z583
@ivan07z583 3 ай бұрын
The quality of this video is 10x the other ones that you can find around
@themoderncoder
@themoderncoder 3 ай бұрын
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 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 3 ай бұрын
Great explanation!
@hiteshupreti4250
@hiteshupreti4250 8 ай бұрын
@TheModernCoder pls make video on git stash too
@themoderncoder
@themoderncoder 8 ай бұрын
Gotchu: kzbin.info/www/bejne/eISvq3RupKiHbck
@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.
@shobhitsrivastava9112
@shobhitsrivastava9112 8 ай бұрын
wow
@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
@diegoramos27
@diegoramos27 3 ай бұрын
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
@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 3 ай бұрын
kzbin.info/www/bejne/sIDRqJqunqhjhaMsi=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 3 ай бұрын
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.
@whitegroyper
@whitegroyper 26 күн бұрын
gogo gaga
@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 10 күн бұрын
Nice but too abstract 😢
@satishmeghavvarnam1269
@satishmeghavvarnam1269 3 ай бұрын
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 3 ай бұрын
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
Рет қаралды 18 М.
How to be a git expert
46:26
Floating Little Leaves of Code
Рет қаралды 140 М.
Айттыңба - істе ! | Synyptas 3 | 7 серия
21:55
kak budto
Рет қаралды 1,5 МЛН
0% Respect Moments 😥
00:27
LE FOOT EN VIDÉO
Рет қаралды 39 МЛН
蜘蛛侠这操作也太坏了吧#蜘蛛侠#超人#超凡蜘蛛
00:47
超凡蜘蛛
Рет қаралды 44 МЛН
Git MERGE vs REBASE
16:12
Academind
Рет қаралды 1 МЛН
Git REBASE Explained: Unveiling Rebase's Hidden Hazard
4:01
The Modern Coder
Рет қаралды 7 М.
ThePrimeagen Hacks My Productivity
3:30
Scott Macchia
Рет қаралды 29 М.
Testcontainers have forever changed the way I write tests
12:11
Dreams of Code
Рет қаралды 86 М.
GIT: Merge or Rebase? What's the difference?
10:47
Front-end Science із Сергієм Пузанковим
Рет қаралды 128 М.
Git Branching and Merging - Detailed Tutorial
54:28
SuperSimpleDev
Рет қаралды 173 М.
Git rebase: how to resolve conflicts
7:32
Team Programmer
Рет қаралды 36 М.
Git MERGE vs REBASE: Everything You Need to Know
4:34
ByteByteGo
Рет қаралды 306 М.
Learn Git Rebase in 6 minutes // explained with live animations!
6:43
The Modern Coder
Рет қаралды 749 М.
10 Must Know Git Commands That Almost Nobody Knows
15:22
Web Dev Simplified
Рет қаралды 94 М.