GIT - 4.5 Start with an existing project

This lesson shows how to add source control to an existing project that does not yet have a Git repository. The starting point is a terminal in our home directory with a projects folder where all our Git projects live. In the previous lesson we created a repository inside a test subfolder and then deleted it, so the folder is back to a plain directory without version control. We jump into it:

cd test

Inside, we have a coucou.html file. To confirm there is no repository yet, we list every file including hidden ones with ls -al: there is no .git directory anywhere.

Initializing the repository with git init

To put this folder under version control with Git, we use the git init command. The trailing dot is optional and refers to the current folder:

git init .

Git replies that it initialized an empty repository in the current folder. The shell prompt now shows the branch name master in front of the cursor, which is Git's way of telling us we are in a repository. A new ls -al confirms that a hidden .git directory has appeared next to coucou.html.

Running git status shows that coucou.html is currently untracked. That makes sense: even though the file existed before, the repository we just created is brand new and has never seen it. Since we destroyed the previous repository in the prior lesson, this is a fresh repository with no knowledge of the old commits or of the old coucou.html history. To bring the file under version control, we would now run git add coucou.html followed by git commit. This is the standard way to bootstrap version control on any existing project, whether it has one file or hundreds.

Summary

This lesson teaches how to initialize Git version control in an existing project folder that has no version control yet. You learn to use the `git init` command to create a new Git repository, which generates the hidden `.git` directory containing Git's metadata. The lesson also demonstrates verifying successful initialization by checking the command prompt (which now shows the branch name "master") and using `git status` to view untracked files.

Key points

  • Use `git init` to initialize a new Git repository in any existing project folder
  • Git creates a hidden `.git` directory to store all version control metadata
  • After initialization, the command prompt displays the current branch name (e.g., 'master')
  • All existing files in the folder are initially untracked until you explicitly add them
  • Use `git status` to verify initialization and see which files Git is not yet tracking

FAQ

How do you put an existing project under Git version control?

Navigate to your project folder in the terminal and run `git init`. This creates the `.git` directory and initializes the folder as a Git repository ready for version tracking.

Why are my existing project files marked as untracked after running `git init`?

When you initialize a repository with `git init`, Git starts fresh with no history of existing files. All current files are considered untracked until you explicitly add them to the staging area with `git add` and create a commit.

How can you verify that Git was initialized correctly?

Run `git status` to check the repository state and list untracked files, or look for the `.git` directory in your project folder. You should also see the branch name (like 'master') appear in your command prompt.