To use Git for version control, you must first designate a project directory as a Git Repository. This setup is a one-time process for any new project.
1. Starting a New Repository (git init)
The git init command is used to initialize a new, empty Git repository in the current directory.
Execution
To start, you first need a project folder. You can create a new one and navigate into it using the terminal:
mkdir my-new-project
cd my-new-project
Next, run the initialization command inside that folder:
git init
The .git Directory
When you run git init, Git creates a hidden subdirectory named .git inside your project folder.
- This directory is the Repository itself.
- It contains all of Git’s tracking information, metadata, object history (snapshots), branch pointers, and configuration.
- Crucially, this folder must never be deleted if you want to preserve your project’s history.
Note: Your actual project files (e.g.,
index.html,style.css) are referred to as the Working Directory. The repository (.gitfolder) is separate from the files you edit.
2. Ignoring Files (.gitignore)
Most projects have files that should not be tracked by Git. These typically include:
- Compiled Code (e.g.,
/dist,/build). - Dependency Folders (e.g.,
/node_modules,/vendor). - Sensitive Data (configuration files containing passwords or keys).
- Log Files or Temporary Files.
To instruct Git to ignore these files and directories, you create a plain text file named .gitignore in the root of your project.
Execution Example: Creating .gitignore
Inside your project folder, create a file named .gitignore and add exclusion patterns:
# .gitignore content
# Ignore the entire node_modules folder
node_modules/
# Ignore all files ending with .log anywhere in the project
*.log
# Ignore configuration files in the root folder
config.ini
# Ignore the "temp" folder only in the root
/temp/
Benefit: Once a file or folder is listed in
.gitignore, Git will ignore it when checking the project status, preventing you from accidentally committing unnecessary or sensitive data.
3. Repository Status (git status)
The git status command is the most frequently used command. It provides a summary of the current state of your repository.
What git status shows:
- The current branch you are on.
- Changes to be committed (files in the Staging Area).
- Changes not staged for commit (modified files in the Working Directory).
- Untracked files (new files in the Working Directory that Git doesn’t know about yet).
Execution
git status
