Creating a Repository
Overview
Teaching: 10 min
Exercises: 0 minQuestions
Where does Git store information?
Objectives
Create a local Git repository.
Once Git is configured, we can start using it. Let’s create a directory for our work and then move into that directory:
$ mkdir digits-classifier
$ cd digits-classifier
Then we tell Git to make digits-classifier a repository—a place where
Git can store versions of our files:
$ git init
If we use ls to show the directory’s contents,
it appears that nothing has changed:
$ ls
But if we add the -a flag to show everything,
we can see that Git has created a hidden directory within digits-classifier called .git:
$ ls -a
. .. .git
Git stores information about the project in this special sub-directory. If we ever delete it, we will lose the project’s history.
We can check that everything is set up correctly by asking Git to tell us the status of our project:
$ git status
If you are using a different version of git than I am, then then the exact wording of the output might be slightly different.
On branch master
Initial commit
nothing to commit (create/copy files and use "git add" to track)
Places to Create Git Repositories
Bob starts a new project to work on a Support Vector Machine,
svm, for hisdigits-classifierproject. Despite Alice’s concerns, he enters the following sequence of commands to create one Git repository inside another:$ cd # return to home directory $ mkdir digits-classifier # make a new directory digits-classifier $ cd digits-classifier # go into digits-classifier $ git init # make the digits-classifier directory a Git repository $ mkdir svm # make a sub-directory digits-classifier/svm $ cd svm # go into digits-classifier/svm $ git init # make the svm sub-directory a Git repositoryWhy is it a bad idea to do this? (Notice here that the
digits-classifierproject is now also tracking the entiresvmrepository.) How can Bob undo his lastgit init?Solution
Git repositories can interfere with each other if they are “nested” in the directory of another: the outer repository will try to version-control the inner repository. Therefore, it’s best to create each new Git repository in a separate directory.
To recover from this little mistake, Bob can just remove the
.gitfolder in the svm subdirectory. To do so he can run the following command from inside thedigits-classifierdirectory:$ rm -rf svm/.gitBut be careful! Running this command in the wrong directory, will remove the entire git-history of a project you might wanted to keep. Therefore, always check your current directory using the command
pwd.To be sure that there is no conflicting repository in the directory, check the output of
git status. If it looks like the following, you are good to go to create a new repository as shown above:$ git statusfatal: Not a git repository (or any of the parent directories): .gitNote that we can track files in directories within a Git:
$ touch svm/classify.py # create svm files $ cd .. # return to digits-classifier directory $ ls svm # list contents of the svm directory $ git add svm/* # add all contents of digits-classifier/svm $ git status # show svm files in staging area $ git commit -m "add svm files" # commit digits-classifier/svm to digits-classifier Git repositorySimilarly, we can ignore (as discussed later) entire directories, such as the
svmdirectory:$ nano .gitignore # open the .gitignore file in the text editor to add the svm directory $ cat .gitignore # if you run cat afterwards, it should look like this:svm
Key Points
git initinitializes a repository.