Initializing a Repository4:13 with Jay McGavren
Learn what "git init" does and how it works.
Our project for this course will be a simplified version of a web site for a company that makes gold, silver, and bronze medals to award at sporting events. If you launch a Treehouse Workspace using the button on this video's page, you'll see a
medals directory (directory is a fancy word for folder) with two files in it:
<!DOCTYPE html> <html> <body> <p>We have a fine selection of medals.</p> </body> </html>
<h1>Check out our bronze medals!</h1> <p>Medallion: $20</p> <p>Ribbon: $10</p>
You don't need to worry too much about the particular content of these files; if you really want to know more about HTML, visit our Introduction to HTML and CSS course.
You can create a repository within a project directory on your computer. That's what we'll do here.
We need to change into our project directory so we can turn it into a Git repo. If we type
ls and press Enter, it will run the
ls command. This lists files and directories within the current directory. By the way, the
ls command isn't part of Git, but it's a standard command on all Unix-like systems. It's worth your time to learn how to use it. We have more coverage of the
ls command in this video from our Introduction to the Terminal course.
- We can see our project directory,
medals, in the output of ls.
- To get into the
cdcommand is another important command that works on Linux, Mac and Windows systems.
cdis also covered in our Introduction to the Terminal course; you can learn more about it in this video.
- Now that we're in the project directory, we need to initialize the new repository. We'll do this with the
- Like all the Git commands we'll be using in this course, it starts with the name of the
gitexecutable, followed by a space. Then, we type the name of the subcommand we want to run, like
commitor, in this case,
- Press Enter to run it. Git will initialize a repository in the current directory, by creating a new directory named
- You won't see the
.gitdirectory at first. But
lshas a special command line option that will cause it to show all files, even hidden ones.
- If we hit Enter to run it, we can see the
.gitdirectory in the output of
Our project for this course will be a simplified version of a website for 0:00 a company that makes gold, silver, and bronze medals to award at sporting events. 0:04 If you launch a Treehouse workspace using the button on this videos page, you'll see 0:08 a medals directory, directory's a fancy world for folder, with two files in it. 0:12 A medals.html file that holds the main page content, and 0:17 a bronze.html file that holds information on the store's bronze medals. 0:20 You don't need to worry too much about the particular content of these files. 0:25 If you really want to know more about html, you can see the teacher's notes for 0:29 more info. 0:32 The important part is the get commands we'll be using to track their contents. 0:34 Because we'll be making many of our changes using the console, 0:38 the sidebar may not display what the directory actually holds all the time. 0:41 You can right-click on the sidebar and choose to refresh the list, but 0:44 I don't want to do that over and over. 0:49 So I'm going to the View menu and choose Hide Sidebar. 0:51 When I need the sidebar later, I'll use the Show Sidebar menu to reveal it again. 0:55 For the most part, I'll be using simple commands that let us look at 0:59 the contents of the directory from the console. 1:02 There's two ways to get a git repository onto your computer. 1:05 One is to clone, or copy, a repo from another machine. 1:08 We'll look at this later in the course. 1:12 The other is to create a repository within a project directory on your computer. 1:14 That's what we'll do here. 1:18 Right now we're in the workspace directory which holds all of the files for 1:19 this workspace. 1:22 We need to change into our project directory so 1:24 we can turn it into a Git repo. 1:26 So I'll click down in the console to activate it. 1:29 And now if we type ls and press Enter, it will run the ls command. 1:32 This lists files and directories within the current directory. 1:36 By the way, the ls command isn't part of get, but it's a standard command on all 1:40 Unix-like systems, such as Mac, Linux, and the subsystem for Linux on Windows. 1:44 It's worth your time to learn how to use it. 1:49 We'll have more info in the teacher's notes. 1:51 We can see our project directory, medals, in the output of ls. 1:54 To get into the medals directory, we'll use the cd or change directory command. 1:58 Then we tap a space, 2:03 followed by the name of the directory we want to change into, medals. 2:04 We can see that the directory name in our shell prompts has changed to 2:09 workspace/medals. 2:12 And if we run again, we will see the files for our project. 2:15 Now that we are in the project directory, we need to initialize the new repository. 2:18 We will do this with the git init command. 2:22 Like all the git commands we'll be using in this course, 2:26 it starts with the name of the git executable followed by a space. 2:28 Then we type the name of the sub command we want to run like add or commit. 2:32 Or in this case, init. 2:36 That's the whole command, so press Enter to run it. 2:38 It will initialize a repository in the current directory 2:41 by creating a new directory named doc git within it. 2:43 Or at least it's supposed to. 2:47 But if we run ls again, we don't see a .git directory. 2:49 This is because the file name begins with a dot. 2:53 On Unix-like systems, 2:56 such as Linux, a dot at the start of a filename means that the file is hidden. 2:57 Let's show the sidebar and refresh it. 3:01 That Git directory won't appear there either. 3:06 But ls has a special command line option that will cause it to show all files, 3:08 even hidden ones. 3:12 Options to other commands work just like the options for the git executable. 3:14 We type ls, a space, and then the option we want, -a. 3:18 The -a stands for all, as in list all files. 3:21 If we hit Enter to run it, we can see the .git directory in the output of ls. 3:26 But why would git create this directory only to hide it from us? 3:31 The reason is that you generally don't want to directly interact with the .git 3:34 directory. 3:38 It stores all of your repository's history in a special format 3:39 that only the git command really knows how to update safely. 3:42 You do not want to modify the files in the .git directory yourself. 3:46 You could corrupt your repository's history, 3:50 meaning you would lose everything that hadn't been cloned to other computers. 3:52 Don't worry, that isn't a common problem. 3:56 It's rare for people to modify the contents of .git directly, 3:58 either accidentally or on purpose. 4:01 It just explains why the directory is hidden. 4:03 But it's much safer to modify the .git directory 4:06 indirectly by making commits through the git command. 4:08 We'll do just that in the next video. 4:11
You need to sign up for Treehouse in order to download course files.Sign up