https://git-scm.com/official website to explain the blog http://www.ruanyifeng.com/blog/2014/06/git_remote.htmlgit: Common commands 1, git clone from the remote service Repository Clone (1) build directory with remote directory same name
$ git clone <版本库的网址>
$ git clone https://github.com/jquery/jquery.git
(2)生成目录与远程目录不同名称
$ git clone <版本库的网址> <本地目录名>
(3)git clone 支持多种协议 除了HTTP(s)以外,还支持SSH、Git、本地文件协议等,下面是一些例子。
$ git clone http[s]://example.com/path/to/repo.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/
SSH协议还有另一种写法。
$ git clone [[email protected]]example.com:path/to/repo.gi
通常来说,Git协议下载速度最快,SSH协议用于需要用户认证的场合。各种协议优劣的详细讨论请参考官方文档。
(4)克隆版本库的时候,所使用的远程主机自动被Git命名为origin。如果想用其他的主机名,需要用git clone命令的-o选项指定。
$ git clone -o jQuery https://github.com/jquery/jquery.git $ git remote jQuery (命令表示,克隆的时候,指定远程主机叫做jQuery。)
2、git remote 为了便于管理,Git要求每个远程主机都必须指定一个主机名。
(1)git remote命令就用于管理主机名。
带选项的时候,git remote命令列出所有远程主机。
$ git remote origin
(2) git remote -v选项,可以参看远程主机的网址。
$ git remote-v origin [email protected" . Com:jquery/jquery.git (Fetch) (only a remote host, called Origin, and its URL.) Origin [email protected] . Com:jque Ry/jquery.git (push)
(3) $ git remote show < hostname > View details of this host.
(4 ) $ git remote add < host name > < URL; Add remote host.
(5 ) $ git remote rm < Host name > Remove hostname
(6) $ git remote rename < original hostname > < new host name; Restart name
3, git fetch once the repository for the remote host has been updated (the GIT term is called commit), you need to retrieve the updates locally, then use the git fetch command.
(1) $ git Fetch < remote host name;
updates a remote host to retrieve all branches (branch) updates (by default)
(2)$ git fetch <远程主机名> <分支名> 取回特定分支的更新,指定分支名。($ git fetch origin master)
(3)所取回的更新,在本地主机上要用"远程主机名/分支名"的形式读取。比如origin主机的master,就要用origin/master读取。
(4)git branch命令的-r选项,可以用来查看远程分支,-a选项查看所有分支。
$ git branch -r 可以用来查看远程分支,-a选项查看所有分支。
$ git branch -r origin/master $ git branch -a * master (本地分支) remotes/origin/master(远程分支)
(5)取回远程主机的更新以后,可以在它的基础上,使用git checkoutcommand to create a new branch.
$ git checkout -b newBrach origin/master
(6)此外,也可以使用git merge命令或者git rebase命令,在本地分支上合并远程分支。
$ git merge origin/master # 或者 $ git rebase origin/master
4、git pull git pull命令的作用是,取回远程主机某个分支的更新,再与本地的指定分支合并。它的完整格式稍稍有点复杂。
(1) $ git pull <远程主机名> <远程分支名>:<本地分支名>
比如,取回origin主机的next分支,与本地的master分支合并,需要写成下面这样。
$ git pull orgin next:master
如果远程分支是与当前分支合并,则冒号后面的部分可以省略。
$ git pull orgin next
上面命令表示,取回origin/next分支,再与当前分支合并。实质上,这等同于先做git fetch,再做git merge。
(2)在某些场合,Git会自动在本地分支与远程分支之间,建立一种追踪关系(tracking)。比如,在git clone的时候,
所有本地分支默认与远程主机的同名分支,建立追踪关系,也就是说,本地的master分支自动"追踪"origin/master分支。
Git也允许手动建立追踪关系。 git branch --set-upstream master origin/next (上面命令指定master分支追踪origin/next分支.)
(3)如果当前分支与远程分支存在追踪关系,git pull就可以省略远程分支名。
$ git pull origin 就可以拉下来当前分支对应的远端分支内容
上面命令表示,本地的当前分支自动与对应的origin主机"追踪分支"(remote-tracking branch)进行合并。
(4)如果当前分支只有一个追踪分支,连远程主机名都可以省略。
$ git pull 上面命令表示,当前分支自动与唯一一个追踪分支进行合并。
(5)如果合并需要采用rebase模式,可以使用--rebase选项。
$ git pull --rebase <远程主机名> <远程分支名>:<本地分支名>
如果远程主机删除了某个分支,默认情况下,git pull 不会在拉取远程分支的时候,删除对应的本地分支。
这是为了防止,由于其他人操作了远程主机,导致git pull不知不觉删除了本地分支。
-p 就会在本地删除远程已经删除的分支。
$ git pull -p # 等同于下面的命令 $ git fetch --prune origin $ git fetch -p
5、git push 将本地分支的更新,推送到远端分支(1)
$ git push <远程主机名> <本地分支名>:<远程分支名>
git pullis < remote branch >:< local branch;
git pushis < local branch >:< remote branch >.
$ git push orgin master:master(2)
$ git push origin master 省略远程分支,直接推送到追踪分支,如果远程分支不存在会被重新创建(3) $ git push orgin means that the current branch is pushed to the branch of the tracing relationship
(4)$ git push 当前分支只有一个追踪分支(所有远程主机中)
如果当前分支与多个主机存在追踪关系,则可以使用-u选项指定一个默认主机,这样后面就可以不加任何参数使用git push。
$ git push -u origin master
(5)不带任何参数的git push,默认只推送当前分支,这叫做simple方式。此外,还有一种matching方式,会推送所有有
对应的远程分支的本地分支。Git 2.0版本之前,默认采用matching方法,现在改为默认采用simple方式。
如果要修改这个设置,可以采用git config命令 $ git config --global push.default matching # 或者 $ git config --global push.default simple
(6)还有一种情况,就是不管是否存在对应的远程分支,将本地的所有分支都推送到远程主机,这时需要使用--all option.
$ git push --all origin(上面命令表示,将所有本地分支都推送到Origin host. )
(7) If the version of the remote host is newer than the local version, Git will give an error when pushing, and ask to do it locally first
git pullMerge the differences and then push to the remote host. At this point, if you must push, you can use--forceOptions.
$ git push --force origin (这种提交不会出现在分支图中)
上面命令使用The--force option results in an overwritten version of the update on the remote host. Unless you are sure you want to do this, you should try to avoid using--forceOptions.
(7) Finally,git pushdoes not push tags (tag) unless you use the--tagsOptions.
$ git push origin --tags
(8)如果省略本地分支名,则表示删除指定的远程分支,因为这等同于推送一个空的本地分支到远程分支。
$ git push origin :master (表示删除origin主机的master分支.) # 等同于 $ git push origin --delete master
Git remote operation detailed