git基本命令
- 版本控制系统
-
- 中央式版本控制系统(VCS)
- 分布式版本控制系统(DVCS)
- git 原理
-
- 三个基本概念
- github ssh密钥添加
- 克隆下拉
- 添加和提交
- 推送改动
- 分支
- 更新与合并
-
- git fetch 和 git pull的区别
- 回退
- 清除untracked files
- 查看各分支关系
- 标签或其他
参考
图解git原理与日常实用指南
git简明教程
版本控制系统
三个核心:
- 版本控制
- 主动提交(commit历史)
- 远程仓库(协同开发)
中央式版本控制系统(VCS)
分布式版本控制系统(DVCS)
分布式与中央式的区别主要在于,分布式除了远程仓库之外,团队中每一个成员的机器上都有一份本地仓库,每个人在自己的机器上就可以做基本操作。
git 原理
三个基本概念
- 工作区: 本地工作目录
-
版本库: .git, git的本地版本库,所有版本信息都会存在这里 git commit
HEAD指向最后一次提交
- 暂存区: .git/index 缓存区,临时保存改动 git add
github ssh密钥添加
参考文章
克隆下拉
git clone -b branchname ssh://...
添加和提交
// 添加到暂存区
git add <filename>
git add .
// 提交改动
git commit -m "commit message"
// 更改最后一次提交
git commit --amend
推送改动
// 提交到远端仓库
// origin为远端仓库 master为远端仓库的分支
git push origin master
// 将仓库连接到远程服务器,并命名为origin
git remote add origin <server>
分支
// 创建分支
git branch <分支名>
// 切换分支
git checkout <分支名>
// 创建并切换分支
git checkout -b <分支名>
// 删除分支
git branch -d <分支名>
// 推送本地分支到远端仓库
git push origin <分支名>
更新与合并
// 更新本地仓库到最新改动,从建立本地分支时的远程仓库分支(upstream分支)
git pull
// 从指定远程仓库
git pull <仓库名> <分支名>
// 合并其他分支到当前分支
git merge <其他分支名>
// 比较分支差异
git diff <source branch> <target branch>
// 默认比较working directory 相对于其他节点的变换
git diff <branch name>
git diff HEAD
git diff //工作区对于Index暂存区的变化
git fetch 和 git pull的区别
#fetch
// 下拉远端指定分支的更新到版本库
// 取回的更新名字 远端服务器/分支,例origin/dev
git fetch <远端服务器> <分支>
// 默认取回origin服务器的所有分支更新,方便查看其他人的进度
git fetch
git fetch origin
#pull
git pull = git fetch + git merge FETCH_HEAD
回退
reset和checkout区别
// checkout除切换分支的功能,还可用于从历史提交/暂存区拷贝文件到工作目录
git checkout HEAD~ foo.c
git checkout <commitID> files
// Reset命令把当前branch/HEAD指向另一个位置,有选择的变动工作区和暂存区
git reset HEAD // Index回滚到最后一次提交
清除untracked files
// 查看要清除的文件及目录
git clean -nfd
//清除文件及目录
git clean -fd
查看各分支关系
git log --graph --decorate --oneline --simplify-by-decoration --all
标签或其他
git log
git status