Git - Basic Workflow

Beginner

The Git workflow is the heart of version control. Once you master these four commandsβ€”init, add, commit, and statusβ€”you'll understand 80% of daily Git usage.

The Core Workflow

Every Git project follows this pattern:

1. git init          ← Create repository
2. [Edit files]      ← Make changes  
3. git add           ← Stage changes
4. git commit        ← Save snapshot
5. [Repeat 2-4]      ← Continue working

Creating Your First Repository

Let's create a simple project to practice with:

Create and navigate to project directory:
mkdir my-first-git-project
cd my-first-git-project
Initialize Git repository:
git init
Expected Output:
Initialized empty Git repository in /Users/john/my-first-git-project/.git/
The .git directory contains all Git's metadata. Never edit files inside it manually!

Understanding Repository Status

Before making changes, let's see what Git thinks about our empty repository:

git status
Expected Output:
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

This tells us:

  • We're on the "main" branch
  • No commits exist yet
  • No files to track

Creating and Tracking Files

Let's create some files to work with:

Create a README file:
echo "# My First Git Project" > README.md
echo "This is a practice repository for learning Git." >> README.md
Create a simple Python script:
echo 'print("Hello, Git!")' > hello.py

Now check the status:

git status
Expected Output:
On branch main

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
	README.md
	hello.py

nothing added to commit but untracked files present (use "git add" to track)

Key insight: Git sees the files but isn't tracking them yet. They're in the "untracked" state.

The Three States in Detail

Every file in your Git repository is in one of these states:

Working Directory     Staging Area      Repository
     (modified) ---------> (staged) --------> (committed)
                git add              git commit
  • Working Directory: Files as they exist on your disk right now
  • Staging Area: Files prepared for the next commit
  • Repository: Permanently saved snapshots

Staging Files

Let's add files to the staging area:

Add specific file:
git add README.md
Check status:
git status
Expected Output:
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
	new file:   README.md

Untracked files:
  (use "git add ..." to include in what will be committed)
	hello.py

Notice that README.md is now "staged" (green in terminals with color), while hello.py remains untracked.

Add all files at once:
git add .
Verify all files are staged:
git status
Expected Output:
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached ..." to unstage)
	new file:   README.md
	new file:   hello.py

Making Your First Commit

A commit is like taking a snapshot of your project. Each commit needs a message explaining what changed:

Commit with message:
git commit -m "Initial commit: Add README and hello script"
Expected Output:
[main (root-commit) a1b2c3d] Initial commit: Add README and hello script
 2 files changed, 3 insertions(+)
 create mode 100644 README.md
 create mode 100644 hello.py

Congratulations! You've made your first commit. Let's break down what happened:

  • [main (root-commit) a1b2c3d]: Branch name, commit type, and unique ID
  • 2 files changed, 3 insertions(+): Summary of changes
  • create mode 100644: File permissions and creation status
Check status after commit:
git status
Expected Output:
On branch main
nothing to commit, working tree clean

"Working tree clean" means all changes are committedβ€”a satisfying state!

The Complete Workflow in Action

Let's practice the full workflow by making more changes:

1. Modify existing file:
echo "" >> README.md
echo "## Features" >> README.md
echo "- Learn Git basics" >> README.md
echo "- Practice version control" >> README.md
2. Create new file:
echo "*.log" > .gitignore
echo "temp/" >> .gitignore
3. Check what changed:
git status
Expected Output:
On branch main
Changes not staged for commit:
  (use "git add ..." to update what will be committed)
  (use "git checkout -- ..." to discard changes to working directory)
	modified:   README.md

Untracked files:
  (use "git add ..." to include in what will be committed)
	.gitignore

no changes added to commit (use "git add" or "git commit -a")
4. Stage all changes:
git add .
5. Commit changes:
git commit -m "Add project features and gitignore file"
Expected Output:
[main b2c3d4e] Add project features and gitignore file
 2 files changed, 5 insertions(+)
 create mode 100644 .gitignore

Viewing Your History

See what you've accomplished:

git log
Expected Output:
commit b2c3d4e7f8g9h0i1j2k3l4m5n6o7p8q9r0s1t2u3 (HEAD -> main)
Author: John Doe 
Date:   Wed Oct 25 14:30:22 2023 -0700

    Add project features and gitignore file

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0
Author: John Doe 
Date:   Wed Oct 25 14:25:15 2023 -0700

    Initial commit: Add README and hello script
One-line format (easier to read):
git log --oneline
Expected Output:
b2c3d4e Add project features and gitignore file
a1b2c3d Initial commit: Add README and hello script

Common Variations

Selective Staging

# Add specific files
git add README.md hello.py

# Add all files in directory
git add src/

# Add all Python files
git add *.py

# Add all changes (tracked files only)
git add -u

# Add all changes including untracked files
git add .

Commit Shortcuts

# Stage all tracked files and commit in one step
git commit -am "Quick commit message"

# Open editor for longer commit message
git commit

# Amend the last commit (change message or add files)
git commit --amend

Best Practices for Beginners

Good Commit Messages

βœ… Good examples:
git commit -m "Add user authentication feature"
git commit -m "Fix bug in login validation"
git commit -m "Update README with installation instructions"
git commit -m "Remove deprecated API endpoints"
❌ Poor examples:
git commit -m "stuff"
git commit -m "changes"
git commit -m "fixed it"
git commit -m "asdf"

Commit Frequency

  • βœ… Commit often: Small, logical changes are easier to understand and undo
  • βœ… One feature per commit: Don't mix unrelated changes
  • βœ… Test before committing: Don't commit broken code
  • ❌ Don't commit half-finished work: Use stashing instead (covered later)

Understanding git status Output

Learn to read Git's status messages:

Changes to be committed:      ← Staged (green)
Changes not staged for commit: ← Modified but not staged (red)  
Untracked files:              ← New files Git doesn't know about

Common Beginner Mistakes

Forgetting to Stage

Problem: Running git commit without git add first
Solution: Always check git status before committing

Poor Commit Messages

Problem: Vague messages like "fix" or "update"
Solution: Describe what you changed and why

Committing Too Much

Problem: One huge commit with many unrelated changes
Solution: Make smaller, focused commits

Practice Exercise

Try this on your own:

  1. Create a new file called todo.txt
  2. Add some tasks to it
  3. Stage and commit it
  4. Modify the file by adding more tasks
  5. Stage and commit the changes
  6. Use git log --oneline to see your history

Summary

You now understand Git's fundamental workflow:

  • βœ… git init creates a repository
  • βœ… git status shows current state
  • βœ… git add stages changes
  • βœ… git commit saves snapshots
  • βœ… Files exist in three states: modified, staged, committed
  • βœ… Good commit messages are descriptive and concise

Next Steps

Now that you understand the local workflow, let's learn about creating and managing repositories:

β†’ Git - Repository Basics

Practice Tip: Use this workflow daily. Create small practice projects and commit frequently. Muscle memory for these commands is essential for Git mastery.