1. What is Git? Why Version Control?¶
Git is a distributed version control system—a tool that helps you manage code “changes.” Think of it like a “code diary”: when you write an article, you save a “version” after each edit without overwriting previous content. Git lets you record every modification, switch back to any version at any time, and even merge changes from multiple collaborators.
2. Installing Git¶
- Download the installer: Visit the Git official website and download the version for your OS (Windows/macOS/Linux).
- Installation steps:
- Windows: Run the installer and click “Next” repeatedly. Finally, check “Use Git Bash to open the terminal” (for easier operations later).
- macOS/Linux: Install via your system’s package manager (e.g., macOS with Homebrew:brew install git; Linux withsudo apt install git). - Verify installation: Open your terminal (Git Bash for Windows, Terminal for macOS/Linux) and run:
git --version
If the version number appears, Git is installed successfully.
3. Configuring Git Identity¶
Every time you commit code, Git records your name and email. Set this once (permanent):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Note: Use the same email as your code hosting platform (e.g., GitHub, Gitee) for easy identification.
4. Cloning a Remote Repository¶
A “remote repository” is a code store on the internet (e.g., GitHub, Gitee). To start working, copy it to your local machine:
1. Get the repository URL: On the remote platform (e.g., GitHub), navigate to your repo, click “Clone or download,” and copy the URL (e.g., https://github.com/YourUsername/YourRepo.git).
2. Clone the repo: Run in your local terminal:
git clone https://github.com/YourUsername/YourRepo.git
Git will create a folder (named after the repo) with all remote content.
5. Understanding the Repository’s “Three-Zone” Structure¶
After cloning, enter the repo folder (e.g., cd YourRepo). The core structure is:
- Working Directory: Files you’re actively editing (e.g., index.html).
- Staging Area: Temporary storage for changes (use git add to “stage” work).
- Local Repository: Where versions are permanently saved (use git commit to save staged changes).
Workflow analogy:
Working Directory (drafts) → git add (move to “to-do folder”) → git commit (save to “diary”).
6. Modify Code and Commit to Local Repository¶
Suppose you edit index.html (e.g., change the title from <title>Old Title</title> to <title>New Title</title>). Here’s how to commit:
- Check status: Run
git statusto see modified files (e.g.,modified: index.html). - Stage changes: Move files to the staging area (choose specific files or all):
git add index.html # Stage only index.html
# OR
git add . # Stage all changes (note the space before the dot)
- Commit to local repo: Save staged changes with a descriptive message:
git commit -m "Update homepage title to New Title"
Write concise, clear messages (e.g., “Fix bug” or “Add navbar”) for easy tracking.
7. Push Code to Remote Repository¶
Local changes aren’t synced to the remote yet. Push to update the remote:
git push origin main
origin: Default name for the remote repo (no need to change).main: Branch name (older repos may usemaster;mainis now standard).
8. Quick Cheat Sheet for Basic Commands¶
| Command | Purpose |
|---|---|
git status |
Check current repo modification status |
git log |
View local commit history |
git pull |
Fetch and merge remote updates |
git branch |
List all branches (default: main) |
git checkout -b new-branch |
Create and switch to a new branch (advanced) |
9. Summary¶
Git’s core workflow is: Clone → Modify → Stage → Commit → Push. Remember these simple commands (clone/add/commit/push), and you’ll master version control. Start with a small repo (e.g., create an empty repo on GitHub/Gitee, clone it, edit a file, and push) to practice!
Next step: Try creating a repo on GitHub/Gitee, clone it locally, modify a file, commit, and push to experience Git’s magic firsthand!