Sub topic 6
What git clone creates on disk
This chapter assumes you have Git installed, a GitHub account, and a terminal. It also assumes you know what a commit and a branch are. Here you will look at what git clone leaves on your machine, so that later chapters on branches, shallow clones, and fixing errors make sense.
One command, four results
A clone is not a download of the latest files. git clone does these things in order:
- Creates a new directory.
- Copies the repository's full history into a hidden
.gitdirectory inside it. - Adds a remote named
originthat points to the URL you cloned from. - Checks out the remote's default branch, which writes the files of its latest commit into the directory.
A ZIP archive from GitHub gives you only step 4, with no history and no link back to GitHub.
The working tree and the .git directory
The working tree is the set of ordinary files you see and edit. The .git directory is the repository itself. It holds every commit, every file version, and your configuration. If you delete .git, the folder becomes plain files with no history.
Run a clone and look inside:
git clone https://github.com/octocat/Hello-World.git
cd Hello-World
ls -aThe listing includes .git next to the project files. The -a flag is needed because names starting with a dot are hidden by default.
The origin remote
A remote is a named URL for another copy of the repository. Git names the one you cloned from origin. Check it with:
git remote -vThe output lists origin twice, once for fetch and once for push. The URL matches the form you cloned with, HTTPS or SSH. Later git fetch and git pull use this remote by default.
Branches: local and remote-tracking
After a clone you have one local branch, the remote's default branch (often main). You also have remote-tracking branches, which are read-only bookmarks of where each branch on origin was at your last fetch. They are named origin/<branch>.
git branch -aThe output shows the local branch with an asterisk, followed by entries such as remotes/origin/HEAD and remotes/origin/main. Other branches on GitHub appear only as remotes/origin/... entries. You have their commits, but no local branch for them yet.
Inspecting the history
Because the full history was copied, you can read it without a network connection:
git log --oneline -n 5This shows the five most recent commits. Everything you inspect here is read from .git.
The clone is independent
Your clone is a complete repository. Commits you make stay on your machine until you push them. Commits others push to GitHub do not appear until you fetch. This is why the later chapter on keeping a clone current exists.
Common mistakes
- Running Git commands outside the clone directory. You get
fatal: not a git repositoryuntil youcdinto it. - Deleting
.gitto "clean up". This destroys the history and the remote link. - Assuming other branches are missing. They are present as
origin/...and become local branches when you check them out.
Exercise
You clone a repository and run git branch -a. You see * main and remotes/origin/main, remotes/origin/dev. Does your machine have the commits of the dev branch, and do you have a local dev branch?
Show answer
You have the commits of dev, stored in .git and visible through the remote-tracking branch origin/dev. You do not yet have a local dev branch. Running git switch dev creates one that tracks origin/dev.
Exercise
A teammate sends you a folder copied from a clone, but without its hidden files. git status fails with "not a git repository". Why?
Show answer
The hidden .git directory holds the repository data, including history and the origin remote. Without it, the folder contains only a working tree and Git has nothing to read.