站内链接:

Intro

Repository and Branch

理解仓库和分支的区别:

  • repository: 某一个指定路径的代码集合, 本地仓库, 远程仓库
  • branch: 某一个提交对象往回看的历史, commit 对象上的一个指针

Origin

origin 就是一个名字,它是在你 clone 一个托管在 Github 上代码库时,git 为你默认创建的指向这个远程代码库的标签.origin 指向的是 repository,master 只是这个 repository 中默认创建的第一个 branch. 执行命令git clone时 git 做了如下的操作:

    1. 将远程主机命名为origin, 这个比较重要, 有助于理解
    1. 创建一个指向 master 分支的指针并将其命名为origin/master, 实际指向remotes/origin/HEAD -> origin/master
    1. 在本地创建 master 分支, 指向origin/master

上述的步骤可以通过git branch -a的分支指向了解大概. 另外查看远程仓库信息: git remote -v, 另外关于 fetch 的更详细信息见git-fetch说明.

Clone Process

Main Handle

所有的 git 都是围绕三大步骤来完成:

  • clone: 涉及 remote server, remote repository, remote branch
  • update code: 涉及 local repository, local branch
  • push: 涉及 remote server, remote repository, remote branch

其中 clone 会根据 remote server/branch 拷贝整个仓库到本地, 在 push 之前 local repository 和 remote repository 是相对独立的个体.

Branch

利用命令: git branch -a 可以发现, 实际上 master 指向 local branch, origin/master 指向 remote branch, 在本地两者的指针指向同一批文件:

1
2
git diff origin/master remotes/origin/master
# 其中master就是一个"refspec", 格式: "+<src>:<dst>", src表示local branch, dst表示remote branch, 默认情况下省略dst.

Add

在本地添加远程仓库:

格式:git remote add

说明:shortname 标识远程服务器名,url 标识路径(最好使用 ssh 方式)

例子:git remote add origin git@github.com:unlessbamboo/vim.git

将本地的远程仓库推送到服务器版本库中

格式:git pull 【show-name】 master

例子:git pull test1 master

Push/Pull

push

命令:git push 【short-name】 【本地分支名称】

例子:git push origin master

pull

命令:git pull 【short-name】 【本地分支名称】

例子:git pull origin master

PS:注意仓库和分支的区别

删除

rm

命令: git rm filename

说明: 将文件从暂存区-staged 中清除

force rm

命令: git rm -f file1

说明: 文件 file1 已经添加到本地 staged 中, 但是没有推送到 server 的快照中, 该功能一般不会使用, 也不应该使用.

cached

命令: git rm –cached file2

说明: 将文件从 staged 中删除, 但是不删除磁盘上的文件, 这个功能非常有用.

Update

url 路径

命令:git remote set-url 【short-name】 【newURL】

例子:git remote set-url origin git@github.com:unlessbamboo/vim.git

delete

命令:git remote rm 【short-name】

说明:该命令一般用于用户不想使用特定的镜像或者贡献者不再贡献时使用, 注意, 这里是删除 repository, 并非删除 branch.

rename

命令:git remote rename [old-name] [new-name]

例子:git remote rename test1 test2

Get

get remote

命令:git remote

说明:一般默认的远程仓库服务器名字都是 origin

get remote url

命令:git remote -v

说明:不同的 URL 上可以设置不同的权限,另外 HTTPS 的 URL 方式在每次 push 都会提示密码登陆,这个问题很重要。

ps: 用于辨别 HTTPS/SSH,非常重要

get remote info

命令:git remote show [remote-name]

说明: 获取远程分支的详细信息: URL 信息、当前分支详细信息、合并情况, 非常重要

参考: