Working Code
Example 1: Checking your Git installation and configuration
Check if Git is installed:
git --version
Expected output:
git version 2.43.0
Any version 2.x or higher will work. If Git is not installed:
# macOS
brew install git
# Verify
git --version
Initial setup (one-time only, applies to all projects):
git config --global user.name "John Doe"
git config --global user.email "john@example.com"
Example 2: Creating your first repository
mkdir my-project
cd my-project
git init
Output:
Initialized empty Git repository in /Users/dale/my-project/.git/
Example 3: Checking status with git status
git status
Output:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
Create a file and check again:
echo "# My First Project" > README.md
git status
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
Untracked files are files Git is not yet tracking. They appear in red.
Try It Yourself
git status color meanings
git status
Output colors:
- Red: Untracked or modified but not staged
- Green: Staged files (waiting to be committed)
# Add files
echo "First file" > file1.txt
echo "Second file" > file2.txt
git status
Output:
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
file1.txt
file2.txt
# Stage one file
git add README.md
git status
Output:
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
new file: README.md <- green
Untracked files:
file1.txt <- red
file2.txt
What is HEAD?
HEAD is a pointer to your current working position.
Commit A -> Commit B -> Commit C
^
HEAD (current position)
Right now there are no commits, so HEAD has nothing to point to. It becomes active after the first commit.
"Why?" -- What if there were no version control?
Without version control, you end up with problems like these:
project_final.zip,project_final_v2.zip,project_final_REALLY_FINAL.zip- "It worked yesterday but broke today" -- no idea what changed
- A teammate edits the same file and someone's work gets overwritten
Git solves all of these:
| Problem | Git Solution |
| ---------------------------- | ------------------------------------ |
| Multiple file versions | Snapshots saved as commits |
| What changed? | git diff shows change history |
| Revert to a previous version | Restore from commit history |
| Team collaboration conflicts | Branches and merges manage conflicts |
Git's three areas
Working Directory -> (git add) -> Staging Area -> (git commit) -> Repository
- Working Directory: Where your actual files live
- Staging Area: A holding area for changes to include in the next commit
- Repository: Where commit history is stored (the
.gitdirectory)
Common Mistakes
Mistake 1: Forgetting git config
git commit # Without setting up identity
# Author identity unknown
# Please tell me who you are.
Set your name and email first:
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
Mistake 2: Running git init inside a nested directory
cd ~/projects/myapp
git init
# Accidentally running it again in a subdirectory
cd src
git init # Creates a nested repository
Run git init only once, at the project root.
Mistake 3: Deleting the .git directory
# Never do this!
rm -rf .git # All Git history is gone
The .git directory is Git's database. Deleting it is irreversible.
Deep Dive
Checking and managing git config
Your settings are stored in ~/.gitconfig:
git config --list
Output:
user.name=John Doe
user.email=john@example.com
core.editor=vim
Check individual settings:
git config user.name
# John Doe
# View the global config file directly
cat ~/.gitconfig
Per-project settings (local config):
# Use a different email for a specific project
git config user.email "work@company.com" # Without --global, it's project-specific
Inside the .git directory
git init creates a hidden .git directory:
ls .git/
Output:
HEAD config description hooks/ info/ objects/ refs/
Key files:
HEAD: Pointer to the current branchconfig: Repository-specific settingsobjects/: Actual commit, file, and tree data (stored as SHA-1 hashes)refs/: Branch and tag pointers
Understanding this structure reveals how Git works under the hood.
Setting the default branch name (main vs master)
Recent Git versions default to main:
# Set default branch name for new repositories
git config --global init.defaultBranch main
For existing repositories created with master:
# Rename master to main
git branch -m master main
- Run
git --versionto verify Git is installed. - Set your name and email with
git config --global user.name "Name"andgit config --global user.email "email". - Create a new directory and initialize it with
git init. - Create a
README.mdfile and rungit status. Think about what the red and green items mean. - Run
git config --listto review your current settings.
Q1. What is the difference between red and green files in git status?
- A) Red = deleted files, Green = new files
- B) Red = not staged, Green = staged files
- C) Red = conflicted files, Green = normal files
- D) Red = binary files, Green = text files