1 00:00:00,000 --> 00:00:04,536 [MUSIC] 2 00:00:04,536 --> 00:00:09,650 Previously, we've shown you how to set up a Git repository and commit files to it. 3 00:00:09,650 --> 00:00:12,550 But with just the commands you know now, when you commit a file, 4 00:00:12,550 --> 00:00:14,540 well you're committed. 5 00:00:14,540 --> 00:00:16,120 If you decide you need to delete or 6 00:00:16,120 --> 00:00:18,890 move a file, you can do it in your working directory. 7 00:00:18,890 --> 00:00:21,160 But you can't do it in the Git repo. 8 00:00:21,160 --> 00:00:24,430 If you staged the wrong file, we haven't shown you how to unstage it. 9 00:00:25,780 --> 00:00:29,210 In these next few videos, we'll fix all that and more. 10 00:00:29,210 --> 00:00:30,390 We'll show you how to delete or 11 00:00:30,390 --> 00:00:34,110 move files in the get repo as well as your working directory. 12 00:00:34,110 --> 00:00:37,690 You'll learn how to unstage files that you've accidentally staged. 13 00:00:37,690 --> 00:00:40,850 We'll help you reset a file's contents back to the way they looked 14 00:00:40,850 --> 00:00:42,510 after your last commit. 15 00:00:42,510 --> 00:00:46,270 We'll show you how to bring back a file that you've accidentally deleted. 16 00:00:46,270 --> 00:00:49,450 And if you decide you don't like the changes you made in the commit, 17 00:00:49,450 --> 00:00:51,940 we'll show you how to undo that commit. 18 00:00:51,940 --> 00:00:53,650 This is the good stuff. 19 00:00:53,650 --> 00:00:57,170 In these lessons you'll begin to see the real power of Git. 20 00:00:57,170 --> 00:00:57,970 Ready? 21 00:00:57,970 --> 00:00:58,790 Let's get started. 22 00:00:59,940 --> 00:01:05,110 We've added the tin.html file showcasing the store's new tin medals. 23 00:01:05,110 --> 00:01:10,690 If we run git status, we'll see the file is untracked. 24 00:01:10,690 --> 00:01:11,610 So let's add it. 25 00:01:11,610 --> 00:01:16,919 Git add tin.html, and then we'll commit it, 26 00:01:16,919 --> 00:01:20,910 git commit -m "Add tin medals". 27 00:01:23,280 --> 00:01:26,922 And we close our editor, satisfied with a job well done. 28 00:01:26,922 --> 00:01:30,350 But suppose we later learn that the customers weren't too pleased with the new 29 00:01:30,350 --> 00:01:33,590 tin medals, and we've decided to drop the product. 30 00:01:33,590 --> 00:01:35,150 If we run the ls command, 31 00:01:35,150 --> 00:01:39,650 it will show the tin.html file here in our project directory. 32 00:01:39,650 --> 00:01:44,040 We can delete the file from our terminal using the rm command, which stands for 33 00:01:44,040 --> 00:01:48,360 remove, rm tin.html. 34 00:01:48,360 --> 00:01:51,790 Just like ls, the rm command isn't part of Git, but 35 00:01:51,790 --> 00:01:56,730 it is standard on all Unix-like systems, so it's worth learning how to use. 36 00:01:56,730 --> 00:01:59,410 See the teacher's notes if you want more info. 37 00:01:59,410 --> 00:02:03,950 Running ls again will show that the tin.html file is gone. 38 00:02:03,950 --> 00:02:07,730 But the tin.html file is being tracked by Git. 39 00:02:07,730 --> 00:02:10,270 Will deleting the file from our working directory 40 00:02:10,270 --> 00:02:12,750 also delete it from the repository? 41 00:02:12,750 --> 00:02:16,420 Let's try our trusty git status command to find out. 42 00:02:16,420 --> 00:02:19,810 It shows that the tin.html file has been deleted, but 43 00:02:19,810 --> 00:02:23,590 it shows that in the changes not staged for commit section. 44 00:02:23,590 --> 00:02:25,290 Why is that? 45 00:02:25,290 --> 00:02:30,210 In Git, commits don't just add or modify files, they can delete them as well. 46 00:02:30,210 --> 00:02:35,280 This is important because repositories can be shared across multiple computers. 47 00:02:35,280 --> 00:02:39,120 When you decide to remove a file from your project, you don't want that file to be 48 00:02:39,120 --> 00:02:42,250 left sitting in your coworker's copy of the project. 49 00:02:42,250 --> 00:02:45,840 You want it to be deleted from your coworker's machine, too. 50 00:02:45,840 --> 00:02:49,860 Making file deletions part of a commit ensures that the copies of your Git 51 00:02:49,860 --> 00:02:53,460 repository will have those deletions applied, too. 52 00:02:53,460 --> 00:02:57,260 And by the way, in case you're worried about others using this feature to delete 53 00:02:57,260 --> 00:03:01,120 files from your machine, there are ways to undo the deletion of a file, 54 00:03:01,120 --> 00:03:03,680 even after you've committed the deletion. 55 00:03:03,680 --> 00:03:06,360 We'll see one way in an upcoming video. 56 00:03:06,360 --> 00:03:10,890 So how can we make the deletion of tin.html part of a commit? 57 00:03:10,890 --> 00:03:14,260 The key is to use the git rm command. 58 00:03:14,260 --> 00:03:17,770 Git rm is set up to work much like the plain rm command. 59 00:03:17,770 --> 00:03:21,362 So it's much like taking our previous command and 60 00:03:21,362 --> 00:03:25,392 sticking the word git in front of it, git rm tin.html. 61 00:03:29,067 --> 00:03:30,992 Let's run git status again. 62 00:03:32,880 --> 00:03:37,441 And we'll see the deletion of tin.html is listed in the changes to be 63 00:03:37,441 --> 00:03:39,800 committed section now. 64 00:03:39,800 --> 00:03:43,093 Next, we can commit as usual, 65 00:03:43,093 --> 00:03:47,789 git commit -m, "Remove tin medals". 66 00:03:52,843 --> 00:03:54,943 Now we can run git status again, and 67 00:03:54,943 --> 00:03:59,150 this time it will show that the working directory is clean. 68 00:03:59,150 --> 00:04:03,950 And if we run ls, we'll see that the tin.html file is still gone. 69 00:04:03,950 --> 00:04:09,680 By the way, we didn't need to run rm tin.html as a separate step. 70 00:04:09,680 --> 00:04:14,190 Git rm will remove the file from the working directory for you, if it exists.