1 00:00:06,930 --> 00:00:09,630 - All right, let's talk about branches. 2 00:00:09,630 --> 00:00:11,730 Branches are used to develop new features 3 00:00:11,730 --> 00:00:13,830 in isolation from the main branch 4 00:00:13,830 --> 00:00:15,300 so that you can quietly test. 5 00:00:15,300 --> 00:00:16,950 And at the moment that you're ready, 6 00:00:16,950 --> 00:00:18,660 you are going to merge. 7 00:00:18,660 --> 00:00:20,520 So the main branch is the default branch 8 00:00:20,520 --> 00:00:22,593 and other branches can be manually added. 9 00:00:23,460 --> 00:00:25,890 Any git repository can have multiple branches 10 00:00:25,890 --> 00:00:28,740 to allow developers to work independently. 11 00:00:28,740 --> 00:00:31,080 And after completion, you can merge the branches 12 00:00:31,080 --> 00:00:32,613 back to the main branch. 13 00:00:33,570 --> 00:00:34,747 In other versions of Git, 14 00:00:34,747 --> 00:00:36,600 "master" was the name of the main branch. 15 00:00:36,600 --> 00:00:37,890 We already talked about that. 16 00:00:37,890 --> 00:00:41,250 That's nowadays deprecated terminology. 17 00:00:41,250 --> 00:00:46,050 And if it's needed, you can use git branch -M main 18 00:00:46,050 --> 00:00:49,110 to set main as the default branch name. 19 00:00:49,110 --> 00:00:52,020 I'd recommend to do that to avoid confusion. 20 00:00:52,020 --> 00:00:54,870 And when you see master, well you know it's the old way 21 00:00:54,870 --> 00:00:56,943 and you should replace main with master. 22 00:00:57,960 --> 00:00:59,640 Now if you work with branches 23 00:00:59,640 --> 00:01:02,970 every now and then it's important to compare your branches. 24 00:01:02,970 --> 00:01:05,430 You can use git diff to do so. 25 00:01:05,430 --> 00:01:08,310 So git diff followed by the different branches 26 00:01:08,310 --> 00:01:09,990 will preview branch differences 27 00:01:09,990 --> 00:01:12,393 and see if there are any potential conflicts. 28 00:01:13,410 --> 00:01:16,830 If you want to merge another branch into the active branch 29 00:01:16,830 --> 00:01:19,140 then you use git merge followed by the name 30 00:01:19,140 --> 00:01:20,403 of the remote branch. 31 00:01:21,450 --> 00:01:23,340 So how do we use branches? 32 00:01:23,340 --> 00:01:26,340 Well, you can use git checkout -b 33 00:01:26,340 --> 00:01:28,560 followed by the branch name to create new branch 34 00:01:28,560 --> 00:01:29,643 and start using it. 35 00:01:30,510 --> 00:01:33,930 Then use git push origin dev-branch to push the new branch 36 00:01:33,930 --> 00:01:36,000 to the remote repository. 37 00:01:36,000 --> 00:01:37,440 Once you have pushed the new branch 38 00:01:37,440 --> 00:01:39,060 to the remote repository, 39 00:01:39,060 --> 00:01:41,280 you can use git checkout main to switch back 40 00:01:41,280 --> 00:01:42,870 to the main branch. 41 00:01:42,870 --> 00:01:46,950 And delete the branch using git branch -d dev-branch. 42 00:01:46,950 --> 00:01:49,470 Or maybe you want to merge before you do so. 43 00:01:49,470 --> 00:01:51,003 So let's talk about merges. 44 00:01:51,990 --> 00:01:53,700 After you make changes to a branch, 45 00:01:53,700 --> 00:01:56,460 the changes can be merged into the main branch. 46 00:01:56,460 --> 00:01:59,790 And that's typically what everybody wants to do, right? 47 00:01:59,790 --> 00:02:02,280 That's what you make your changes for. 48 00:02:02,280 --> 00:02:04,230 Before you do so, it's a good idea to have a look 49 00:02:04,230 --> 00:02:05,730 at the differences. 50 00:02:05,730 --> 00:02:07,410 git diff is going to help you. 51 00:02:07,410 --> 00:02:09,060 So git diff main newfiles, 52 00:02:09,060 --> 00:02:12,993 if newfiles is the name of your new branch, will do that. 53 00:02:14,280 --> 00:02:16,260 So after you have used git checkout main 54 00:02:16,260 --> 00:02:18,690 you can use git merge newfiles to apply changes 55 00:02:18,690 --> 00:02:20,643 from the branch to the main branch. 56 00:02:21,480 --> 00:02:24,000 And if files have been modified in multiple branches, 57 00:02:24,000 --> 00:02:26,430 automatic merges are not possible 58 00:02:26,430 --> 00:02:28,800 where merge conflicts exists. 59 00:02:28,800 --> 00:02:32,280 In such case you need a little bit more of work. 60 00:02:32,280 --> 00:02:34,680 The conflicts must be resolved manually 61 00:02:34,680 --> 00:02:36,573 by editing the affected files. 62 00:02:37,980 --> 00:02:40,740 So before showing you the demo, one more thing. 63 00:02:40,740 --> 00:02:42,720 Instead of git checkout, nowadays 64 00:02:42,720 --> 00:02:44,730 you'll also see git switch. 65 00:02:44,730 --> 00:02:46,440 Git switch, which allows you to switch 66 00:02:46,440 --> 00:02:48,030 between different branches. 67 00:02:48,030 --> 00:02:49,920 You want to use git switch, 68 00:02:49,920 --> 00:02:50,820 that's fine as well. 69 00:02:50,820 --> 00:02:52,170 Where I'm typing, git checkout 70 00:02:52,170 --> 00:02:54,360 just use git switch and it'll work. 71 00:02:54,360 --> 00:02:55,193 Let me show you. 72 00:03:02,740 --> 00:03:06,670 Okay, to start with git branch newfiles 73 00:03:07,770 --> 00:03:09,870 and I'm using git status. 74 00:03:09,870 --> 00:03:13,773 Git status is telling us that we are on the branch main. 75 00:03:14,670 --> 00:03:17,320 Now git checkout newfiles 76 00:03:18,390 --> 00:03:20,460 switches to the branch, newfiles. 77 00:03:20,460 --> 00:03:22,110 I don't even have to do a git status 78 00:03:22,110 --> 00:03:23,400 in order to do so. 79 00:03:23,400 --> 00:03:25,770 I'm in newfiles right now. 80 00:03:25,770 --> 00:03:27,300 So as you can see in newfiles 81 00:03:27,300 --> 00:03:32,300 we still have the current content of the main branch 82 00:03:32,583 --> 00:03:35,460 and that's what we need because we want to develop 83 00:03:35,460 --> 00:03:37,560 on the content of the main branch. 84 00:03:37,560 --> 00:03:41,677 So I'm using echo three > three.txt. 85 00:03:44,700 --> 00:03:47,100 The mission here is that I need a file 86 00:03:47,100 --> 00:03:49,810 and I am going to use my git add 87 00:03:52,800 --> 00:03:54,633 as well as my git commit. 88 00:03:58,650 --> 00:04:01,360 And next my git push 89 00:04:03,660 --> 00:04:05,455 where I need the argument 90 00:04:05,455 --> 00:04:10,455 - -set-upstream origin newfiles. 91 00:04:11,610 --> 00:04:13,590 We need to tell the remote environment 92 00:04:13,590 --> 00:04:15,150 about this branch as well. 93 00:04:15,150 --> 00:04:16,770 And for now the remote environment 94 00:04:16,770 --> 00:04:19,233 doesn't know anything so this is important. 95 00:04:21,600 --> 00:04:24,930 So now the git push has been done 96 00:04:24,930 --> 00:04:27,753 and I'm going to use git checkout main. 97 00:04:28,920 --> 00:04:31,470 So we are in the main branch. 98 00:04:31,470 --> 00:04:33,810 And what is LS showing? 99 00:04:33,810 --> 00:04:38,810 Well, LS is showing a lot but we don't see three.txt. 100 00:04:38,970 --> 00:04:42,543 That is letters three.txt, the file that we just created. 101 00:04:43,440 --> 00:04:46,110 You can use git diff if you want to compare. 102 00:04:46,110 --> 00:04:47,920 Git diff main newfiles 103 00:04:48,810 --> 00:04:52,140 because there we can see the differences. 104 00:04:52,140 --> 00:04:54,600 And in the differences on the top you can see 105 00:04:54,600 --> 00:04:58,050 that we have a +++ three txt. 106 00:04:58,050 --> 00:04:59,760 That's the differences. 107 00:04:59,760 --> 00:05:02,940 So if I'm using git status, then what do we see? 108 00:05:02,940 --> 00:05:05,250 Well, we see nothing relevant. 109 00:05:05,250 --> 00:05:07,710 Only the message about this git ignore. 110 00:05:07,710 --> 00:05:10,050 Now what if I want to merge the changes? 111 00:05:10,050 --> 00:05:13,830 Well that will be git merge newfiles. 112 00:05:13,830 --> 00:05:15,750 That is going to merge the changes 113 00:05:15,750 --> 00:05:18,753 of newfiles into the main branch. 114 00:05:19,770 --> 00:05:23,310 And now if I use my ls command again, 115 00:05:23,310 --> 00:05:25,830 there we can see that three.txt 116 00:05:25,830 --> 00:05:28,140 has been merged into the main branch. 117 00:05:28,140 --> 00:05:29,850 And what is git status showing? 118 00:05:29,850 --> 00:05:32,520 Well, git status is showing on branch main, 119 00:05:32,520 --> 00:05:34,410 your branch is ahead of origin main 120 00:05:34,410 --> 00:05:35,640 by one commit. 121 00:05:35,640 --> 00:05:39,570 Use git push to publish your local commits. 122 00:05:39,570 --> 00:05:42,630 So if you merge, it's getting into the head 123 00:05:42,630 --> 00:05:44,490 and we need to get it out of the head. 124 00:05:44,490 --> 00:05:47,193 And that's what's happening using git push. 125 00:05:48,660 --> 00:05:51,453 And now once more, git status to verify. 126 00:05:52,530 --> 00:05:54,660 And now we can see your branch is up to date 127 00:05:54,660 --> 00:05:55,860 with origin main. 128 00:05:55,860 --> 00:05:57,810 It's always important to use git status, 129 00:05:57,810 --> 00:05:59,460 use git status a lot. 130 00:05:59,460 --> 00:06:01,773 It avoids you from getting confused.