3. Setting Up A Repository¶
3.1. Initializing A New Repository¶
Before we can work with Git, we need to have a Git repository. The most obvious and simple way to get a Git repository is by initialising a new one.
Initialising a new repository is quite straight forward:
git init [DIRECTORY]
Hint
This command will create a new local Git repository in the desired directory path. In case you omit the directory path, the repository will be created in the current directory.
3.2. Cloning An Existing Repository¶
Most of the time you’ll come across existing projects with existing Git repositories. In this case you obviously don’t want to initialise a new Git repository, but use the existing one.
In Git, this is called clone
, as you clone the existing remote Git repository.
Cloning an existing Git repository is again quite straight forward:
git clone {REPOSITORY} [DIRECTORY]
Hint
This command will clone an existing Git repository to the desired directory path. In case you omit the directory path, the repository will be cloned to the current directory.
3.3. Git Directory¶
Git initialises and keeps track of changes by storing all Git metadata in a .git/
subdirectory.
When running git init
, Git will basically create this directory.
Hint
When initialising or cloning a Git repository, the destination directory must not be existing or empty.
3.4. Repository Remotes¶
Of course, local & remote repositories can be synchronised. In Git, this is achieved via Remotes. We’ll have a look at this later in the Remotes chapter.
3.5. Exercise¶
Note
After completing this chapter, you should be able to execute the My First Repository exercise.