1. What is Git?

Git is a distributed version control system. In simple terms, it helps you record all modification histories of your files, making it convenient for team collaboration and personal version rollback. It’s like building a “time machine” for your project, allowing you to revert to any previous version at any time.

2. Why Use Git?

  • Version Rollback: You don’t have to worry about messed-up files—you can always revert to the correct previous version.
  • Collaborative Development: When multiple people modify the same project simultaneously, Git can merge code and avoid conflicts.
  • Local Management: All operations are first performed locally before syncing to the cloud, ensuring security and reliability.

3. Installing Git

Check if Already Installed: Open your terminal (Command Prompt or PowerShell for Windows), and enter:

git --version

If a version number (e.g., git version 2.30.0) is displayed, Git is already installed. Otherwise, proceed to install.

Installation Steps:
- Windows: Download the installer from the Git official website and follow the “Next” prompts.
- Mac: Use Homebrew (install Homebrew first if needed):

  brew install git

Or download the installer from the official website.
- Linux:
- For Ubuntu/Debian: sudo apt-get install git
- For CentOS: sudo yum install git

4. Basic Concepts (Must-Know!)

Use the “Writing an Essay” analogy to understand Git’s core areas:
- Working Directory: The files you’re currently editing (e.g., your essay draft paper).
- Staging Area: Temporary storage for files to be committed (e.g., copying draft content into a “submission box”).
- Local Repository: A “file cabinet” that permanently stores commit history (the hidden .git folder in your project directory).
- Remote Repository: A “shared archive” stored in the cloud (e.g., GitHub, Gitee).

5. Basic Operations (15-Minute Quick Start)

5.1 Initialize a Repository

Goal: Create a Git repository locally.
Command: Navigate to your project folder (e.g., cd ~/myproject) and run:

git init

A hidden .git folder will appear, indicating the repository is initialized.

5.2 Configure User Information

Goal: Tell Git “who you are”—your information will be attached to commit records.
Command:

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

(--global makes settings apply to all repositories; omit it for project-specific settings.)

5.3 Track and Commit Files

Scenario: You wrote a file README.md and want to save it to Git.

  1. Check file status:
   git status

The output will show “Untracked” files.

  1. Add files to staging area:
   git add README.md  # Add a single file
   # Or git add .  # Add all untracked files (`.` = current directory)
  1. Commit to local repository:
   git commit -m "Add project description document"  # `-m` = commit message (required!)

After committing, your changes are permanently recorded in the local repository.

5.4 Version Management (Rollback/View History)

View history:

git log --oneline  # Concise display of commit records (only version hash and message)

Rollback version:
Suppose you made 3 commits and want to revert to the second one:
1. Copy the first 7 characters of the target version hash from git log (e.g., a1b2c3d).
2. Execute:

   git reset --hard a1b2c3d

(--hard forces rollback and discards changes after the target version—use with caution!)

5.5 Branch Operations (Parallel Development)

Scenario: You want to develop a new feature in a separate branch instead of the main branch.

  1. Create and switch to a branch:
   git checkout -b feature/login  # Create "feature/login" branch and switch to it
  1. Merge branches:
    After completing the feature, switch back to the main branch and merge:
   git checkout master          # Switch to main branch
   git merge feature/login      # Merge "feature/login" into master

6. Remote Repositories (Essential for Collaboration)

Assuming you use GitHub/Gitee, here are the basic operations:

6.1 Clone a Remote Repository

Goal: Copy someone else’s repository to your local machine.

git clone https://github.com/username/repo-name.git

6.2 Push Local Code to Remote

Goal: Push local changes to the remote repository.

git push origin master  # "origin" = default remote name; "master" = branch name

6.3 Pull Remote Updates

Goal: Fetch the latest code from the remote repository (to avoid conflicts with local changes).

git pull origin master

7. Common Issues Quick Reference

Scenario Command Example
Wrong commit message git commit --amend
Undo staging area changes git reset HEAD filename
Recover accidentally deleted workspace files git checkout -- filename
Force discard local changes git fetch --all && git reset --hard origin/master

8. Summary

The core of Git is “Timely commits, branch management, and version rollback”. Remember these key commands:
init (initialize) → add (stage) → commit (commit) → log (view history) → reset (rollback) → branch (branch) → push/pull (remote).

Hands-on practice is the best way to learn! Open your terminal immediately and follow the steps above—you’ll master the basics in just 30 minutes!

Xiaoye