Skip to content
On this page

Git 基本操作

本章详细介绍Git的基本操作命令,包括文件操作、提交管理、历史查看等常用功能。

初始化仓库

创建新仓库

bash
# 在当前目录初始化Git仓库
git init

# 创建新目录并初始化Git仓库
git init my-project
cd my-project

# 初始化裸仓库(用于共享仓库)
git clone --bare /path/to/repo

克隆现有仓库

bash
# 克隆远程仓库
git clone https://github.com/username/repository.git

# 克隆到指定目录
git clone https://github.com/username/repository.git my-dir

# 克隆特定分支
git clone -b branch-name https://github.com/username/repository.git

# 浅克隆(只获取最近的提交历史)
git clone --depth 1 https://github.com/username/repository.git

# 克隆特定标签
git clone --branch v1.0.0 https://github.com/username/repository.git

文件操作

查看文件状态

bash
# 查看仓库状态
git status

# 查看简短格式的状态
git status -s

# 查看特定文件状态
git status -- myfile.txt

# 查看详细状态信息
git status -v

添加文件到暂存区

bash
# 添加单个文件
git add filename.txt

# 添加多个文件
git add file1.txt file2.txt

# 添加所有文件
git add .

# 添加特定类型文件
git add *.js

# 添加特定目录下的所有文件
git add src/

# 交互式添加(选择性添加部分更改)
git add -p

# 添加所有更改的文件(包括删除的文件)
git add -A

# 添加修改和新建的文件(不包括删除的文件)
git add -u

查看文件差异

bash
# 查看工作区与暂存区的差异
git diff

# 查看暂存区与最后一次提交的差异
git diff --staged
git diff --cached

# 查看工作区与指定提交的差异
git diff HEAD~1

# 查看特定文件的差异
git diff myfile.txt

# 查看特定提交间的差异
git diff commit1..commit2

# 查看统计信息(增删行数)
git diff --stat

# 查看简短统计
git diff --shortstat

提交操作

创建提交

bash
# 提交暂存区的更改
git commit -m "提交信息"

# 提交所有更改(包括跟踪的文件)
git commit -am "提交信息"

# 添加并提交(添加所有跟踪的文件)
git commit -a -m "提交信息"

# 提交时包含特定文件
git commit filename.txt -m "提交信息"

# 提交到特定分支
git commit -m "提交信息" -o

# 提交时跳过测试(在有pre-commit钩子时)
git commit --no-verify -m "提交信息"

# 提交时添加GPG签名
git commit -S -m "提交信息"

修改提交

bash
# 修改最后一次提交
git commit --amend -m "新的提交信息"

# 修改最后一次提交(保持原提交信息)
git commit --amend --no-edit

# 修改最后一次提交并添加文件
git add newfile.txt
git commit --amend

# 修改提交时间戳
git commit --amend --date="2023-01-01 12:00:00"

查看提交历史

bash
# 查看提交历史
git log

# 查看简洁的提交历史
git log --oneline

# 查看图形化的分支历史
git log --graph --oneline --all

# 查看指定数量的提交
git log -n 5

# 查看特定文件的提交历史
git log -- filename.txt

# 查看作者的提交历史
git log --author="作者名"

# 查看包含特定文本的提交
git log --grep="关键字"

# 查看指定日期范围的提交
git log --since="2023-01-01" --until="2023-12-31"

# 查看统计信息
git log --stat

# 查看差异统计
git log --shortstat

# 查看图形化统计
git log --graph

撤销操作

撤销工作区更改

bash
# 撤销单个文件的更改
git checkout -- filename.txt

# 撤销所有文件的更改
git checkout .

# 撤销特定路径的更改
git checkout -- src/

# 使用switch命令撤销(Git 2.23+)
git restore filename.txt
git restore .

撤销暂存区更改

bash
# 取消暂存单个文件
git reset HEAD filename.txt

# 取消暂存所有文件
git reset HEAD

# 使用restore命令取消暂存(Git 2.23+)
git restore --staged filename.txt
git restore --staged .

撤销提交

bash
# 软重置(保留更改在暂存区)
git reset --soft HEAD~1

