Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Git Basic Operations

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:

A simple operation procedure:

$ git init    
$ git add .    
$ git commit  

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:

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.

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                         # 查看状态,工作目录干净