Prerequisites: Part 3: Setting Up Git
Time: 20-30 minutes
Goal: Create your first Git repository and make your first commits
A repository (or "repo") is a folder that Git is tracking. It contains:
- Your project files (code, documents, images, etc.)
- Git's hidden
.gitfolder with all the version history - Optional configuration files
Think of it as a "project folder with superpowers"!
Let's create a simple repository to practice with.
# Navigate to where you want to create your project
cd ~
# Create a new directory
mkdir my-first-repo
cd my-first-repo
# Verify you're in the right place
pwdYou should see a path ending in my-first-repo.
git initExpected Output:
Initialized empty Git repository in /path/to/my-first-repo/.git/
π Congratulations! You just created your first Git repository!
# Check the current status
git statusExpected Output:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
# Look for hidden files (you should see .git folder)
ls -laThe .git folder is where Git stores all the version history. Never delete this folder!
# Create a simple text file
echo "# My First Git Repository" > README.md
echo "This is my first project using Git!" >> README.mdOr create the file with your text editor:
# My First Git Repository
This is my first project using Git!
## What I'm Learning
- How to initialize a Git repository
- How to add files to Git
- How to make commits
- The basics of version control
## Goals
- [ ] Make my first commit
- [ ] Understand the staging area
- [ ] Learn to write good commit messagesgit statusExpected Output:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)
Git found your file but isn't tracking it yet!
Git has three main areas:
Working Directory β Staging Area β Repository (.git)
(your files) (files ready (committed files)
to commit)
- Working Directory: Where you edit files
- Staging Area: Files ready to be committed (like a "preparation area")
- Repository: Permanently saved snapshots of your project
git add README.mdgit statusExpected Output:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
The file is now "staged" and ready to commit!
git commit -m "Add README file with project description"Expected Output:
[main (root-commit) a1b2c3d] Add README file with project description
1 file changed, 1 insertion(+)
create mode 100644 README.md
π You just made your first commit! This is a permanent snapshot of your project.
git statusExpected Output:
On branch main
nothing to commit, working tree clean
Perfect! Everything is committed and saved.
Your commit message should explain what you did and why:
git commit -m "Add user login functionality"
git commit -m "Fix typo in header navigation"
git commit -m "Update README with installation instructions"
git commit -m "Remove deprecated API calls"git commit -m "stuff" # Too vague
git commit -m "fixed it" # What did you fix?
git commit -m "asdf" # Not descriptive
git commit -m "COMMIT" # Useless[Action] [What you changed] [Why, if not obvious]
Examples:
Add contact form to homepage
Fix navigation menu on mobile devices
Update dependencies for security patch
Remove unused CSS classes
Let's practice with more files:
echo "print('Hello, Git!')" > hello.pyOpen README.md in your text editor and add:
## Files in this project
- README.md - This description file
- hello.py - A simple Python scriptgit statusYou should see:
hello.pyas untracked (new file)README.mdas modified
# See exactly what changed in README.md
git diff README.md# Add specific files
git add README.md
git add hello.py
# Or add everything at once
git add .git commit -m "Add Python script and update README"# See all commits
git logExpected Output:
commit b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b2c3
Author: Your Name <your.email@example.com>
Date: Mon Jan 15 10:30:00 2024 -0500
Add Python script and update README
commit a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0
Author: Your Name <your.email@example.com>
Date: Mon Jan 15 10:15:00 2024 -0500
Add README file with project description
# Compact view
git log --oneline
# See what files changed
git log --stat
# Visual graph (useful with branches later)
git log --graph --onelineTry this on your own:
- Create a file called
notes.txt - Add some text about what you've learned
- Stage the file with
git add - Commit with a descriptive message
- Check the log to see your new commit
Solution:
echo "I learned how to initialize a Git repository!" > notes.txt
git add notes.txt
git commit -m "Add learning notes file"
git log --oneline| Command | What it does |
|---|---|
git init |
Initialize new repository |
git status |
Check repository status |
git add <file> |
Stage specific file |
git add . |
Stage all changes |
git commit -m "message" |
Create commit with message |
git log |
View commit history |
git log --oneline |
Compact commit history |
- Make sure you ran
git init - Check you're in the right directory
- You haven't made any changes
- Or changes aren't staged (run
git add)
- Set your name and email (review Part 3)
- It's hidden. Use
ls -lato see hidden files - On Windows, enable "Show hidden files"
- β
How to create a Git repository with
git init - β The difference between working directory, staging area, and repository
- β
How to add files with
git add - β
How to make commits with
git commit - β How to write good commit messages
- β
How to view project history with
git log - β Basic Git workflow: edit β add β commit
Great job! You now understand the fundamental Git workflow. Next, we'll dive deeper into Git commands and learn about viewing differences between versions.
Continue to Part 5: Basic Git Commands
Before moving on, make sure you can:
- Create a new Git repository from scratch
- Add files to the staging area
- Make commits with descriptive messages
- View your commit history
- Understand what
git statusis telling you
Previous: Part 3: Setting Up Git | Next: Part 5: Basic Git Commands