In this video, you are finally going to understand Git and GitHub. But not just the commands, but what's actually happening underneath. And I want to talk to two kinds of people watching right now. Maybe you've heard of Git, you've installed it, you have even run a few Git commands by just copying them from internet, but if someone asked you what a commit actually is, or what's happening when you push or pull from a repository, you couldn't really say. Or maybe you use Git every single day at work. You commit your work, you push, pull, merge, but you don't really understand how it really works. So, when something breaks, you don't know what to do. You'll probably ask AI for help and copy commands that you don't understand. Now, here's the thing. Both of you have the exact problem. Git feels like a black box. So, in the next few minutes, I'm going to open that box with simple pictures and everyday analogies, and I promise by the end, all the main Git concepts that feel like mystery will finally become clear. Here is exactly what you'll walk away knowing. How Git saves your work, what's a Git history, what's actually inside a commit, and why Git almost never loses your work. Second, what a branch really is underneath and why it's almost free to create. Third, how real teams build on GitHub without ever overriding each other. And finally, the real difference between reset, revert, and rebase. The three commands most people don't really understand. And we actually created a handout with all the concepts and commands that I will cover here, so download it from below for an easy reference, and let's get started. But first, quick thing. We noticed that 80% of people who watch our videos every month are still not subscribed yet. Me and our entire video editor team are putting insane time and effort into each and every video to make it as valuable and high quality as possible for you. So, So, if any of these videos have helped you at all, giving it a like and subscribing to the channel and becoming officially part of our community is the simplest way to support us. And it would mean the world to us. Seriously. Thank you and let's get into it. So, let's start from the beginning. Imagine you're writing code for a project and it finally works. So, just in case, you copy the whole folder and call it project-final. Then you change one thing and you make another copy, project final version two. A day later, you call it project final really final. Now a week passes and you realize the version from two days ago actually worked better. So, you go to open it, but wait, was that version two or version three? And what exactly did you change between them again? You have no idea. So, you're opening files side by side, comparing them by eye, trying to remember what happened and what you changed. Now, keep this exact folder in your mind because every single thing we covered today exists to fix one piece of this entire mess. So, let's start with the very first problem. So, let's stay with that messy folder and really look at what's going wrong. First problem, you're saving copies by hand. So, you only have the versions you remembered to save. Second, the copies tell you nothing. Project final V2 does not say what changed or why. And third, to compare two versions, you have to open both and check line by line. Now, imagine a tool that fixes all three. Every time you reach a point worth keeping, you tell that tool to save this and it takes a complete snapshot of your whole project at that moment, then keeps every such snapshot in order, so in a sequence, attaching a short notes of what changed exactly. Like fixed the checkout bug or added a payment functionality and so on, and lets you jump back to any one of those snapshots. And that's exactly what Git is. Each snapshot on the timeline is called a commit. So, you're committing a version into your history timeline. But, a commit is actually not just a snapshot. Each commit stores three things: the snapshot of your files, some metadata like who made the changes, when, and the description of what changed. And most importantly, a pointer to its parent, which is the commit that came right before it. And every commit gets a unique ID. It's a long string called a hash. So, think of it like a unique fingerprint. And Git calculates that hash from the commit's entire contents. So, if even one character of one file changes in the entire folder, that fingerprint changes completely. Now, this is interesting because each commit points back to its parent, your commits form a chain, each one linked to the one before, all the way back to the very first commit. That chain is your entire Git history. So, the whole mess of final V2 really final folders is replaced by one clean chain of fingerprinted snapshots. So, now we understand concept of commits. But, the moment you start committing, a new situation or new challenge comes up. Let's say you sit down and do two unrelated things. You fix a bug on the payment page, so that part's done, but you also start a new feature, but it's only half finished. Now, you want to save your work. If you commit everything together, you've got a problem because this snapshot mixes a finished bug fix with a broken half-built feature. So, later, if you wanted to undo just the feature or just the bug, you can't because it's tangled up with the fix. So, what you really want is to say, "For this snapshot, take only the bug fix. Leave the half-finished feature for next time." So, you want to choose change by change what goes into each commit. And that's exactly where the staging area comes in, or sometimes called the index. Before you commit, you put the specific changes you want into a waiting area or a staging area with git add commit. And then git commit actually snapshots only what's in the staging area. All right. So, think of it like packing a box before you ship it. You decide exactly what goes into each box separately before you seal it. So, the rhythm is always editing your files, git add specific file changes to choose what's relevant, and then git commit to save to seal that box and to save that snapshot of what you edit. Again, all these commands and concepts will be in the handout for quick visual reference. So, just grab it from below. Now, let's say your project is live. Real people are using it, and you want to build something new for the project, like a login page. Now, if you build it directly on your live version and break something halfway through, then real users will hit those bugs. So, you think, "Fine, I'll just copy the whole project and just build there and test it around until it's fully functioning." But now, you are back to the copy problem, merging files by hand. So, what you actually want is to split off from your timeline a separate line of work where you can commit freely, experiment, mess things up, even break things, while your main version stays safe. And that's exactly what a branch is. So, you create a branch called login feature to develop that feature without affecting the main version. And it splits off your main line. Now, here's an interesting question. When you create a branch, does git copy all your files? Just duplicate the whole thing? Well, the answer is no. A branch is literally just a pointer, a tiny file that contains one commit hash. So, creating a branch means writing one small file. That's all. It's like sticking a labeled bookmark onto one commit in your chain. And that's why branches are instant and basically free, and why developers create one for every small task. So, when you make a new commit on that branch, the branch pointer simply just slides forward to the new commit automatically. Now, let me explain a concept that is confusing for even experienced Git users sometimes. In very simple terms, imagine your commits as a row of photos laid out left to right in the order you took them. A branch, remember, is just a little name label stuck onto the newest photo. And you can have more than one label on one photo. Here, we've got two labels sitting there, main and login feature. So, here's the problem. You're about to take a new photo, so make a new commit, but there are two labels. So, which one should move forward onto your new photo? Git needs some way to know which branch you're actually working on right now. So, to keep track of that, Git uses one more little marker to track where you are, on which branch, on which commit at any point, which is a small you're here arrow. And this arrow is called head. All it does is point at the one label you're using right now. And this is where one everyday command comes in, called Git checkout. So, when you run Git checkout login feature, you're telling Git, "I want to work on this branch now." And Git simply moves your you're here arrow over from the main branch, or wherever you are right now, onto the login feature label. That's all switching branches actually is. Just moving head pointer from one label to another. Now, let's take a new photo, meaning you make a new commit. First, a new photo appears at the end of the row. Then, the login feature label steps forward onto the new photo. And because your you are here arrow was pointing at that label, the arrow also comes along with it. So, now the label and the arrow are both sitting on the newest photo or newest commit, ready for the next one. So, that's the whole point of head. You never manage these by hand, which means your new commits always land on the right branch because head is quietly following whichever branch you checked out. And once you can picture all this and understand conceptually one confusing warning finally makes sense. Normally, you check out a branch, a label, but you can also check out one specific commit, like a single photo, by pointing git checkout straight at that photo instead of a label. Maybe you just want to go back and look at an old snapshot of your project from last week. But here's the difference. That old photo has no branch label on it. So, your your here arrow lands directly on the bare photo without a label, which means not on the branch. Git calls this detached head, which means your head is pointing straight at a commit instead of at a branch. You're looking at a past snapshot of your project, but you're not on any branch right now. You can still look around and browse. You can even take new photos there or make new commits. But because there's no label following it, those new commits do not actually belong to any branch. Nothing is marking them as part of your project's normal history. So, they're very easy to lose track of afterwards. So, that's what this scary detached head message really means is Git is warning you that you're going to lose track of these changes. So, you need to go back to the branch and continue working there. Now, everything we've built so far, every commit, every branch, this entire photo album as an analogy lives in exactly one place, which is your local computer. And the moment you work with other people, that causes two serious problems. First, it's fragile. If your laptop is lost or breaks, your only copy of the album, all your branches, all your commits, the entire Git history, just goes with it because it's not saved elsewhere. And second, nobody can reach your work. Your teammate needs your commits and Git history to do their part, but they're sitting on your machine. So, do you email them the entire project folder every time you make a new commit? And do they email theirs back when they make changes? Obviously not, right? So, what you really want is one shared album. Let's go with the photo album analogy here. So, one shared album that doesn't live on anyone's personal computer, it lives in one place online that everybody can reach. And everyone sends their new photos up to it, and everyone brings everyone else's photos down from it locally. So, one shared copy that's the single source of truth and a safe backup if your laptop dies. And that's exactly what GitHub is. This is also the moment to clear up the most common beginner confusion. Git and GitHub are not the same thing. Git is a tool on your computer. It's a command-line tool and the whole mechanism around structuring your code and changes. It's what makes the photos and keeps your album in order. GitHub is a website, is a platform that holds a copy of that album online, where your whole team can reach it. Because that copy lives away from your machine, it's called the remote. So, basically just the remote copy of the code base. And you only need three commands to move work between your computer and that shared copy. So, basically when you start, you You on GitHub and you just create an empty album, again, our photo album analogy, that the team will share. And this is what's called a remote or shared Git repository or Git project. And then to add your work into this empty Git project on GitHub, you basically only need three Git commands. Git clone with a GitHub project's URL in order to connect to that shared project from your local computer and download that empty initial project. And this will create a folder on your computer with all the connection information. And you then just take your local work and copy into that folder. And then you do Git push, which means which takes all your photos locally and sends them up to the shared album, that repository. And then once your teammates start adding their photos up there as well, their commits and changes, you need Git pull to bring their changes down to your local computer. So, everyday rhythm of a team using Git is very simple. You pull down the latest changes, do your work, you push your latest changes back up for the rest of the team to then pull it down on their computers. But hold on, that word pull is actually way more than just a command. There is much more behind it. Because when you bring your teammates changes down, those new changes or photos don't just magically slot into your album. Their work and your work have to be combined. And combining two sets of photos is its own thing and challenge that Git manages for you, but comes with a few surprises. First of all, that combining of two rows of photos, two sets of changes that two people have been working in parallel, is called merging. Let's say your login feature branch is finished and you want it back into main. You stand on main, that's where your head is, and merge the other branch in with Git merge command. Now, what Git does here depends on one simple question. Did main move change while you were working in login feature branch? Case one, nobody touched main while you were gone. So, your login feature photos continue straight on from where main still is. So, Git doesn't build anything new here. [clears throat] It just slides the main label forward until it lands on your latest photo. So, your history stays one clean straight line. Git calls this a fast forward. Main simply fast forwarded up to the latest commit in login feature branch. Now, case two, teammates added photos to main branch while you were gone, which is the most common scenario when you work in a team. So, now main and login feature have both moved forward in different directions and each has photos the other doesn't. Git cannot just slide a label here. So, what it does, it adds one brand new photo called a merge commit. A normal photo points back to the one photo before it. A merge commit points back to two at once, the newest photo on main and the newest on login feature, tying the two lines into one. And now, the connection back to Git pull. Remember from last section, pull [clears throat] brings your teammates work down and combines it or merges. That's because pull is really two steps in one. First, it downloads their new photos, which is Git fetch command, separately, but then it merges them into your branch. So, those are two commands in one. So, every time you pull, you're merging the remote changes into your local ones. Now, one more thing here. That merge photo or merge commit that Git creates does not contain any real work, right? Its only job is to tie two lines together. So, that means if your team merges constantly, then your history just fills up with these empty commits or empty photos. But, often you'd rather keep the straight line and avoid these noisy commits. So, instead of tying the lines together, you can pick up your own photos and relay them at the end of the latest mate, as if you'd started after your teammates. And that separate command is called rebase. So, instead of merge, we're doing rebase. And you can even tell pull command to do this instead of a normal merge by providing git pull rebase parameter. So, for now, hold the simple contrast here. Merge ties two lines together with an extra photo. Rebase relays your photos onto the end, so the line stays straight. But, we'll come back to rebase properly later. It has one more important side that you need to understand. And finally, let's step back and look at the shape of our history. It used to be a straight line, but with merges, it forks and rejoins, like a road that splits and later comes back together. So, once a whole team does this all the time, your history just becomes a web of splits and joints. Now, there is one important problem that happens when everyone starts pushing and merging their changes into the main branch. Imagine your junior teammate merges a feature branch straight into the main branch. Nobody looked at it, no senior or experienced engineer reviewed the code, and it turns out it had a bug that breaks the entire app for everyone. And the whole team is now blocked because one change went in unchecked. Plus, that bug may end up with end users, damaging reputation of your app. So, instead, what you want is, before anyone's work joins the main project, they need to propose it. So, basically, they need to say, "I've done all these changes. My feature branch is now ready for my point of view, and I don't have a permission to merge this in because I'm a junior developer, can someone look at my changes, review it, and confirm that this can be merged safely. So, a teammate reviews it, and only then, once approved, it's added in. And that's exactly what a pull request is, or sometimes also called a merge request. So, you're requesting that your changes be pulled into the main branch. So, the real [snorts] everyday workflow of this is you pull the latest, you create a branch called login feature or a bug fix or whatever. You commit your work, you push that branch into GitHub, and once you're done, you open a pull request saying, "Here are my changes, please review before we merge." Now, your teammate sees your pull request, and they comment, "Can you rename this variable? Also, this is not a security best practice. Can you please fix and change it?" You go back, you fix, you commit those changes, push again, and it updates the same pull request automatically. Everyone's happy, they click merge, and your feature joins the project, reviewed with a full record of who changed what and why. And this is a functionality that you can use on GitHub with the entire workflow. So, basically, it's not just storage for your code files, it has features that allow your whole team to review work, keep different versions, and so on. And now that you understand commits, pointers, and sharing, let's tackle the thing that everyone Googles or chat GPTs in panic, which is "Oh my god, I just made a mistake. How do I undo this?" Because there is not just one undo in Git. There are actually three. And which one you choose depends entirely on one question. Have you already pushed the changes to the remote or not? So, situation one, you made two commits locally, they're a mess, and you haven't pushed them yet. And you want to fix that mess. And that's where reset command comes in. It slides the branch pointer backward to an earlier commit, and the photos after it fall out of album. And it comes in three flavors, so to say. And the difference between the three is what happens to the actual changes once they have been removed from the commit history. The first one is the soft parameter. So, get reset soft. That moves the pointer, so removes the commit, but keeps your changes in the staging area. So, you still have the changes locally, but they have been removed from the commit history. With our analogy, you still have the photos in your hand already to re-commit or make changes first. The default one, if you don't provide any parameter, is mixed. So, reset mixed keeps your changes in the files, but un-staged. So, they're not added to the staging area yet. These are like having photos just loose on the desk. And final is hard. Reset hard, that moves the pointer, throws away all those changes completely. So, the photos go straight in the bin, which is the destructive one. But, we have situation two where the bad commit is already pushed to the remote repository, and your teammates may already check them out. Now, you should not just slide the pointer back. You should not remove the commit from the history, because then you're just destroying everyone else's copy of the project. So, instead of erasing, you want to undo it forward. And that's a command called revert. So, basically get revert simply creates a brand new commit on top of the bad commit that is the exact inverse of the bad one. Like adding a negative of a photo, something like that. So, it cancels out the changes while leaving the full album intact. So, here's a rule that ties it together. Reset rewrites the history, which is safe locally if you haven't pushed the commit to the remote yet. And revert adds to the history, just doing the inverse of the bad commit, which is safe to use when you have already pushed your changes to the remote. So, with all this, we actually covered the most important concepts of Git, and if you understand this, you're probably ahead of 80% of engineers out there. And if you want to keep this knowledge, make sure to download the accompanying handout from the video description as an easy refresher for these concepts. So, to recap, we started with a messy folder, and we introduced a proper mechanism to not only keep track of the changes ourselves, but to work on it on a whole team level. So, Git gives you one clean chain of snapshots, which are commits, each a fingerprinted box pointing to its parent. The staging area lets you choose what goes into each one. Branches are just cheap pointers or labels on [snorts] commits, and head marks where you are at any given point. GitHub takes the whole thing online, so your team can collaborate through push and pull requests, and when something goes wrong, you've got reset and revert. Because once you stop just memorizing commands and blindly executing them and start seeing Git as a graph of snapshots connected by pointers, then the whole thing just finally clicks. If this video made Git make sense to you, then share it with one friend or colleague who you think will also benefit from this knowledge. And tell me in the comments what other concepts you want me to cover next. And with that, thanks for watching, and I'll see you in the next video.
Generated algorithmically for Search Engine
Indexing.