You understand what Git is and why it matters. Now let's get your hands dirty and actually set up your first repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.. By the end of this lesson, you'll have a fully functional Git repo ready for your code.
Turning a folder into a repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.
Here's the beautiful thing about Git: it doesn't care what kind of project you have. A Python script, a React app, a novel written in Markdown, Git handles them all the same way.
To start tracking any folder, you just run one command:
# Navigate to your project folder
cd my-awesome-project
# Initialize Git
git initYou'll see confirmation like this:
Initialized empty Git repository in /home/you/my-awesome-project/.git/That's it. Your folder is now a Git repository.
What just happened?
Git created a hidden folder called .git in your project directory. This isn't just a simple folder, it's a complete database containing:
- Every commitWhat is commit?A permanent snapshot of your staged changes saved in Git's history, identified by a unique hash and accompanied by a message describing what changed. you've ever made (or will make)
- All branch information and configuration
- The staging areaWhat is staging area?A holding zone in Git where you place changes you want to include in your next commit, letting you select exactly which modifications to save.'s current state
- Compressed snapshots of your files
.git folder: You might be tempted to peek inside or even delete it if you're cleaning up space. Don't. This folder IS your repository. Delete it, and you delete your entire project history. If you need to stop using Git on a project, just stop running Git commands, leave that folder alone.Verifying everything worked
How do you know Git is actually watching your folder? Run the status command:
git statusIf Git is initialized, you'll see something like:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)This tells you three important things:
- You're on the default branch (usually called
main) - You haven't made any commits yet
- Git is ready and waiting for you to add files
If you see "fatal: not a git repositoryWhat is repository?A project folder tracked by Git that stores your files along with the complete history of every change, inside a hidden .git directory.", Git isn't initialized. Run git init again.
Setting up your first .gitignore
Before you start adding files, there's one crucial step every project needs: creating a .gitignore file. This tells Git which files to completely ignore.
Why you need this
Not everything belongs in version control. Some files are:
- Auto-generated (build outputs, compiled code), they'll be recreated
- Environment-specific (your personal settings, local database), they won't work on other machines
- Sensitive (APIWhat is api?A set of rules that lets one program talk to another, usually over the internet, by sending requests and getting responses. keys, passwords), sharing these is a security disaster
.gitignore file. If you run git add . without one, you'll commit node_modules/ (hundreds of megabytes), .env files (your secrets), and build artifacts. Always create .gitignore before your first git add.Common patterns to ignore
Here's a basic .gitignore for a Node.js project:
# Create the file
touch .gitignore
# Add common patterns
echo "node_modules/" >> .gitignore
echo ".env" >> .gitignore
echo "dist/" >> .gitignore
echo "*.log" >> .gitignore| Pattern | What it ignores | Why |
|---|---|---|
node_modules/ | All npm packages | These are installed via npm install and can be huge |
.env | Environment variables | Contains secrets like API keys |
dist/ or build/ | Compiled output | Generated automatically from source |
*.log | Log files | Constantly changing, not useful for history |
.DS_Store | macOS system files | Just clutter for other operating systems |
.gitignore templates for every language. Search "github gitignore [your-language]" and copy the appropriate file. It's much better than building one from scratch.Your initialization checklist
When starting a new project, here's the exact sequence to follow:
# 1. Create your project folder
mkdir my-new-project
cd my-new-project
# 2. Initialize Git
git init
# 3. Create .gitignore (copy from GitHub templates)
curl -o .gitignore https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
# 4. Verify it worked
git statusAt this point, you're ready to start coding. When you create files, Git will notice them as "untracked." That's the subject of our next lesson.
Quick reference
| Command | What it does | When to use it |
|---|---|---|
git init | Initialize a new repo | Once per project |
git status | See repo state | Constantly, your best friend |
touch .gitignore | Create ignore file | Before first commit |
git init in existing repo | Does nothing harmful | Safe to run again |
# Initialize a new repository
git init
# Check the status of your repository
git status
# Create a .gitignore file
echo "*.log" > .gitignore
echo "node_modules/" >> .gitignore
echo ".env" >> .gitignore