Git分布式管理系统命令指南
一直依赖GitHub Desktop客户端的便捷操作,对git的命令越来越生疏了,现在将常用命令总结一下,我会按照我们创建本地仓库的流程和日常维护的案例长期完善、补充这篇文章的知识点。
- git clone
将远程仓库的代码克隆到本地。
// 该命令会在本地主机生成一个目录,与远程主机的版本库同名
git clone https://github.com/mengft/Boutique.git
// 如果要指定不同的目录名,可以将目录名作为git clone命令的第二个参数。
git clone <版本库的网址> <本地目录名>
git clone https://github.com/mengft/Boutique.git LocalBoutique
git clone支持多种协议,除了HTTP(s)以外,还支持SSH、Git、本地文件协议等,下面是一些例子。
$ git clone http[s]://example.com/path/to/repo.git
$ git clone http://git.oschina.net/yiibai/sample.git
$ git clone ssh://example.com/path/to/repo.git
$ git clone git://example.com/path/to/repo.git
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git
$ git clone rsync://example.com/path/to/repo.git
- git remote
- git remote
- git remote -v
- git remote add
- git remote remove
- git remote set-url
git remote 查看远程仓库的信息
git remote -v 查看远程仓库的详细信息
有同学会说,我的远程仓库名字不是Boutique吗?为什么得到的是origin,其实origin只是远程仓库的简写或者引用而已。
git remote add <name> <url>
TODO: git remote命令的意义是什么?
git remote remove <name> 删除远程仓库的关联
git remote set-url <name> <newurl> 修改远程仓库关联
- git branch
- git branch
- git branch -r
- git branch -a
- git branch
- git branch -d
- git branch -vv
- git branch -m
// 不带参数:列出本地已经存在的分支,并且在当前分支的前面用"*"标记
git branch
// 查看远程版本库分支列表
git branch -r
// 查看所有分支列表,包括本地和远程
git branch -a
// 创建名为<name>的本地分支
git branch <name>
// 删除<name>分支,如果在分支中有一些未merge的提交,那么会删除分支失败,此时可以使用 git branch -D <name>:强制删除dev分支
git branch -d <name>
// 可以查看本地分支对应的远程分支
git branch -vv
// 分支重命名
git branch -m <oldName> <newName>
- git checkout
- 操作分支
- 操作文件
// 切换分支
git checkout <name>
// 切换分支,如果分支不存在则创建分支并完成切换
git checkout -b <name>
// 放弃单个文件的修改
git checkout <filename>
// 放弃当前目录的修改
git checkout .
- git fetch
git fetch <远程仓库名> <远程分支名>:<本地分支名>
// 拉取远程分支上的本地指向分支xxx,到origin/xxx
git fetch
// 不设置分支时默认问为master分支
git fetch origin
git merge origin/master
// 拉取origin远程仓库的master分支到origin/master,origin/master是虚拟分支,使用git branch -a 是查看不到的
git fetch origin master
git merge origin/master
// 拉取origin远程仓库的master分支到本地的temp分支(一般是一个未创建的分支,会在本地创建一个temp的分支,做分支merge暂存使用,这个分支是真实存在的,可以通过git branch查到)
git fetch origin master:temp
git merge temp
- git pull
git pull <远程主机名> <远程分支名>:<本地分支名>
// 如果当前分支只有一个追踪关系
git pull
// 如果当前分支与远程分支存在追踪关系
git pull <远程主机名>
// 如果与当前分支合并,则可省略本地分支名
git pull <远程主机名> <远程分支名>
// 拉取origin远程仓库的master分支并与本地的mengft分支进行merge
git pull origin master:mengft
// 手动建立追踪关系
git branch --set-upstream master origin/next
// 取消对master的跟踪
git branch --unset-upstream master
// 清理远程已删除本地还存在的分支
git fetch --prune origin
- git tag
// 列出当前仓库的所有标签
git tag
// 列出符合模式的标签,*为标识符
git tag -l 'v0.1.*'
// 查看标签版本信息
git show <tagName>
// 删除标签
git tag -d <tagName>
// 创建轻量标签
git tag v0.2.0 -light
// 创建附注标签
解释:参数-a即annotated的缩写,指定标签类型,后附标签名。参数m指定标签说明,说明信息会保存在标签对象中。
git tag -a <tagName> -m <tagDescription>
git tag -a release-1.7.0 -m "1.7.0 release version"
// 补打标签(给指定的commit打标签)
解释:打标签不必要在HEAD之上,也可在之前的版本上打,这需要你知道某个提交对象的校验和,通过git log命令获取。
git tag -a <tagName> 49e0cd22f6bd9510fe65084e023d9c4316b446a6
// 发布标签到远程仓库
git push <远程主机名> <tagName>
git push origin release-1.7.0
// 将本地所有标签一次性提交到git服务器
git push <远程主机名> --tags
git push origin --tags
-
git push
- 分支处理
// 将本地的master分支推送到origin主机的master分支。如果master不存在,则会被新建 git push <远程主机名> <本地分支名>:<远程分支名> // 将本地的master分支推送到origin主机的master分支。如果master不存在,则会被新建(本地分支创建完成之后push到远程分支) git push origin master // 删除远程分支 git push <远程主机名> :<远程分支名> git push origin :test git push origin --delete test // 如果当前分支与远程分支之间存在追踪关系,则本地分支和远程分支都可以省略。 git push origin // 将当前分支推送到远程的同名的简单方法 git push origin HEAD
- 标签处理
// 将本地tag推送到远程主机 git push <远程主机名> <tagName> git push origin release-1.7.0 // 将本地tag全部推送到远程主机 git push origin --tags // 将当前分支推送到源存储库中的远程引用匹配主机。 这种形式方便推送当前分支,而不考虑其本地名称 git push origin HEAD:master