Creating a Repository
Overview
Teaching: 10 min
Exercises: 0 minQuestions
Where does Git store information?
Objectives
Create a local Git repository.
Describe the purpose of the
.git
directory.
Once Git is configured, we can start using it.
The usage we will demonstrate is making a repository of shell scripts.
First, let’s create a directory in Desktop
folder for our work and then move into that directory:
$ cd ~/Desktop
$ mkdir shell-scripts
$ cd shell-scripts
Then we tell Git to make shell-scripts
a repository
– a place where Git can store versions of our files:
$ git init
It is important to note that git init
will create a repository that
includes subdirectories and their files—there is no need to create
separate repositories nested within the shell-scripts
repository, whether
subdirectories are present from the beginning or added later. Also, note
that the creation of the shell-scripts
directory and its initialization as a
repository are completely separate processes.
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 shell-scripts
called .git
:
$ ls -a
. .. .git
Git uses this special subdirectory to store all the information about the project,
including all files and sub-directories located within the project’s directory.
If we ever delete the .git
subdirectory,
we will lose the project’s history.
Next, we will change the default branch to be called main
.
This might be the default branch depending on your settings and version
of git.
See the setup episode for more information on this change.
git checkout -b main
Switched to a new branch 'main'
We can check that everything is set up correctly by asking Git to tell us the status of our project:
$ git status
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
If you are using a different version of git
, the exact
wording of the output might be slightly different.
Places to Create Git Repositories
To track different types of scripts, we will first create a
user-input
project inside ofshell-scripts
project with the following sequence of commands:$ cd ~/Desktop # return to Desktop directory $ cd shell-scripts # go into shell-scripts directory, which is already a Git repository $ ls -a # ensure the .git subdirectory is still present in the shell-scripts directory $ mkdir user-input # make a subdirectory shell-scripts/user-input $ cd user-input # go into user-input subdirectory $ git init # make the user-input subdirectory a Git repository $ ls -a # ensure the .git subdirectory is present indicating we have created a new Git repository
Is the
git init
command, run inside theuser-input
subdirectory, required for tracking files stored in theuser-input
subdirectory?Solution
Correcting
git init
MistakesNow we know how a nested repository is redundant and may cause confusion down the road. Say we would like to remove the nested repository. How can we undo our last
git init
in theuser-input
subdirectory?Solution – USE WITH CAUTION!
Background
Solution
Key Points
git init
initializes a repository.Git stores all of its repository data in the
.git
directory.