# 混合重置(保留更改在工作区,默认)
git reset --mixed HEAD~1

# 硬重置(丢弃所有更改)
git reset --hard HEAD~1

# 重置到指定提交
git reset --hard commit-hash

# 重置特定文件到指定提交
git checkout commit-hash -- filename.txt

查看和比较

查看对象内容

bash
# 查看提交详情
git show commit-hash

# 查看特定文件在特定提交时的内容
git show commit-hash:filename.txt

# 查看标签信息
git show tag-name

# 查看分支信息
git show branch-name

比较操作

bash
# 比较工作区和暂存区
git diff

# 比较暂存区和HEAD
git diff --cached

# 比较工作区和指定提交
git diff HEAD~1

# 比较两个提交
git diff commit1..commit2

# 比较两个分支
git diff branch1..branch2

标签操作

创建标签

bash
# 创建轻量标签
git tag v1.0.0

# 创建附注标签
git tag -a v1.0.0 -m "版本1.0.0"

# 创建标签并指定提交
git tag -a v1.0.0 commit-hash -m "版本1.0.0"

# 创建签名标签
git tag -s v1.0.0 -m "签名版本标签"

管理标签

bash
# 列出所有标签
git tag

# 列出匹配模式的标签
git tag -l "v1.*"

# 删除标签
git tag -d v1.0.0

# 查看标签信息
git show v1.0.0

# 推送标签到远程
git push origin v1.0.0
git push origin --tags

# 删除远程标签
git push origin --delete v1.0.0

远程仓库操作

管理远程仓库

bash
# 查看远程仓库
git remote -v

# 添加远程仓库
git remote add origin https://github.com/username/repo.git

# 重命名远程仓库
git remote rename old-name new-name

# 移除远程仓库
git remote remove name

# 修改远程仓库URL
git remote set-url origin https://github.com/username/new-repo.git

# 查看远程仓库信息
git remote show origin

# 删除远程仓库
git remote remove origin

推送和拉取

bash
# 推送到远程仓库
git push origin main

# 推送新分支到远程
git push -u origin new-branch

# 推送所有分支
git push --all origin

# 推送标签
git push origin --tags

# 强制推送(谨慎使用)
git push --force origin main
git push --force-with-lease origin main

# 拉取远程更新
git pull origin main

# 获取远程更新(不合并)
git fetch origin

# 获取所有远程更新
git fetch --all

# 拉取特定标签
git fetch origin tag v1.0.0

实用技巧

临时存储

bash
# 临时存储当前更改
git stash

# 临时存储并添加描述
git stash push -m "工作进行中"

# 查看存储列表
git stash list

# 应用最近的存储
git stash pop

# 应用特定存储
git stash apply stash@{0}

# 查看存储差异
git stash show

# 删除存储
git stash drop stash@{0}
git stash clear  # 清空所有存储

清理操作

bash
# 查看未跟踪的文件
git clean -n

# 删除未跟踪的文件
git clean -f

# 删除未跟踪的目录
git clean -fd

# 交互式清理
git clean -i

# 删除忽略的文件
git clean -X

搜索和过滤

bash
# 搜索提交信息中的文本
git log --grep="关键字"

# 搜索文件内容中的文本
git log -S "搜索内容"

# 搜索特定路径的更改
git log -- path/to/file

# 按作者搜索
git log --author="作者名"

# 按日期搜索
git log --since="2023-01-01" --until="2023-12-31"

配置和信息

配置操作

bash
# 设置用户信息
git config --global user.name "Your Name"
git config --global user.email "email@example.com"

# 设置编辑器
git config --global core.editor "code --wait"

# 设置行结束符处理
git config --global core.autocrlf true

# 查看配置
git config --list
git config user.name

获取仓库信息

bash
# 查看仓库信息
git config --get remote.origin.url

# 查看分支信息
git branch -v

# 查看远程分支
git branch -r

# 查看所有分支
git branch -a

# 查看当前分支
git branch --show-current

这些基本操作涵盖了日常Git使用中的大部分场景,熟练掌握这些命令可以大大提高工作效率。