Initialize an existing project as a Git repository.
- [Instructor] In this movie, we're going to use Git to start tracking changes inside our Explore California project. Our project already has files in it 'cause it's an existing project, but Git is not being used to track them right now. Git has no knowledge about them and there's no Git repository established to keep track of the files or the changes. To initialize Git, we need to navigate to the project directory in the command line. So right now, you can see I'm still inside that first Git project. So let's use CD to back up a directory. I can use LS to see the different directories that are in there or DIR if you're on Windows. Then I'll use CD to go into the Explore California Project instead. I'm going to use LS-LA to show a listing of all the files that are there, including the dot files. You can ignore this DS_Store file, that's something that the Macintosh uses to keep track of positions in the finder. Notice that there is no .Git directory in this project folder. That's how we know the Git is not tracking this project, because there is no Git directory at the root of the project. And if we were to type Git Status, Git comes up and says "Not a git repository" or any of the parent directories. That's important because we might be further down in the tree. Right? So there's not a Git directory that's right in the current directory, but maybe we're further down into the project. This would look up at all those parent projects and see "Is there a Git directory anywhere up above me?" It goes to parent directory to parent directory and so on, all the way to the top of the hard drive, looking for that Git directory. So, it's not a Git repository, we're not inside a nested directory of a Git repository. This is not being tracked by Git. So, we remember how to initialize a repository, right? We use the command "git init" then initializes an empty Git repository. Now if we do "LS-LA", you can see that directory ".git" has been added. Now if we type "git status" it comes up and it tells us about the untracked files. Because now there's a repository and it sees that there is a working directory, and the working directory has files which are not in the repository. Let's also try typing "git log". Notice that we get an error when we type "git log" right now. That's because, at the moment, the head is not pointed at anything. We don't have a commit yet. So the current branch master does not have any commits, there's nothing for it to show. So it says add everything in the current directory to the staging tree. Now if I type "git status" you'll see that there's a long list of files that it's planning on adding. So let's add them. "git commit -m" "git commit -m" and it's very common just to use "Initial Commit" for your very first commit when you have an existing project. It's not very descriptive, but it just let's us know we're adding all the project files at this point. We're not tracking any changes, we're just taking it in whatever state it is, and adding it to our repository. Now if I type "git log" we see that first commit, the head is pointing to ashod, 938c64 and so on. And we're ready to start tracking our changes. "git status" now comes up and says your working tree is clean, everything that's in the working directory is also in the staging tree, and in the repository. Now before we move on to making changes, I want to make sure it's clear that the Explore California repository, and the first Git project repository that we were working with before are completely separate. Remember, all the repository information is stored in that ".git" folder at the top level of those two directories. So the two are completely separate. You can have as many Git repositories as you want on your computer, and each one will track its changes separately. For me, for example, I develop projects for many different clients. I have a number of different Git repositories on my computer, and when I move into a project directory, I'm automatically working in that Git project and with that Git repository. If I change the directory for a different client briefly, I'm now working in a different Git repository with a different set of changes. Now that we've told Git that it should start tracking files, we've given it its first set of files that it should track. We're ready to start making edits to our project.
- Exploring the history of version control
- Installing Git on Mac, Windows, and Linux
- Initializing a repository
- Writing useful commit messages
- The Git three-tree architecture
- Tracking when files are added, edited, deleted, or moved
- Viewing change sets and comparing versions
- Undoing changes and retrieving previous versions
- Ignoring changes to select files