Git基本命令和操作流程
%%{init: {'flowchart':{'curve':'basis'}}}%%
flowchart LR
%% ====== Nodes ======
subgraph Local["本地仓库"]
W["工作目录
(Working Directory)"]
S["暂存区/缓存区
(Index/Stage)"]
L["本地库/版本库
(.git)"]
end
subgraph Remote["远程仓库"]
R["远程仓库
(GitHub/Gitee等)"]
end
%% ====== Flows ======
R -- clone/fetch --> L
W -- add --> S
S -- commit --> L
L -- push --> R
L -- checkout --> W
R -- pull=fetch+merge--> W
克隆仓库
将远程仓库复制到本地,得到本地仓库(.git)和一份对应的工作目录。
git clone https://github.com/git/git.git
分支管理
- 查看分支
git branch:查看本地分支git branch -r:查看远程分支git branch -a:查看所有分支git status:查看当前分支是否有未提交修改
- 创建/切换分支
git branch <name>:创建新分支git checkout <name>:切换分支git checkout -b <name>:创建并切换分支
- 重命名/删除分支
git branch -m <old> <new>:重命名分支git branch -d <name>:安全删除分支(已合并才删)git branch -D <name>:强制删除分支
- 合并分支
git merge <name>:将某分支合并到主分支git revert <commit>:在分支上安全回滚git revert HEAD:撤销前一次commit操作git revert HEAD^n