Git Basic Operations
Git's job is to create and save snapshots of your project and compare them with later snapshots.
This chapter introduces the commands for creating and committing snapshots of your project.
The following 6 commands are commonly used in Git:git clone、git push、git add 、git commit、git checkout、git pull, which we will introduce in detail later.

Explanation:
- workspace: Working Directory
- staging area: Staging Area/Cache
- local repository: Version Library or Local Repository
- remote repository: Remote Repository
A simple operation procedure:
$ git init
$ git add .
$ git commit
- git init - Initialize a repository.
- git add . - Add files to the staging area.
- git commit - Add the contents of the staging area to the repository.
Repository Creation Commands
The following table lists the commands for creating a repository in git:
| Command | Description |
|---|---|
git init |
Initialize a repository |
git clone |
Copy a remote repository, that is, download a project. |
Commit and Modify
Git's job is to create and save snapshots of your project and compare them with later snapshots.
The following table lists the commands related to creating and committing snapshots of your project:
| Command | Description |
|---|---|
git add |
Add files to the staging area |
git status |
View the current status of the repository and display the files with changes. |
git diff |
Compare file differences, i.e., the differences between the staging area and the working directory. |
git difftool |
Use an external diff tool to view and compare file changes. |
git range-diff |
Compare the differences between two commit ranges. |
git commit |
Commit the staging area to the local repository. |
git reset |
Revert to a previous version. |
git rm |
Delete files from the staging area and working directory. |
git mv |
Move or rename working directory files. |
git notes |
Add a note. |
git checkout |
Switch branches. |
git switch (Git 2.23 版本引入) |
Switch branches more clearly. |
git restore (Git 2.23 版本引入) |
Restore or undo file changes. |
git show |
Display detailed information about Git objects. |
Commit Log
| Command | Description |
|---|---|
git log |
View historical commit records |
git blame |
View the historical modification records of a specified file in list form |
git shortlog |
Generate a concise commit log summary |
git describe |
Generate a readable string that describes the current commit based on Git's tag system |
Remote operations
| Command | Description |
|---|---|
git remote |
Remote Repository Operations |
git fetch |
Fetch a code repository from remote |
git pull |
Download remote code and merge |
git push |
Upload remote code and merge |
git submodule |
Manage projects that contain other Git repositories |
Git File Status
Git file statuses are divided into three types: Working Directory, Staging Area, and Local Repository. Understanding these concepts and how they interact is the key to mastering Git.
Working Directory
The working directory is the project files you see on your local computer. It is where you actually operate on files, including viewing, editing, deleting, and creating files. All changes to files first occur in the working directory.
Files in the working directory may have the following statuses:
- Untracked: Newly created files that are not recorded by Git.
- Modified: Files already tracked by Git have been changed, but these changes have not yet been committed to Git's records.
Staging Area
The staging area, also called the Index, is a temporary storage area used to hold changes that are about to be committed to the local repository. You can selectively add changes from the working directory to the staging area, so you can commit changes to multiple files at once without having to commit all file changes.
- Use
git addThe command adds files from the working directory to the staging area. - Use
git add .The command adds all changes in the current directory to the staging area.
git add # 添加指定文件到暂存区
git add . # 添加所有更改到暂存区
Local Repository
The local repository is a database hidden in the .git directory, used to store all the commit history of the project. Each time you commit changes, Git saves the contents of the staging area to the local repository.
Use git commit -m "commit message" The command commits the changes in the staging area to the local repository.
git commit -m "commit message" # 提交暂存区的更改到本地仓库
File Status Transition Flow
Untracked: Newly created files are initially untracked. They exist in the working directory but are not tracked by Git.
touch newfile.txt # 创建一个新文件
git status # 查看状态,显示 newfile.txt 未跟踪
Tracked: By using the git add command to add untracked files to the staging area, the files become tracked.
git add newfile.txt # 添加文件到暂存区
git status # 查看状态,显示 newfile.txt 在暂存区
Modified: After changes are made to tracked files, these changes are shown as modified, but they have not yet been added to the staging area.
echo "Hello, World!" > newfile.txt # 修改文件
git status # 查看状态,显示 newfile.txt 已修改
Staged: Use git add After the command adds the modified files to the staging area, the files enter the staged state, waiting to be committed.
git add newfile.txt # 添加文件到暂存区
git status # 查看状态,显示 newfile.txt 已暂存
Committed: Use git commit After the command commits the changes in the staging area to the local repository, these changes are recorded and the file status returns to tracked.
git commit -m "Added newfile.txt" # 提交更改
git status # 查看状态,工作目录干净