DevOps · Infrastructure as Code (IaC) · Git Series – Part 3
Now that your Git environment is configured with your username, email, editor, and helpful aliases, you’re ready for the real action — creating repositories, tracking files, and making your first commit.
This chapter covers:
- What a repository is
- How to create a Git repo (2 methods)
- How to add files
- How to stage & commit
- How to inspect your repository
- How to undo mistakes safely
- Hands-on examples
Let’s begin.
A git repository is the foundation of version control in Git. This comprehensive guide teaches you how to initialize a repository, make commits, and master the fundamentals of Git workflow.
Table of Contents
1. What Is a Git Repository?
A Git repository (repo) is a collection of files plus the entire history of changes.
It’s your project folder + Git’s change-tracking engine.
Two types:
- Local repository — on your system
- Remote repository — hosted on GitHub / GitLab / Bitbucket
We start with local repos here.
2. Create a New Git Repository
Method 1: Convert an existing folder
If you already have a project:
cd my-project
git initThis command creates a hidden directory:
.git/
This is where Git stores commits, logs, branches, and metadata.
Method 2: Create a new project with Git
mkdir my-first-git-project
cd my-first-git-project
git init
You now have a fully functional Git repository.
3. Checking Repository Status
Your first command in any repo is always:
git status
It tells you:
- New or modified files
- Tracked vs untracked files
- Current branch
- What needs to be staged or committed
4. Create Your First File
echo "Hello Git!" > hello.txt
Check status:
git status
You will see untracked files.
5. Stage Your First Change
Staging tells Git which changes you are ready to commit.
git add hello.txt
Or stage everything:
git add .
Recommended alternative:
git add -A
Check again:
git status
You’ll now see the file under Changes to be committed.
6. Make Your First Commit
A commit is a snapshot of the staged changes.
git commit -m "Initial commit: added hello.txt"
You have now officially created your first Git commit.
7. Viewing Commit History
git log
Or a prettier one if you set aliases:
git lg
8. Modify a File & Track the Change
echo "This is my first change." >> hello.txt
git status
Again, you will see the file as modified.
Stage and commit:
git add hello.txt
git commit -m "Updated hello file with new line"
9. Undoing Mistakes (Safe Options)
Mistakes happen — Git helps you fix them without losing work.
Undo staged file
You added it accidentally:
git restore --staged hello.txt
Undo changes in working directory
Revert the file to last commit:
git restore hello.txt
Amend last commit
Fix wrong commit message or include an extra change:
git commit --amend
10. Full Hands-On Example
Start fresh:
mkdir demo-git
cd demo-git
git init
Create a file:
echo "Git Demo" > demo.txt
git status
git add demo.txt
git commit -m "Initial commit for demo"
git log --oneline
Modify:
echo "Adding more content." >> demo.txt
git add .
git commit -m "Updated demo.txt"
git log --oneline --graph
11. Common Git Commands Summary Table
| Action | Command |
|---|---|
| Initialize repo | git init |
| Check status | git status |
| Stage file | git add <file> |
| Stage everything | git add . |
| Commit changes | git commit -m "message" |
| View history | git log |
| Undo staged file | git restore --staged <file> |
| Undo working changes | git restore <file> |
| Amending commit | git commit --amend |
12. What’s Next?
You now know how to:
✔ Create repositories
✔ Add files
✔ Stage & commit
✔ View history
✔ Undo mistakes
In the next chapter:
➡️ “Git New Files – Understanding Tracked vs Untracked, Modified, and Ignored Files”
You’ll learn how Git treats files at every stage and how .gitignore works.