Sub topic 5
Choosing the target directory
This chapter assumes you have Git installed, a terminal open, and a clone URL for a repository. It also assumes you know what a branch and a commit are. You will learn where git clone puts the new repository and how to control that location.
The default behavior
By default, git clone creates a new directory in your current working directory. Git derives the directory name from the last part of the URL and removes the .git suffix.
git clone https://github.com/octocat/Hello-World.gitThis creates ./Hello-World. The SSH URL [email protected]:octocat/Hello-World.git produces the same name.
Because the result depends on where you run the command, check your location first with pwd. Cloning from your home directory scatters repositories there. Many developers keep a dedicated folder such as ~/projects and run every clone from inside it.
Naming the directory yourself
Add a second argument after the URL to choose the directory name.
git clone https://github.com/octocat/Hello-World.git my-projectGit creates ./my-project and puts the repository inside. The origin remote still points to the same GitHub URL. Only the local folder name changes.
You can also give a path. Git creates any missing parent directories.
git clone https://github.com/octocat/Hello-World.git ~/projects/demo/helloThis is useful when you need two copies of one repository, for example to compare two branches side by side. Give each clone a different name.
git clone https://github.com/octocat/Hello-World.git hello-a
git clone https://github.com/octocat/Hello-World.git hello-bCloning into the current directory
Pass . as the target to clone into the directory you are already in.
mkdir my-project
cd my-project
git clone https://github.com/octocat/Hello-World.git .Git accepts an existing directory only if it is empty. If it contains any files, the clone stops and nothing is changed.
Common errors
The most common failure is a target that already exists and has content:
fatal: destination path 'Hello-World' already exists and is not an empty directory.This usually means you already cloned the repository, or an earlier attempt left files behind. You have three options:
- Use the existing clone. Run
cd Hello-Worldandgit statusto see its state. - Choose a different name, such as
git clone <url> Hello-World-2. - Remove the old directory yourself, only if you are sure nothing in it is worth keeping.
Git never overwrites or merges into a non-empty directory. This protects any local work you have there.
A second problem is spaces in paths. Quote the path so the shell passes it as a single argument.
git clone https://github.com/octocat/Hello-World.git "my projects/hello"Moving a clone afterward
A clone is a self-contained directory. You can rename or move it with mv after cloning, and Git keeps working. The repository's history and remote settings live inside the .git folder, not in the folder name or location.
mv Hello-World ~/projects/hello-world
cd ~/projects/hello-world
git statusExercise
You run git clone https://github.com/octocat/Hello-World.git twice in the same folder. What happens on the second run, and how would you get a second, separate copy?
Show answer
The second run fails with "destination path 'Hello-World' already exists and is not an empty directory", and nothing is changed. To get a second copy, pass a different target name, for example git clone https://github.com/octocat/Hello-World.git Hello-World-2.
Exercise
What does the command git clone <url> . do, and when does it fail?
Show answer
It clones the repository directly into the current directory instead of creating a new subdirectory. It fails if the current directory is not empty.