Git Quick Start
This chapter will let you experience Git's basic features in 5 minutes through a simple example, helping you quickly understand how Git works.
Preparation
Make sure you have:
- ✅ Installed Git
- ✅ Configured username and email
- ✅ Have a usable terminal or command line
Your First Git Repository
1. Create Project Folder
# Create a new project folder
mkdir my-first-git-project
cd my-first-git-project2. Initialize Git Repository
# Initialize Git repository
git init
# You will see output similar to:
# Initialized empty Git repository in /path/to/my-first-git-project/.git/🎉 Congratulations! You just created your first Git repository.
3. View Repository Status
# View current repository status
git statusOutput similar to:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)Creating and Tracking Files
1. Create First File
# Create a simple text file
echo "# My First Git Project" > README.md
echo "This is a practice project to learn Git." >> README.md2. View Status Changes
git statusOutput:
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)📝 Explanation: Git discovered a new file README.md but hasn't started tracking it yet.
3. Add File to Staging Area
# Add file to staging area
git add README.md
# View status again
git statusOutput:
On branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md✨ Explanation: The file is now in the staging area, ready to be committed.
First Commit
1. Create Commit
# Create first commit
git commit -m "Add README file"Output:
[main (root-commit) a1b2c3d] Add README file
1 file changed, 2 insertions(+)
create mode 100644 README.md🎊 Congratulations: You completed your first commit!
2. View Commit History
# View commit history
git logOutput:
commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 (HEAD -> main)
Author: Your Name <your.email@example.com>
Date: Mon Jan 1 12:00:00 2024 +0800
Add README fileModifying and Updating Files
1. Modify File
# Add more content to the file
echo "" >> README.md
echo "## Learning Content" >> README.md
echo "- Git basic operations" >> README.md
echo "- Version control concepts" >> README.md2. View Modifications
# View file status
git statusOutput:
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" or "git commit -a")# View specific modification content
git diffOutput:
diff --git a/README.md b/README.md
index 1234567..abcdefg 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1,5 @@
# My First Git Project
This is a practice project to learn Git.
+
+## Learning Content
+- Git basic operations
+- Version control concepts3. Commit Modifications
# Add modifications to staging area
git add README.md
# Commit modifications
git commit -m "Add learning content list"Viewing Project History
1. View Commit History
# View detailed history
git log
# View concise history
git log --onelineConcise output:
b2c3d4e Add learning content list
a1b2c3d Add README file2. View File Changes
# View differences between two commits
git diff a1b2c3d b2c3d4e
# View specific commit content
git show b2c3d4eCreating More Files
Let's create a more complex project structure:
1. Create Code File
# Create a simple Python script
cat > hello.py << EOF
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def main():
print("Hello, Git!")
print("This is my first Git project")
if __name__ == "__main__":
main()
EOF2. Create Configuration File
# Create .gitignore file
cat > .gitignore << EOF
# Python
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
# Editors
.vscode/
.idea/
*.swp
*.swo
# System files
.DS_Store
Thumbs.db
EOF3. Add Multiple Files at Once
# View current status
git status
# Add all new files
git add .
# View staging area status
git status
# Commit all modifications
git commit -m "Add Python script and .gitignore file"Basic Branch Operations
1. View Branches
# View current branch
git branch
# Output:
# * main2. Create New Branch
# Create and switch to new branch
git checkout -b feature/add-greeting
# Or use new syntax (Git 2.23+)
# git switch -c feature/add-greeting3. Work on New Branch
# Modify Python file
cat > hello.py << EOF
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def greet(name):
return f"Hello, {name}!"
def main():
name = input("Please enter your name: ")
greeting = greet(name)
print(greeting)
print("Welcome to the world of Git!")
if __name__ == "__main__":
main()
EOF
# Commit modifications
git add hello.py
git commit -m "Add interactive greeting feature"4. Switch Back to Main Branch
# Switch back to main branch
git checkout main
# View file content (you'll find it's the old version)
cat hello.py5. Merge Branch
# Merge feature branch to main
git merge feature/add-greeting
# View file after merge
cat hello.py
# Delete merged branch
git branch -d feature/add-greetingRemote Repository Experience
1. Connect to GitHub (Optional)
If you have a GitHub account, you can experience remote repositories:
# Create a new repository on GitHub (via web)
# Then add remote repository
git remote add origin https://github.com/yourusername/my-first-git-project.git
# Push to remote repository
git push -u origin main2. Simulate Collaboration (Local)
# Create a "collaborator" clone
cd ..
git clone my-first-git-project collaborator-project
cd collaborator-project
# Simulate collaborator's modifications
echo "- Branch management" >> README.md
git add README.md
git commit -m "Collaborator adds: Branch management learning content"
# Return to original project
cd ../my-first-git-project
# Simulate pulling updates from remote
git pull ../collaborator-project mainPractical Tips Demonstration
1. View Beautified Log
# View beautified log using alias (if configured earlier)
git lg
# Or use complete command directly
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit2. Undo Operation Demonstration
# Create a wrong modification
echo "This is a wrong modification" >> README.md
# View status
git status
# Undo working directory modifications
git restore README.md
# Or use old syntax
# git checkout -- README.md3. Staging Area Operations
# Modify file
echo "Temporary modification" >> hello.py
# Add to staging area
git add hello.py
# Remove from staging area (but keep working directory modifications)
git restore --staged hello.py
# View status
git statusProject Summary
Let's see what we've accomplished:
# View final project structure
ls -la
# View complete commit history
git log --oneline --graph --all
# View project statistics
git log --statYour project should now contain:
- 📄
README.md- Project description file - 🐍
hello.py- Python script - 🚫
.gitignore- Git ignore file - 📁
.git/- Git repository data (hidden folder)
Quick Reference Card
Basic Command Cheat Sheet
# Repository operations
git init # Initialize repository
git clone <url> # Clone repository
git status # View status
# File operations
git add <file> # Add file to staging area
git add . # Add all files
git commit -m "message" # Commit modifications
git diff # View modifications
# History viewing
git log # View commit history
git log --oneline # Concise history
git show <commit> # View specific commit
# Branch operations
git branch # View branches
git checkout -b <branch> # Create and switch branch
git merge <branch> # Merge branch
git branch -d <branch> # Delete branchGit Workflow Diagram
Working Directory
↓ git add
Staging Area
↓ git commit
Local Repository
↓ git push
Remote RepositoryNext Steps
Through this quick start tutorial, you have experienced:
- ✅ Creating and initializing Git repositories
- ✅ Adding and committing files
- ✅ Viewing history and status
- ✅ Basic branch operations
- ✅ File modifications and undoing
In the following chapters, we will learn in depth:
- Git workflow principles
- Concepts of working directory, staging area, and repository
- More branch management techniques
- Remote repository collaboration
- Advanced Git operations
Practice Suggestions
- Repeat operations: Practice basic add, commit workflow multiple times
- Experiment with branches: Create more branches and try different merge scenarios
- View history: Use different
git logparameters to view history - Make intentional mistakes: Try various undo operations and learn to fix errors
Remember: Git is a very safe system, most operations can be undone, so don't be afraid to experiment!