Initializing a Repository (git init)
Every great project at CodeHarborHub starts with a single step: telling Git to start watching your files. This process is called Initialization.
The git init command transforms an ordinary directory (folder) into a GitHub-ready Git Repository.
Think of git init as the "Start Engine" button for your project. It revs up Git's engine and gets it ready to track every change you make to your code.
What does git init actually do?โ
When you run this command, Git creates a hidden folder named .git inside your project. This folder is the "Brain" of your projectโit stores every version, every branch, and every configuration for that specific repository.
The "Before and After"โ
- Before
git init: Your folder is just a collection of files. If you delete a line of code, itโs gone forever. - After
git init: Your folder is a Repository. Git is now "listening" for changes, ready to save snapshots of your work.
How to Use Itโ
Follow these steps to start your first repository:
- Open your Terminal (or Git Bash on Windows).
- Navigate to your project folder:
cd Desktop/my-web-project - Run the Initialization:
git init
Expected Output:
Initialized empty Git repository in C:/Users/Ajay/Desktop/my-web-project/.git/
The Repository Lifecycleโ
Common Use Casesโ
| Scenario | What to do? |
|---|---|
| New Project | Create a folder โ git init. |
| Existing Code | Go to the existing folder โ git init. |
| Mistake? | If you initialized the wrong folder, simply delete the hidden .git folder to stop tracking. |
Important Rules for Beginnersโ
- Don't Touch the
.gitFolder: You will see this folder if you have "Hidden Items" turned on. Never delete or modify files inside it manually, or you might lose your entire project history! - Don't Nest Repositories: Avoid running
git initinside a folder that is already inside another Git repository. One project = One.gitfolder. - The "Main" Branch: By default, Git creates a starting branch. In modern development, this is usually called
main(though older versions might call itmaster). You can rename it later, but for now, just know that this is your "Main" branch where your stable code will live.
You only need to run git init once per project. Once the .git folder exists, Git will continue to track that folder forever until you delete it.