1 00:00:00,970 --> 00:00:05,420 Before we continue, here's a quick note for those of you using Workspaces. 2 00:00:05,420 --> 00:00:08,480 Depending on how long it's been since you've watched the last video, 3 00:00:08,480 --> 00:00:10,890 your Workspace connection may have timed out. 4 00:00:10,890 --> 00:00:12,530 When you reconnect to the Workspace, 5 00:00:12,530 --> 00:00:17,120 you'll find yourself in the directory named workspace, not the Medals Git repo. 6 00:00:17,120 --> 00:00:20,340 Git commands won't work outside of Git repo. 7 00:00:20,340 --> 00:00:22,270 Whenever you reconnect to a workspace, 8 00:00:22,270 --> 00:00:26,250 make sure to change back into the repo directory with cd medals. 9 00:00:27,370 --> 00:00:30,090 Once you've done that, you'll be able to issue git commands again. 10 00:00:31,230 --> 00:00:34,830 We've initialized the git repository in our projects directory, but 11 00:00:34,830 --> 00:00:38,200 our projects files haven't been added to the repository yet. 12 00:00:38,200 --> 00:00:41,340 In fact, git has no idea they exist. 13 00:00:41,340 --> 00:00:46,210 In this video, we're going to fix that by staging and then committing these files. 14 00:00:46,210 --> 00:00:50,280 Our project directory is known in git terminology as the working directory, 15 00:00:50,280 --> 00:00:52,810 because it's the directory where we actually edit and 16 00:00:52,810 --> 00:00:55,220 do other work on our files. 17 00:00:55,220 --> 00:00:59,326 Right now, our working directory contains two files, medals.html and 18 00:00:59,326 --> 00:01:02,640 bronze.html, that haven't been added to git. 19 00:01:02,640 --> 00:01:06,290 There are three states every file goes through in a git repository. 20 00:01:06,290 --> 00:01:10,820 When you make changes to a file in the working directory, it's modified. 21 00:01:10,820 --> 00:01:14,090 You don't necessarily want to include all of these modified files 22 00:01:14,090 --> 00:01:15,070 in your next commit. 23 00:01:15,070 --> 00:01:18,550 So you need to specify which ones you will include. 24 00:01:18,550 --> 00:01:21,180 You do this by adding files to the index, 25 00:01:21,180 --> 00:01:24,660 more commonly known as the staging area or cache. 26 00:01:24,660 --> 00:01:28,540 The staging area is where you place the files you are going to commit. 27 00:01:28,540 --> 00:01:32,168 Files you've added to the index are referred to staged files. 28 00:01:32,168 --> 00:01:35,110 When you staged all the files you want you make a commit and 29 00:01:35,110 --> 00:01:38,710 that's when the files are actually added to your git repository. 30 00:01:38,710 --> 00:01:41,550 Then when you next make a change to any of those files, 31 00:01:41,550 --> 00:01:44,320 they're treated as modified again. 32 00:01:44,320 --> 00:01:47,740 You can stage and commit the files again to save a new version of them. 33 00:01:47,740 --> 00:01:52,630 And the cycle repeats, let's try finding out what state are project files are in 34 00:01:52,630 --> 00:01:55,270 using the git status command. 35 00:01:55,270 --> 00:01:58,600 This is our go to command to find out the current status or 36 00:01:58,600 --> 00:02:00,100 what's going on with git. 37 00:02:00,100 --> 00:02:01,467 You'll be using it a lot. 38 00:02:03,471 --> 00:02:07,060 There's some stuff at the top of the output that we can ignore for now. 39 00:02:07,060 --> 00:02:09,490 Then we have a list of untracked files, 40 00:02:09,490 --> 00:02:11,790 these are files that git isn't tracking yet. 41 00:02:11,790 --> 00:02:16,320 It's not keeping track of changes to them, so we're not saving new versions of them. 42 00:02:16,320 --> 00:02:17,980 Since the git repo is brand new, 43 00:02:17,980 --> 00:02:21,140 that means that both of the files in our project are untracked. 44 00:02:22,140 --> 00:02:27,340 There are also a couple helpful messages encouraging us to run the git add command. 45 00:02:27,340 --> 00:02:33,434 Let's try that, git add, and then the name of the file we want to add, medals.html. 46 00:02:35,440 --> 00:02:37,490 We'll hit Enter and the command will run. 47 00:02:37,490 --> 00:02:40,330 There's no output which is actually a good thing. 48 00:02:40,330 --> 00:02:43,440 Many git commands produce output only when there's an error, 49 00:02:43,440 --> 00:02:46,130 empty output means everything went fine. 50 00:02:46,130 --> 00:02:49,020 The git add command adds a file to the staging area, 51 00:02:49,020 --> 00:02:50,590 the place that we compose our commit. 52 00:02:51,720 --> 00:02:54,840 Now let's try running git status again to see what's changed. 53 00:02:55,890 --> 00:02:58,320 The output is now in two sections. 54 00:02:58,320 --> 00:03:01,060 There's a new changes to be committed section, 55 00:03:01,060 --> 00:03:03,440 which lists files in the staging area. 56 00:03:03,440 --> 00:03:07,610 That list consists of one file right now, medals.html. 57 00:03:07,610 --> 00:03:11,150 The untracked files section looks much the same as before, 58 00:03:11,150 --> 00:03:14,710 except that now it only includes bronze.html. 59 00:03:14,710 --> 00:03:19,960 Our medals.html file has transitioned from modified to staged. 60 00:03:19,960 --> 00:03:23,240 Next, we need to commit our changes to the repository. 61 00:03:23,240 --> 00:03:27,740 We use the git commit command to commit our staged changes. 62 00:03:27,740 --> 00:03:30,410 You need to provide a message to go with every commit. 63 00:03:30,410 --> 00:03:33,090 A brief note explaining what the command does. 64 00:03:33,090 --> 00:03:35,580 We do this with the -m option, 65 00:03:35,580 --> 00:03:39,370 -m should be followed by a string in quotation marks. 66 00:03:39,370 --> 00:03:46,070 Since this commit adds medals.html, we'll use a message of, add main site page. 67 00:03:48,540 --> 00:03:52,080 These messages are one of many features that make git a great tool for 68 00:03:52,080 --> 00:03:53,370 collaborating. 69 00:03:53,370 --> 00:03:57,350 Messages are permanently attached to a commit in the repo history. 70 00:03:57,350 --> 00:04:00,080 If one of your collaborators sees this commit in the future and 71 00:04:00,080 --> 00:04:03,750 doesn't understand it, this message will help them figure it out. 72 00:04:03,750 --> 00:04:07,310 It might also help you figure out what your own commit does if it's been six 73 00:04:07,310 --> 00:04:08,700 months since you made it. 74 00:04:08,700 --> 00:04:11,820 Let's take a moment to talk about commit message style. 75 00:04:11,820 --> 00:04:12,820 As a rule of thumb, 76 00:04:12,820 --> 00:04:16,750 a commit message should complete the phrase, this commit will. 77 00:04:16,750 --> 00:04:21,640 This commit will add the main site page, this commit will remove sale description, 78 00:04:21,640 --> 00:04:24,510 this commit will add new products, etc. 79 00:04:24,510 --> 00:04:27,300 This isn't required, but if you form good habits now 80 00:04:27,300 --> 00:04:30,460 the other people working on your project will thank you later. 81 00:04:30,460 --> 00:04:34,200 Now if we press Enter to run the git commit command, we'll see an error. 82 00:04:34,200 --> 00:04:37,570 In addition to a commit message, git needs to know your name and 83 00:04:37,570 --> 00:04:40,450 email address so it can attach them to the commit. 84 00:04:40,450 --> 00:04:43,280 This is another of git's collaboration features. 85 00:04:43,280 --> 00:04:47,080 It allows other people working on the project to contact you if they need to ask 86 00:04:47,080 --> 00:04:48,480 you about the commit. 87 00:04:48,480 --> 00:04:52,850 The name and email address are permanently stored as part of gits configuration. 88 00:04:52,850 --> 00:04:57,910 The git config command allows you to add and edit values in that configuration. 89 00:04:57,910 --> 00:05:03,090 Let's run the commands it suggests, git config space --global. 90 00:05:04,609 --> 00:05:09,460 Space user.email, that's the name of the option you're going to set. 91 00:05:09,460 --> 00:05:15,013 And then we need the value of that option in quotes, 92 00:05:15,013 --> 00:05:22,257 I'm gonna use my email of jay.mcgavern@teamtreehouse.com. 93 00:05:22,257 --> 00:05:24,340 Obviously you should be using your own email there. 94 00:05:25,700 --> 00:05:28,630 And then we'll run git config again to set the user name. 95 00:05:28,630 --> 00:05:34,171 So git config -- global again, user.name, 96 00:05:34,171 --> 00:05:38,800 and again we need the value in quotes. 97 00:05:38,800 --> 00:05:45,185 I'll use my name, "Jay McGavren", and press Enter to set the config. 98 00:05:46,220 --> 00:05:48,230 Now with a user name and email stored, 99 00:05:48,230 --> 00:05:50,830 I should be able to make a commit successfully. 100 00:05:50,830 --> 00:05:53,420 Most shells keep a history of commands you've entered. 101 00:05:53,420 --> 00:05:56,330 You can hit the up arrow key to bring up previous commands so 102 00:05:56,330 --> 00:05:58,160 you don't have to type them again. 103 00:05:58,160 --> 00:06:01,860 This isn't a feature of git, it's a feature of the shell itself. 104 00:06:01,860 --> 00:06:05,110 I'm going to hit the up arrow until the git commit command is active and 105 00:06:05,110 --> 00:06:07,090 then hit Enter to rerun it. 106 00:06:07,090 --> 00:06:09,970 This time we'll see message confirming the commit. 107 00:06:09,970 --> 00:06:14,530 By the way if you leave of the -m command line option when running git commit, 108 00:06:14,530 --> 00:06:18,720 git will launch a text editor so you can enter a commit message. 109 00:06:18,720 --> 00:06:21,890 Here I reset my workspace to the state that was in before the commit and 110 00:06:21,890 --> 00:06:24,250 now I'll run git commit by itself. 111 00:06:24,250 --> 00:06:27,100 Git will launch a text editor with an empty line at the top for 112 00:06:27,100 --> 00:06:31,350 you to type your commit message and some comments below explaining what to do. 113 00:06:31,350 --> 00:06:34,960 We've set up workspaces to use a text editor called Nano for 114 00:06:34,960 --> 00:06:36,880 editing git messages. 115 00:06:36,880 --> 00:06:43,199 With Nano, you just type your message normally, Add main site page. 116 00:06:44,950 --> 00:06:48,040 Next we need to save the file and exit the editor. 117 00:06:48,040 --> 00:06:51,700 Nano displays commands you can run across the bottom of the screen. 118 00:06:51,700 --> 00:06:55,280 The caret character, the one that looks like an arrow pointing upwards stands for 119 00:06:55,280 --> 00:06:56,360 the Ctrl key. 120 00:06:56,360 --> 00:07:01,340 So Ctrl+G will get help, Ctrl+O will write out the file, etc. 121 00:07:01,340 --> 00:07:04,150 We want to press Ctrl+O to write out the file. 122 00:07:04,150 --> 00:07:06,350 The file name has already been set up for us, so 123 00:07:06,350 --> 00:07:09,920 we should just press Enter to accept the existing file name. 124 00:07:09,920 --> 00:07:13,180 Then we can press Ctrl+X to exit the editor. 125 00:07:13,180 --> 00:07:17,290 When the editor exits, git will read your commit message from the saved file and 126 00:07:17,290 --> 00:07:19,220 use that to complete the commit. 127 00:07:19,220 --> 00:07:23,860 This gives us the same result as if we had used the -m option to git commit. 128 00:07:23,860 --> 00:07:29,620 One more quick note, on other systems git uses an editor named Vi by default. 129 00:07:29,620 --> 00:07:33,790 Vi can be difficult to even exit out of if you don't know what you're doing. 130 00:07:33,790 --> 00:07:35,330 If you try to make a git commit and 131 00:07:35,330 --> 00:07:38,858 find yourself stuck in an editor, see the teacher's notes for some help. 132 00:07:38,858 --> 00:07:43,626 Committing our medals.html cause its status to transition from 133 00:07:43,626 --> 00:07:45,630 staged to committed. 134 00:07:45,630 --> 00:07:47,340 Now the cycle can repeat. 135 00:07:47,340 --> 00:07:50,690 It is ready for us to modify additional files, stage them, and 136 00:07:50,690 --> 00:07:52,290 add them as another commit. 137 00:07:52,290 --> 00:07:53,580 Once a commit is complete, 138 00:07:53,580 --> 00:07:56,520 it's a permanent part of the repository's history. 139 00:07:56,520 --> 00:08:00,430 You can review that history with the git log command. 140 00:08:00,430 --> 00:08:05,480 For each commit, git log shows the author name and email, the date and 141 00:08:05,480 --> 00:08:08,580 time of the commit, and the commit message. 142 00:08:10,010 --> 00:08:12,918 If you want, you can add the -p option to git log. 143 00:08:12,918 --> 00:08:18,810 Git log -p, then git log will show the lines that were added in each file. 144 00:08:18,810 --> 00:08:22,050 Anytime a git command shows output that's too long for the screen, 145 00:08:22,050 --> 00:08:25,130 it will show that output using a pager program. 146 00:08:25,130 --> 00:08:27,490 Don't worry about the details of that program. 147 00:08:27,490 --> 00:08:29,690 All you need to know is that you can use the up and 148 00:08:29,690 --> 00:08:32,250 down arrow keys to scroll through the output, and 149 00:08:32,250 --> 00:08:36,190 press the Q key when you're ready to quit back to the shell prompt. 150 00:08:36,190 --> 00:08:39,370 Those are the basics of the git add and git commit commands. 151 00:08:39,370 --> 00:08:42,080 You can do almost anything with what we've already shown you. 152 00:08:42,080 --> 00:08:44,299 But there are additional options with git add and 153 00:08:44,299 --> 00:08:48,630 git commit that can make it easier to work with large numbers of files. 154 00:08:48,630 --> 00:08:50,980 See the teacher's note if you'd like to learn more. 155 00:08:50,980 --> 00:08:55,270 Congratulations, you've just committed to your first file to your git repository. 156 00:08:55,270 --> 00:08:56,620 The process is similar for 157 00:08:56,620 --> 00:08:59,920 committing changes to existing files as we'll see in the next video.