1. What is Git? Why Learn Git?¶
In simple terms, Git is a version control system (VCS), like a “file history recorder” that helps you:
- Track every modification to files and revert to previous versions at any time;
- Avoid code conflicts and simplify merging changes from multiple contributors during collaboration;
- Save different “states” of your project (e.g., “branches” for developing new features) without affecting the main project.
Scenario Example: If you wrote a paper and worry about mistakes after edits, Git can easily restore the original version. When working in a team, if you modify code segment A and a colleague modifies segment B, Git automatically merges changes instead of causing conflicts (“fights” between modifications).
2. Installing and Configuring Git¶
2.1 Installing Git¶
- Windows: Download the installer from the official website (https://git-scm.com/) and follow the default setup (“Next” repeatedly).
- Mac: If Homebrew is not installed, open Terminal and run:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Then install Git with:
brew install git
- Linux: For Ubuntu/Debian:
sudo apt install git
For CentOS:
sudo yum install git
After installation, open Terminal (use “Git Bash” or “Command Prompt” on Windows) and run:
git --version
If the version number (e.g., git version 2.40.1) appears, the installation is successful!
2.2 Configuring Git Identity¶
Git records your identity (name and email) for every commit, like a “signature”. First, configure the global identity (applies to all repositories):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
To set a repository-specific identity (only for a particular project), remove --global and run in the project directory:
git config user.name "Project-Specific Name"
git config user.email "project-specific-email@example.com"
3. Local Repository: Creating a Project from “0” to “1”¶
3.1 Initializing a Repository¶
Create a new folder (e.g., my-first-git-project) on your computer, then navigate to it and initialize it with Git:
# Navigate to the folder (e.g., Windows on D: drive, Mac/Linux in current directory)
cd D:/my-first-git-project
# Initialize the repository
git init
After running, a hidden .git folder will appear in the directory. This is the core data of your Git repository, and your project is now a Git repository!
3.2 Adding Files and Committing¶
Create a file (e.g., index.html) in my-first-git-project with content like <h1>Hello Git!</h1>.
- Step 1: Stage the file in Git’s “staging area”
Usegit addto add files to the staging area (tells Git: “I want to commit this file”):
# Add a single file
git add index.html
# Add all files (use '.' for all changes)
git add .
- Step 2: Commit to the local repository
After staging, usegit committo save changes to the local repository. Include a descriptive message with-m:
git commit -m "First commit: Add index.html file"
The -m flag is for commit messages, which should clearly explain changes (e.g., “Fixed bug” or “Added feature”) for easy tracking.
3.3 Checking Repository Status and History¶
- Check file status: Use
git statusto see which files are modified, added, or deleted (red = unstaged, green = staged). - View commit history: Use
git logto see all commit records. Pressqto exit:
git log
4. Branch Management: A “Safe Space” for Collaboration¶
Project development directly on the main branch risks errors. Git’s branching feature (like “parallel universes”) solves this.
4.1 Creating and Switching Branches¶
- Create a branch: For example, create a
devbranch frommasterto develop new features:
git branch dev # Create the dev branch
- Switch branches: Use
git checkoutto switch to thedevbranch:
git checkout dev
(Note: In older Git versions, use git checkout -b dev to create and switch in one step.)
4.2 Modifying and Merging Branches¶
Make changes in the dev branch and commit (similar to earlier steps: git add . + git commit -m "Modified dev feature").
When ready, merge dev into master:
# Switch back to the master branch
git checkout master
# Merge dev into master
git merge dev
If conflicts occur (both branches modified the same file), Git will mark conflicting sections. Manually resolve conflicts by editing the file (Git uses markers like <<<<<<< HEAD to indicate conflicts; keep desired code and remove markers).
5. Remote Repository: “Putting Your Project Online” (Collaboration & Deployment)¶
All previous steps are local. To enable collaboration or deploy, push the project to a remote repository (e.g., GitHub, Gitee).
5.1 Preparing a Remote Repository¶
- Register on a platform: Use GitHub (global) or Gitee (domestic, faster access). After registration, create a new repository named
my-first-git-projectand save its URL (e.g.,https://github.com/YourUsername/my-first-git-project.git).
5.2 Linking Local Repository to Remote¶
If you already created a local project, link it to the remote repository:
# Link the remote repository (origin is the default alias)
git remote add origin https://github.com/YourUsername/my-first-git-project.git
# Push the local master branch to the remote master branch
git push -u origin master
# Subsequent pushes can omit origin and branch
git push
To clone a remote repository (e.g., pull others’ projects):
git clone https://github.com/YourUsername/my-first-git-project.git
6. Deploying Your Project: From Remote to Live Server¶
Deployment is publishing your project to a server for public access. This example uses static website deployment (HTML/CSS/JS):
6.1 Pulling Code to the Server¶
If your server doesn’t have the project:
# Clone the remote repository to the server
git clone https://github.com/YourUsername/my-first-git-project.git
# Enter the project directory and install dependencies (e.g., Node.js projects)
cd my-first-git-project
npm install
# Build the project (e.g., for Vue/React)
npm run build
6.2 Server Deployment¶
- Static websites: Copy files from the
builddirectory to Nginx’s static folder (e.g.,/var/www/html) and configure Nginx to point to this directory. - Dynamic projects (Node.js): Use
pm2to start the Node process (pm2 start app.js), then configure Nginx to reverse-proxy tolocalhost:3000(assuming the Node service runs on port 3000).
7. Quick Reference for Common Git Commands¶
| Command | Purpose |
|---|---|
git init |
Initialize a new repository |
git add <file> |
Stage a file (use . for all files) |
git commit -m "message" |
Commit staged changes to the local repo |
git status |
Check repository status (modified files) |
git log |
View commit history |
git branch |
List all branches |
git checkout <branch> |
Switch to a branch |
git branch <branch> |
Create a new branch |
git merge <branch> |
Merge a branch into the current branch |
git remote add origin <url> |
Link a remote repository |
git push origin <branch> |
Push local branch to remote |
git pull origin <branch> |
Pull remote branch to local |
git clone <url> |
Clone a remote repository to local |
8. Conclusion¶
Git’s core is version control and collaboration. The workflow spans:
1. Creating/committing to a local repository;
2. Managing branches;
3. Synchronizing with a remote repository;
4. Deploying to production.
Newcomers don’t need to memorize all commands at once. Focus on the basic flow: init → add → commit → push → deploy, then explore advanced topics (e.g., conflict resolution, tagging) later. With hands-on practice, you’ll master Git quickly!