Git命令详解
- 简介
- 什么是Git
- Git的优势
- Git的工作原理
配置Git
git config
- 配置用户信息
git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
- 查看配置信息
git config --list
- 配置用户信息
初始化仓库
git init
- 初始化一个新的Git仓库
克隆仓库
git clone
- 克隆一个远程仓库
git clone <repository-url>
- 克隆一个远程仓库
添加文件到暂存区
git add
- 添加指定文件到暂存区
git add <file>
- 添加所有文件到暂存区
git add .
- 添加指定文件到暂存区
提交更改
git commit
- 提交暂存区的更改
git commit -m "commit message"
- 提交暂存区的更改
查看状态
git status
- 查看当前仓库的状态
查看提交历史
git log
- 查看提交日志
- 常用选项
--oneline
:简洁日志格式--graph
:图形化显示分支和版本演进
撤销更改
git checkout
- 撤销工作区的修改
git checkout -- <file>
- 撤销工作区的修改
git reset
- 将暂存区的修改撤销
git reset HEAD <file>
- 将提交的修改撤销
git reset --hard <commit-hash>
- 将暂存区的修改撤销
分支管理
git branch
- 查看分支列表
- 创建分支
git branch <branch-name>
- 删除分支
git branch -d <branch-name>
git checkout
- 切换到指定分支
git checkout <branch-name>
- 创建并切换到新分支
git checkout -b <branch-name>
- 切换到指定分支
git merge
- 合并指定分支到当前分支
git merge <branch-name>
- 合并指定分支到当前分支
远程仓库操作
git remote
- 查看远程仓库列表
- 添加远程仓库
git remote add <remote-name> <repository-url>
- 删除远程仓库
git remote remove <remote-name>
git fetch
- 获取远程仓库的更新
git fetch <remote-name>
- 获取远程仓库的更新
git pull
- 拉取远程仓库的更新并合并到本地分支
git pull <remote-name> <branch-name>
- 拉取远程仓库的更新并合并到本地分支
git push
- 推送本地分支的更新到远程仓库
git push <remote-name> <branch-name>
- 推送本地分支的更新到远程仓库
标签管理
git tag
- 查看标签列表
- 创建标签
- 轻量级标签:
git tag <tag-name>
- 带附注的标签:
git tag -a <tag-name> -m "tag message"
- 轻量级标签:
- 删除标签
git tag -d <tag-name>
git push
- 推送标签到远程仓库
git push <remote-name> <tag-name>
- 推送标签到远程仓库