1 00:00:00,000 --> 00:00:01,020 In this lesson, 2 00:00:01,020 --> 00:00:04,200 we're going to discuss Git and its core commands. 3 00:00:04,200 --> 00:00:06,450 Now most of the time when developers collaborate 4 00:00:06,450 --> 00:00:07,830 for a certain project, 5 00:00:07,830 --> 00:00:10,530 they need to have a distributed version control system 6 00:00:10,530 --> 00:00:11,700 so they can easily manage 7 00:00:11,700 --> 00:00:13,860 the different versions of the code they're writing 8 00:00:13,860 --> 00:00:17,040 and minimize any conflicts while being able to revert back 9 00:00:17,040 --> 00:00:18,630 to older versions of the code 10 00:00:18,630 --> 00:00:20,640 if they make any kind of mistake. 11 00:00:20,640 --> 00:00:21,690 In this case, 12 00:00:21,690 --> 00:00:24,450 you're going to be able to use something like Git. 13 00:00:24,450 --> 00:00:26,580 Now let's say you just arrived from your vacation 14 00:00:26,580 --> 00:00:28,080 and you wanted to check on the progress 15 00:00:28,080 --> 00:00:29,730 that your team has made on a project 16 00:00:29,730 --> 00:00:31,020 that you were working on. 17 00:00:31,020 --> 00:00:33,480 Well, if you had a version control system, 18 00:00:33,480 --> 00:00:36,630 you could very easily do this by knowing what version it was 19 00:00:36,630 --> 00:00:40,200 or what date it was last controlled or changed. 20 00:00:40,200 --> 00:00:42,150 Now by far, the most commonly used 21 00:00:42,150 --> 00:00:44,220 modern version control system in the world 22 00:00:44,220 --> 00:00:47,190 is known as Git, G-I-T. 23 00:00:47,190 --> 00:00:51,150 Now Git is a mature, actively maintained open-source project 24 00:00:51,150 --> 00:00:53,580 that was originally developed back in 2005 25 00:00:53,580 --> 00:00:57,060 by the same person who developed the Linux operating system. 26 00:00:57,060 --> 00:00:59,310 Now a staggering number of software projects 27 00:00:59,310 --> 00:01:01,530 rely on Git for version control, 28 00:01:01,530 --> 00:01:03,480 including a lot of commercial projects 29 00:01:03,480 --> 00:01:06,060 as well as most of the open-source community. 30 00:01:06,060 --> 00:01:09,690 Its core component of Git is known as the Git repository. 31 00:01:09,690 --> 00:01:12,360 This is a storage area where versions of code 32 00:01:12,360 --> 00:01:14,790 and related files are going to be put. 33 00:01:14,790 --> 00:01:16,260 To get started with Git, 34 00:01:16,260 --> 00:01:18,120 you can use an available package manager 35 00:01:18,120 --> 00:01:20,130 to install the Git package. 36 00:01:20,130 --> 00:01:21,480 To run the Git command, 37 00:01:21,480 --> 00:01:23,610 simply type in git, the options, 38 00:01:23,610 --> 00:01:25,470 and the sub command you want. 39 00:01:25,470 --> 00:01:26,940 Now some of the major sub commands 40 00:01:26,940 --> 00:01:28,260 that you're going to use with Git 41 00:01:28,260 --> 00:01:30,690 include things like the config sub command, 42 00:01:30,690 --> 00:01:33,120 this is used as set up options for repository 43 00:01:33,120 --> 00:01:36,810 or for your Git users, as well as other global options; 44 00:01:36,810 --> 00:01:39,513 the init sub command is used to create a Git repository 45 00:01:39,513 --> 00:01:42,510 or reinitialize an existing one; 46 00:01:42,510 --> 00:01:45,240 the clone sub command is used to create a working copy 47 00:01:45,240 --> 00:01:47,040 of an existing repository; 48 00:01:47,040 --> 00:01:49,590 and the add sub command is used to add files 49 00:01:49,590 --> 00:01:51,930 to be tracked by the Git repository; 50 00:01:51,930 --> 00:01:53,910 the commit sub command is going to be used 51 00:01:53,910 --> 00:01:56,310 to update the Git repository with your changes 52 00:01:56,310 --> 00:01:59,580 and this essentially creates a snapshot of that repository 53 00:01:59,580 --> 00:02:01,200 or a snapshot of the files 54 00:02:01,200 --> 00:02:03,570 at that particular moment in time; 55 00:02:03,570 --> 00:02:05,610 now the status sub command is going to be used 56 00:02:05,610 --> 00:02:08,039 to display the status of the repository; 57 00:02:08,039 --> 00:02:10,830 the branch sub command is used to manage branches 58 00:02:10,830 --> 00:02:13,800 or pointers to specific repository snapshots 59 00:02:13,800 --> 00:02:15,780 after you've committed changes; 60 00:02:15,780 --> 00:02:17,850 the merge sub command is going to be used 61 00:02:17,850 --> 00:02:21,210 to integrate changes from one branch into a master branch; 62 00:02:21,210 --> 00:02:22,860 the pull sub command is used 63 00:02:22,860 --> 00:02:24,480 to acquire and merge changes 64 00:02:24,480 --> 00:02:26,670 that are made to other repositories and branches 65 00:02:26,670 --> 00:02:28,890 into the local working copy; 66 00:02:28,890 --> 00:02:31,050 the push sub command is used to upload 67 00:02:31,050 --> 00:02:33,360 a local working copy of a repository 68 00:02:33,360 --> 00:02:34,890 to a remote repository, 69 00:02:34,890 --> 00:02:37,830 such as a centralized repository or database; 70 00:02:37,830 --> 00:02:40,530 the log sub command is used to display the changes 71 00:02:40,530 --> 00:02:42,360 made to a local repository; 72 00:02:42,360 --> 00:02:43,860 and the checkout sub command 73 00:02:43,860 --> 00:02:45,720 is used to switch to a specific branch 74 00:02:45,720 --> 00:02:47,550 that you want to work with. 75 00:02:47,550 --> 00:02:50,910 All right, let's say you need to create a local repository 76 00:02:50,910 --> 00:02:53,880 and commit some changes to it as you go through the process. 77 00:02:53,880 --> 00:02:56,580 Well, let's talk about the process flow for doing this. 78 00:02:56,580 --> 00:02:59,070 First, you're going to configure global settings 79 00:02:59,070 --> 00:03:00,637 including the username by typing 80 00:03:00,637 --> 00:03:05,637 git config --global user.name 'User', 81 00:03:07,469 --> 00:03:10,260 where users the name of the user you want create. 82 00:03:10,260 --> 00:03:12,390 Second, you're going to create a directory 83 00:03:12,390 --> 00:03:14,220 where your project is going to reside. 84 00:03:14,220 --> 00:03:19,050 To do this, you're going to type in mkdir /dev-project, 85 00:03:19,050 --> 00:03:21,480 and then we're going to change ourself into that directory 86 00:03:21,480 --> 00:03:23,370 by using the cd command. 87 00:03:23,370 --> 00:03:26,430 Now we want to initialize this directory using Git 88 00:03:26,430 --> 00:03:28,230 to make it a Git repository. 89 00:03:28,230 --> 00:03:33,230 So we'll do this by typing git init /dev-project. 90 00:03:33,420 --> 00:03:36,240 Now at this point we want to put some project files 91 00:03:36,240 --> 00:03:37,800 into this repository. 92 00:03:37,800 --> 00:03:38,850 These are going to be the files 93 00:03:38,850 --> 00:03:40,830 that make up the actual development project 94 00:03:40,830 --> 00:03:43,440 that we're storing and controlling using Git. 95 00:03:43,440 --> 00:03:45,660 In Git, you create a working copy 96 00:03:45,660 --> 00:03:47,400 by using the clone sub command. 97 00:03:47,400 --> 00:03:52,400 So we're going to type in git clone /dev-project. 98 00:03:52,980 --> 00:03:56,760 Next, we're going to type Git add myfile 99 00:03:56,760 --> 00:03:58,710 and this will add the project files 100 00:03:58,710 --> 00:04:02,670 into the Git tracking system, specifically myfile. 101 00:04:02,670 --> 00:04:04,920 Next, we want to be able to commit the changes 102 00:04:04,920 --> 00:04:07,110 by taking a snapshot of our project. 103 00:04:07,110 --> 00:04:08,190 So at this stage, 104 00:04:08,190 --> 00:04:10,080 we're going to enter a message that summarizes 105 00:04:10,080 --> 00:04:11,610 what changes we made. 106 00:04:11,610 --> 00:04:16,529 For example, I might type in git commit -m, 107 00:04:16,529 --> 00:04:18,630 and then 'Initial commit, 108 00:04:18,630 --> 00:04:20,156 or whatever message I want'. 109 00:04:21,329 --> 00:04:22,410 Now at this point, 110 00:04:22,410 --> 00:04:24,990 if we want to see the status of our Git repository, 111 00:04:24,990 --> 00:04:26,740 we can type in git status 112 00:04:26,740 --> 00:04:28,710 and this will retrieve the current status 113 00:04:28,710 --> 00:04:30,300 of the change files. 114 00:04:30,300 --> 00:04:32,940 So if I had three files that were being worked on 115 00:04:32,940 --> 00:04:34,680 for a particular step in this project 116 00:04:34,680 --> 00:04:37,020 but only two were ready to be committed at this time, 117 00:04:37,020 --> 00:04:40,590 those would show up here as added but not yet committed. 118 00:04:40,590 --> 00:04:43,770 Another option here is to work with Git branches. 119 00:04:43,770 --> 00:04:45,030 Now branching is a feature 120 00:04:45,030 --> 00:04:47,670 in most modern version control systems. 121 00:04:47,670 --> 00:04:49,097 In Git, branches are part 122 00:04:49,097 --> 00:04:51,540 of your everyday development process, 123 00:04:51,540 --> 00:04:54,750 and Git branches are effectively a pointer to a snapshot 124 00:04:54,750 --> 00:04:56,640 of your different changes you've made. 125 00:04:56,640 --> 00:04:59,280 When you want to add a new feature or a fix or a bug, 126 00:04:59,280 --> 00:05:02,310 no matter how big or small, you're going to spawn a new branch 127 00:05:02,310 --> 00:05:04,380 to encapsulate your changes. 128 00:05:04,380 --> 00:05:06,990 By creating a branch in the master copy of the code, 129 00:05:06,990 --> 00:05:09,030 you're going to be able to then work on that 130 00:05:09,030 --> 00:05:11,310 without affecting all the other developers. 131 00:05:11,310 --> 00:05:13,774 To do this, you're going to type git branch, 132 00:05:13,774 --> 00:05:16,620 and then the name of the new branch that you want. 133 00:05:16,620 --> 00:05:18,000 Then whenever you're done 134 00:05:18,000 --> 00:05:19,230 and you need to merge those changes 135 00:05:19,230 --> 00:05:21,060 back into the master branch, 136 00:05:21,060 --> 00:05:22,920 you're going to type git merge, 137 00:05:22,920 --> 00:05:24,720 and the name of that new branch. 138 00:05:24,720 --> 00:05:26,250 This will be able to make those changes 139 00:05:26,250 --> 00:05:28,530 and put them back into the master branch. 140 00:05:28,530 --> 00:05:31,650 In doing so, allows you to create new and improved versions 141 00:05:31,650 --> 00:05:33,933 of the software and the master branch. 142 00:05:34,830 --> 00:05:36,420 Now this is one of the reasons that Git 143 00:05:36,420 --> 00:05:39,210 is so powerful is this ability to create branches 144 00:05:39,210 --> 00:05:40,860 and then put them back and commit them 145 00:05:40,860 --> 00:05:42,870 into the main master branch. 146 00:05:42,870 --> 00:05:45,180 This allows Git to be very collaborative 147 00:05:45,180 --> 00:05:48,240 and it really becomes game changing for software developers. 148 00:05:48,240 --> 00:05:50,310 So, so far we've been collaborating 149 00:05:50,310 --> 00:05:52,500 with our most important collaborator, right? 150 00:05:52,500 --> 00:05:54,480 We've been working with ourself, 151 00:05:54,480 --> 00:05:57,300 but Git allows us to collaborate with somebody else 152 00:05:57,300 --> 00:05:59,700 or with lots of other people as well 153 00:05:59,700 --> 00:06:01,860 as long as they're also going to have the same privileges 154 00:06:01,860 --> 00:06:03,720 to our Git repository. 155 00:06:03,720 --> 00:06:06,630 Normally, there's going to be some sort of a process flow 156 00:06:06,630 --> 00:06:09,057 when you're collaborating with other developers using Git, 157 00:06:09,057 --> 00:06:10,380 and you're going to work that out 158 00:06:10,380 --> 00:06:12,780 based on your organizational requirements. 159 00:06:12,780 --> 00:06:15,270 For example, a standard way of doing this 160 00:06:15,270 --> 00:06:17,580 might be to first type git pull, 161 00:06:17,580 --> 00:06:19,410 and then pull in the other branch 162 00:06:19,410 --> 00:06:21,870 to pull the other developer's code and changes 163 00:06:21,870 --> 00:06:24,180 and merge them into your local repository. 164 00:06:24,180 --> 00:06:26,580 Then you might type git push, 165 00:06:26,580 --> 00:06:29,070 and the remote repository with your branch 166 00:06:29,070 --> 00:06:32,430 to push your own changes back to that remote repository. 167 00:06:32,430 --> 00:06:34,590 This allows developers all over the world 168 00:06:34,590 --> 00:06:37,620 to pull and push changes into the central repository 169 00:06:37,620 --> 00:06:39,450 so they can all work together. 170 00:06:39,450 --> 00:06:42,090 Now any changes made locally are then going to be uploaded 171 00:06:42,090 --> 00:06:45,330 to that central repository using that push command, 172 00:06:45,330 --> 00:06:47,010 and you can see what changes were merged 173 00:06:47,010 --> 00:06:50,070 and what other actions were taken on that repository. 174 00:06:50,070 --> 00:06:51,660 Again, if you want to be able to see 175 00:06:51,660 --> 00:06:53,490 what's inside your project directory, 176 00:06:53,490 --> 00:06:55,350 you can type git log 177 00:06:55,350 --> 00:06:57,750 and that's going to be able to tell you all the changes 178 00:06:57,750 --> 00:07:00,330 that have happened over a certain period of time. 179 00:07:00,330 --> 00:07:05,330 For example, if I enter git log --since=10.days, 180 00:07:05,580 --> 00:07:07,470 I'm going to see all the commits that have happened 181 00:07:07,470 --> 00:07:09,270 in the last 10 days. 182 00:07:09,270 --> 00:07:12,000 I can use this information to troubleshoot any issues 183 00:07:12,000 --> 00:07:14,490 that may have come up in the last 10 days 184 00:07:14,490 --> 00:07:15,870 or anything I might have missed 185 00:07:15,870 --> 00:07:18,420 if I was on vacation for the last 10 days. 186 00:07:18,420 --> 00:07:19,620 Another thing you can do 187 00:07:19,620 --> 00:07:22,530 is you can navigate and switch between branches of a project 188 00:07:22,530 --> 00:07:25,320 by using the checkout command inside of Git. 189 00:07:25,320 --> 00:07:27,517 To do this, you'll type git checkout 190 00:07:27,517 --> 00:07:30,150 and the specific branch you want to check out. 191 00:07:30,150 --> 00:07:31,500 This enables developers 192 00:07:31,500 --> 00:07:33,690 to focus their attention on different branches 193 00:07:33,690 --> 00:07:34,980 of the same project 194 00:07:34,980 --> 00:07:37,500 and really hone in on what they're working on. 195 00:07:37,500 --> 00:07:39,000 Now another thing that we need to talk about 196 00:07:39,000 --> 00:07:42,090 is this file known as the gitignore file. 197 00:07:42,090 --> 00:07:44,760 This is written as .gitignore, 198 00:07:44,760 --> 00:07:47,250 and it's a file that exists within the repository. 199 00:07:47,250 --> 00:07:49,800 The purpose of this file is to identify files 200 00:07:49,800 --> 00:07:52,890 that should be ignored during a commit action. 201 00:07:52,890 --> 00:07:55,462 For example, you might have a README.txt file 202 00:07:55,462 --> 00:07:59,670 or a project todo.txt list that's inside this directory 203 00:07:59,670 --> 00:08:01,020 and you don't need that to be committed 204 00:08:01,020 --> 00:08:02,400 as part of the project, 205 00:08:02,400 --> 00:08:05,220 but it does reside inside your project directory 206 00:08:05,220 --> 00:08:06,720 just for your convenience. 207 00:08:06,720 --> 00:08:08,580 Well, if you're going to use those 208 00:08:08,580 --> 00:08:11,190 and you list them out inside the gitignore file, 209 00:08:11,190 --> 00:08:13,920 those will Git ignored when you do a commit. 210 00:08:13,920 --> 00:08:17,670 Another thing that's important is the *.git/. 211 00:08:17,670 --> 00:08:20,070 This is a directory that contains all the files 212 00:08:20,070 --> 00:08:21,360 that Git is going to use 213 00:08:21,360 --> 00:08:24,330 to manage version control for your project. 214 00:08:24,330 --> 00:08:25,830 It's going to be a single location 215 00:08:25,830 --> 00:08:28,050 where Git stores all of its information. 216 00:08:28,050 --> 00:08:30,600 Think about it like almost like a flat database. 217 00:08:30,600 --> 00:08:33,299 The directory here resides in the project directory 218 00:08:33,299 --> 00:08:35,220 and it's created with the git init command 219 00:08:35,220 --> 00:08:37,169 at the beginning of your project. 220 00:08:37,169 --> 00:08:40,620 So as you can see, there is a lot to know inside of Git, 221 00:08:40,620 --> 00:08:41,640 but the good news 222 00:08:41,640 --> 00:08:44,159 is you're not going to know it fully in depth. 223 00:08:44,159 --> 00:08:47,010 Git is something used heavily by software developers 224 00:08:47,010 --> 00:08:49,710 and not nearly as much by system administrators. 225 00:08:49,710 --> 00:08:51,120 It's something you should know about, 226 00:08:51,120 --> 00:08:53,920 but you're not going to be an expert on it for the exam.