What is Git?

In simple terms, Git is a “version control tool” that helps us record all modifications to project files, revert to any previous version at any time, and merge code when multiple people collaborate on development. Today, we’ll organize the most commonly used Git commands, from basic to advanced, so you can pull, push, and switch branches with ease!

I. Basic Operations: Initialization & Cloning

1. Initialize a Local Repository

Purpose: Convert an ordinary folder into a Git-manageable repository.
Command: git init
Scenario: When you start a new project and want to use Git for management.
Example:

# Run this in your project folder
git init  

2. Clone a Remote Repository (Pull a Project from Others)

Purpose: Copy a project hosted in a remote repository (e.g., GitHub, GitLab) to your local machine.
Command: git clone <repository-url>
Scenario: When you first start working on a project and need to download the code to your computer.
Example:

# Clone a repository from GitHub (replace with the actual URL)
git clone https://github.com/xxx/xxx.git  

II. Daily Operations: Committing & Viewing

1. Check File Status

Purpose: See which files have been modified, added, or are uncommitted.
Command: git status
Scenario: After writing code, to check the “changes” in the repository.
Example:

# The output will show modified files (red), staged files (green), etc.
git status  

2. Stage Changes

Purpose: Temporarily move modified files to a staging area (preparation for commit).
Command: git add <filename> (single file) or git add . (all files)
Scenario: When you want to commit changes in batches or temporarily save all modifications.
Example:

# Stage all modified files
git add .  

# Stage a specific file (e.g., index.html)
git add index.html  

3. Commit to Local Repository

Purpose: Permanently record staged changes in the local repository (like “saving the current version”).
Command: git commit -m "Commit message"
Scenario: After completing a small feature (e.g., “fix login bug”), commit the changes.
Example:

# Use a concise message to describe what was changed
git commit -m "Fix navigation bar misalignment on the homepage"  

III. Branch Operations: Switching & Merging

1. List Branches

Purpose: View all local branches.
Command: git branch
Scenario: To check which branch you’re on or which branches are available to switch to.
Example:

# All branches are listed; the current branch is marked with a *
git branch  

2. Create & Switch to a New Branch

Purpose: Create a new branch and switch to it immediately (most common use!).
Command: git checkout -b <new-branch-name>
Scenario: To develop a new feature (e.g., “user registration”) without modifying the main branch directly.
Example:

# Create and switch to a branch named "feature/register"
git checkout -b feature/register  

3. Switch to an Existing Branch

Purpose: Switch from the current branch to another existing branch.
Command: git checkout <existing-branch-name>
Scenario: After completing a feature, switch back to the main branch to merge code.
Example:

# Switch to the master branch
git checkout master  

4. Merge Branches

Purpose: Merge changes from one branch into the current branch (e.g., merge a feature branch into master).
Command: git merge <branch-to-merge>
Scenario: After a feature is completed, merge the branch code into the main branch for others to see.
Example:

# First, switch to the master branch, then merge the feature/register branch
git checkout master  
git merge feature/register  

IV. Core Operations: Pull & Push

1. Pull Code (Update Local)

Purpose: Fetch the latest code from the remote repository to avoid conflicts between local and remote versions.
Command: git pull <remote-repo> <branch>
Scenario: When others have pushed code to the remote, pull it first before continuing development.
Example:

# Pull the master branch from the "origin" remote repository
git pull origin master  

2. Push Code (Upload to Remote)

Purpose: Push local commits to the remote repository so team members can see your changes.
Command: git push <remote-repo> <branch>
Scenario: After local commits, push to the remote repository (add -u for the first push to set the upstream branch).
Example:

# First push of the feature/register branch to origin (sets upstream for future pushes)
git push -u origin feature/register  

# Subsequent pushes to the same branch can be done with a simple "git push"
git push  

V. Undo & Recovery (Essential for Avoiding Pitfalls)

1. Undo Uncommitted Changes

Purpose: If you’ve modified files but not committed, revert to their previous state.
Command: git checkout -- <filename>
Scenario: After making changes you decide are incorrect, discard them.
Example:

# Discard changes to index.html
git checkout -- index.html  

2. Undo a Commit (Revert to Previous Version)

Purpose: If you committed incorrect content, revert to the state before the last commit.
Command: git reset --soft HEAD~1
Scenario: When the commit message is wrong or code was committed prematurely.
Example:

# Revert to the previous commit (--soft keeps changes; --hard deletes changes, use with caution!)
git reset --soft HEAD~1  

VI. Important Notes

  1. Clear Commit Messages: Always write clear commit messages (e.g., “Fix login button unresponsive issue”) to make it easy to trace changes later.
  2. Branch Naming Conventions: Use formats like feature/xxx (feature branch) or bugfix/xxx (bugfix branch) to avoid confusion.
  3. Pull Before Pushing Collaboratively: If multiple people modify the same file, pull first to avoid overwriting others’ changes.
  4. Use --hard with Caution: git reset --hard permanently deletes changes; confirm before using!

VII. Summary

Git commands don’t need to be memorized—just a few key ones: init (initialize), clone (clone), add (stage), commit (save), status (status), pull (fetch), push (upload), checkout (branch), merge (merge). Practice these repeatedly, and operations like pull, push, and branch switching will become second nature!

Need more scenario-specific commands? Let me know in the comments—I’ll add more content later!

Xiaoye