1
This commit is contained in:
10
git/git操作.md
10
git/git操作.md
@@ -1,11 +1,13 @@
|
|||||||
1. 初始化仓库
|
1. 初始化仓库
|
||||||
git init # 当前目录初始化为Git仓库
|
git init # 当前目录初始化为Git仓库
|
||||||
git clone <repo_url> # 克隆远程仓库到本地
|
git clone <repo_url> # 克隆远程仓库到本地
|
||||||
|
|
||||||
2. 查看状态与日志
|
2. 查看状态与日志
|
||||||
git status # 查看工作区/暂存区状态
|
git status # 查看工作区/暂存区状态
|
||||||
git log # 查看提交历史
|
git log # 查看提交历史
|
||||||
git log --oneline # 简洁版提交历史
|
git log --oneline # 简洁版提交历史
|
||||||
git log -p # 显示历史提交的代码差异
|
git log -p # 显示历史提交的代码差异
|
||||||
|
|
||||||
3. 添加与提交
|
3. 添加与提交
|
||||||
git add <file> # 添加文件到暂存区
|
git add <file> # 添加文件到暂存区
|
||||||
git add . # 添加所有修改到暂存区
|
git add . # 添加所有修改到暂存区
|
||||||
@@ -21,6 +23,7 @@
|
|||||||
git merge <branch> # 合并指定分支到当前分支
|
git merge <branch> # 合并指定分支到当前分支
|
||||||
git branch -d <branch> # 删除分支(已合并)
|
git branch -d <branch> # 删除分支(已合并)
|
||||||
git branch -D <branch> # 强制删除分支(未合并)
|
git branch -D <branch> # 强制删除分支(未合并)
|
||||||
|
|
||||||
2. 远程分支
|
2. 远程分支
|
||||||
git fetch # 拉取远程最新元数据(不自动合并)
|
git fetch # 拉取远程最新元数据(不自动合并)
|
||||||
git pull # 拉取远程代码并合并(= fetch + merge)
|
git pull # 拉取远程代码并合并(= fetch + merge)
|
||||||
@@ -32,9 +35,11 @@
|
|||||||
1. 撤销工作区修改
|
1. 撤销工作区修改
|
||||||
git restore <file> # 撤销工作区的修改(未add)
|
git restore <file> # 撤销工作区的修改(未add)
|
||||||
git checkout -- <file> # 同上(旧版写法)
|
git checkout -- <file> # 同上(旧版写法)
|
||||||
|
|
||||||
2. 撤销暂存区修改
|
2. 撤销暂存区修改
|
||||||
git restore --staged <file> # 将文件移出暂存区(保留工作区修改)
|
git restore --staged <file> # 将文件移出暂存区(保留工作区修改)
|
||||||
git reset HEAD <file> # 同上(旧版写法)
|
git reset HEAD <file> # 同上(旧版写法)
|
||||||
|
|
||||||
3. 回退提交
|
3. 回退提交
|
||||||
git reset --soft HEAD^ # 回退到上一提交(保留修改到暂存区)
|
git reset --soft HEAD^ # 回退到上一提交(保留修改到暂存区)
|
||||||
git reset --hard HEAD^ # 彻底回退到上一提交(丢弃所有修改)
|
git reset --hard HEAD^ # 彻底回退到上一提交(丢弃所有修改)
|
||||||
@@ -46,24 +51,29 @@
|
|||||||
git remote add <name> <url> # 添加远程仓库
|
git remote add <name> <url> # 添加远程仓库
|
||||||
git remote remove <name> # 删除远程仓库
|
git remote remove <name> # 删除远程仓库
|
||||||
git push origin --delete <branch> # 删除远程分支
|
git push origin --delete <branch> # 删除远程分支
|
||||||
|
|
||||||
五、高级操作
|
五、高级操作
|
||||||
1. 储藏临时修改
|
1. 储藏临时修改
|
||||||
git stash # 储藏当前工作区修改
|
git stash # 储藏当前工作区修改
|
||||||
git stash pop # 恢复最近储藏的修改
|
git stash pop # 恢复最近储藏的修改
|
||||||
git stash list # 查看所有储藏记录
|
git stash list # 查看所有储藏记录
|
||||||
|
|
||||||
2. 标签管理
|
2. 标签管理
|
||||||
git tag # 查看所有标签
|
git tag # 查看所有标签
|
||||||
git tag v1.0.0 # 创建轻量标签
|
git tag v1.0.0 # 创建轻量标签
|
||||||
git tag -a v1.0.0 -m "msg" # 创建附注标签
|
git tag -a v1.0.0 -m "msg" # 创建附注标签
|
||||||
git push origin --tags # 推送所有标签到远程
|
git push origin --tags # 推送所有标签到远程
|
||||||
|
|
||||||
3. 变基(Rebase)
|
3. 变基(Rebase)
|
||||||
git rebase <branch> # 将当前分支变基到目标分支
|
git rebase <branch> # 将当前分支变基到目标分支
|
||||||
git rebase --abort # 终止变基
|
git rebase --abort # 终止变基
|
||||||
git rebase --continue # 解决冲突后继续变基
|
git rebase --continue # 解决冲突后继续变基
|
||||||
|
|
||||||
六、配置相关
|
六、配置相关
|
||||||
git config --global user.name "Your Name" # 设置全局用户名
|
git config --global user.name "Your Name" # 设置全局用户名
|
||||||
git config --global user.email "email@example.com" # 设置邮箱
|
git config --global user.email "email@example.com" # 设置邮箱
|
||||||
git config --list # 查看所有配置
|
git config --list # 查看所有配置
|
||||||
|
|
||||||
七、文件忽略规则
|
七、文件忽略规则
|
||||||
创建 .gitignore 文件,添加需忽略的文件/目录:
|
创建 .gitignore 文件,添加需忽略的文件/目录:
|
||||||
# 示例:
|
# 示例:
|
||||||
|
|||||||
Reference in New Issue
Block a user