Git - Basic Workflow
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:
mkdir my-first-git-project
cd my-first-git-project
git init
Initialized empty Git repository in /Users/john/my-first-git-project/.git/
.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
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:
echo "# My First Git Project" > README.md
echo "This is a practice repository for learning Git." >> README.md
echo 'print("Hello, Git!")' > hello.py
Now check the status:
git status
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:
git add README.md
git status
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.
git add .
git status
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:
git commit -m "Initial commit: Add README and hello script"
[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 ID2 files changed, 3 insertions(+)
: Summary of changescreate mode 100644
: File permissions and creation status
git status
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:
echo "" >> README.md
echo "## Features" >> README.md
echo "- Learn Git basics" >> README.md
echo "- Practice version control" >> README.md
echo "*.log" > .gitignore
echo "temp/" >> .gitignore
git status
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")
git add .
git commit -m "Add project features and gitignore file"
[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
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
git log --oneline
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
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"
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
git commit
without git add
first
Solution: Always check
git status
before committing
Poor Commit Messages
Solution: Describe what you changed and why
Committing Too Much
Solution: Make smaller, focused commits
Practice Exercise
Try this on your own:
- Create a new file called
todo.txt
- Add some tasks to it
- Stage and commit it
- Modify the file by adding more tasks
- Stage and commit the changes
- 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: