Git is like the “Swiss Army knife” in version control tools. Mastering it helps you efficiently manage code, track changes, and collaborate on development. But for beginners, the sheer number of commands can be overwhelming? Don’t worry! This article compiles the 10 most core and commonly used Git commands, guiding you from “initializing a repository” to “collaborative pushing” to quickly get started with Git’s basic operations.

1. git init

Purpose: Initialize a new local Git repository (turn a regular folder into a project manageable by Git).
Syntax: git init
Example: Create a new folder my-project on your computer, open the terminal, navigate to this folder, and run git init. A hidden .git folder will appear in the directory, which is the “heart” of your local repository.
Tip: If you want to directly clone someone else’s repository (e.g., a project on GitHub), use git clone instead of init first—it’s simpler.

2. git clone

Purpose: Copy a complete codebase from a remote repository (e.g., GitHub, GitLab) to your local machine.
Syntax: git clone [remote repository URL]
Example: To clone a repository https://github.com/YourUsername/YourProject.git from GitHub to your local machine, simply run git clone https://github.com/YourUsername/YourProject.git. A folder with the same name as the remote repository will be created locally, containing all the code.
Tip: The remote repository URL can be copied from the project page on GitHub/GitLab (usually an HTTPS or SSH link).

3. git add

Purpose: Stage changes from your working directory (files you just modified) into the “staging area,” telling Git, “These files are ready to be committed.”
Syntax:
- Stage a single file: git add [filename] (e.g., git add app.js)
- Stage all changes: git add . (. represents all files in the current directory)
Example: If you modified app.js and style.css, running git add . will stage both files into the “to-be-committed area.”
Tip: Always use git add after modifying code—otherwise, git commit won’t record your changes!

4. git commit -m "Commit message"

Purpose: Officially commit the staged code to the local repository, creating a version record (with details like modified content, time, and author).
Syntax: git commit -m "Concise commit message"
Example: After staging, run git commit -m "Fix bug where login button was unclickable". The commit message should be clear and concise (e.g., “Fix bug,” “Add user registration feature”) for easy history review later.
Tip: The -m flag is mandatory; otherwise, Git will open an editor for you to write the message (a common pitfall for beginners—using -m is faster).

5. git status

Purpose: Check the current state of your repository, showing which files have been modified, added, or are uncommitted.
Syntax: git status
Example: The output might show:
- Changes to be committed: Files are staged and ready for commit.
- Changes not staged for commit: Files modified but not yet staged (need git add).
- nothing to commit, working tree clean: All changes are committed, and the working directory is clean.
Tip: Always run git status after code changes to avoid missing files that need to be committed.

6. git log

Purpose: View the commit history of your local repository to understand what changes were made, when, and by whom.
Syntax: git log
Example: The output will look like:

commit a1b2c3d (HEAD -> master)
Author: Your Name <your.email@example.com>
Date:   2024-01-01 12:00:00 +0800
    Fix bug where login button was unclickable
commit 456789e
Author: Your Name <your.email@example.com>
Date:   2023-12-31 15:30:00 +0800
    Add user registration page

Tip: Press q to exit the log view. For a more concise display, use git log --oneline (shows only commit IDs and messages).

7. git checkout -b [branch-name]

Purpose: Create and switch to a new branch (e.g., for developing new features or fixing bugs without affecting the main branch).
Syntax: git checkout -b [new-branch-name]
Example: To develop a “user center” feature, run git checkout -b feature/user-center. This creates a new branch named feature/user-center and switches to it automatically.
Tip: Branches are essential for parallel development (e.g., master for stable code, feature branches for new features). Use naming conventions like feature/[name] or bugfix/[name].

8. git merge [branch-name]

Purpose: Merge changes from a specified branch into the current branch (e.g., merging feature/user-center into master).
Syntax: git merge [branch-to-merge]
Example: While on the master branch, run git merge feature/user-center to merge all commits from feature/user-center into master.
Tip: Ensure no uncommitted changes exist in the current branch before merging. If conflicts occur (e.g., conflicting changes in the same file), you’ll need to manually resolve them. (For now, just remember the command; conflict resolution can be explored later.)

9. git pull

Purpose: Fetch the latest code from a remote repository and automatically merge it into the current branch (to avoid outdated local code and conflicts).
Syntax: git pull [remote-repo] [branch] (remote-repo defaults to origin, branch defaults to master)
Example: On your local master branch, run git pull origin master to fetch the latest master code from the remote repository and merge it locally.
Tip: In collaborative environments, git pull is critical to sync with others. Always pull before making changes to prevent conflicts.

10. git push origin [branch-name]

Purpose: Push local branch changes to a remote repository (so team members can access your work).
Syntax: git push [remote-repo] [branch] (remote-repo defaults to origin)
Example: After completing work in the feature/user-center branch, run git push origin feature/user-center. The remote repository will update this branch, and others can pull your code with git pull.
Tip: Always git pull before pushing to confirm no conflicts with the remote branch—otherwise, your push may fail due to outdated local code.

Summary

These 10 commands cover the core workflow of daily Git operations:
Initialize/Clone → Modify & Stage (add) → Commit → Branch Management (checkout/merge) → Collaborate (pull/push).

It’s normal to forget commands initially. Practice them in real projects (e.g., try with your own GitHub repo). Once mastered, Git will efficiently manage your code and eliminate version chaos!

Xiaoye