Skip to content

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

bash
# Create a new project folder
mkdir my-first-git-project
cd my-first-git-project

2. Initialize Git Repository

bash
# 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

bash
# View current repository status
git status

Output 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

bash
# Create a simple text file
echo "# My First Git Project" > README.md
echo "This is a practice project to learn Git." >> README.md

2. View Status Changes

bash
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 (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

bash
# Add file to staging area
git add README.md

# View status again
git status

Output:

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

bash
# 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

bash
# View commit history
git log

Output:

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 (HEAD -> main)
Author: Your Name <your.email@example.com>
Date:   Mon Jan 1 12:00:00 2024 +0800

    Add README file

Modifying and Updating Files

1. Modify File

bash
# 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.md

2. View Modifications

bash
# View file status
git status

Output:

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")
bash
# View specific modification content
git diff

Output:

diff
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 concepts

3. Commit Modifications

bash
# 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

bash
# View detailed history
git log

# View concise history
git log --oneline

Concise output:

b2c3d4e Add learning content list
a1b2c3d Add README file

2. View File Changes

bash
# View differences between two commits
git diff a1b2c3d b2c3d4e

# View specific commit content
git show b2c3d4e

Creating More Files

Let's create a more complex project structure:

1. Create Code File

bash
# 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()
EOF

2. Create Configuration File

bash
# Create .gitignore file
cat > .gitignore << EOF
# Python
__pycache__/
*.pyc
*.pyo
*.pyd
.Python

# Editors
.vscode/
.idea/
*.swp
*.swo

# System files
.DS_Store
Thumbs.db
EOF

3. Add Multiple Files at Once

bash
# 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

bash
# View current branch
git branch

# Output:
# * main

2. Create New Branch

bash
# Create and switch to new branch
git checkout -b feature/add-greeting

# Or use new syntax (Git 2.23+)
# git switch -c feature/add-greeting

3. Work on New Branch

bash
# 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

bash
# Switch back to main branch
git checkout main

# View file content (you'll find it's the old version)
cat hello.py

5. Merge Branch

bash
# 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-greeting

Remote Repository Experience

1. Connect to GitHub (Optional)

If you have a GitHub account, you can experience remote repositories:

bash
# 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 main

2. Simulate Collaboration (Local)

bash
# 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 main

Practical Tips Demonstration

1. View Beautified Log

bash
# 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-commit

2. Undo Operation Demonstration

bash
# 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.md

3. Staging Area Operations

bash
# 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 status

Project Summary

Let's see what we've accomplished:

bash
# View final project structure
ls -la

# View complete commit history
git log --oneline --graph --all

# View project statistics
git log --stat

Your 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

bash
# 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 branch

Git Workflow Diagram

Working Directory
    ↓ git add
Staging Area
    ↓ git commit
Local Repository
    ↓ git push
Remote Repository

Next 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

  1. Repeat operations: Practice basic add, commit workflow multiple times
  2. Experiment with branches: Create more branches and try different merge scenarios
  3. View history: Use different git log parameters to view history
  4. 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!

Content is for learning and research only.