美文网首页
Git - 5.远程仓库

Git - 5.远程仓库

作者: blurryssky | 来源:发表于2016-04-02 14:26 被阅读486次

已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步

创建远程仓库

登陆GitHub

add repository.png

名字填写为gittest,点击Create repository

推送分支git push

git push.png

英语好的同学自行翻译一下,我就不多做叙述了

这里我们用下面箭头标示的方法,用终端推送一个存在的仓库
首先选择SSH,然后进入我们的gittest本地仓库

cd /Users/blurryssky/Desktop/iOS/gittest 

执行以下命令,直接从上面图里的位置复制过来

git remote add origin git@github.com:blurryssky/gittest.git 
git push -u origin master

注意,如果本地git仓库里什么文件都没有,需要先创建一些文件并且提交

SSH警告

当你第一次使用Git的git clone或者git push命令连接GitHub时,会得到一个警告

The authenticity of host 'github.com (192.30.252.130)'
can't be established.
RSA key fingerprint is
SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?

输入yes即可

push success.png

看到这样的界面代表推送已经成功了(如果卡住关掉再来一次即可)

刷新一下我们GitHub,可以看到远程仓库的目录已经和本地一样了

远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库,最好也不要去修改

git push命令,实际上是把当前分支master推送到远程

第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送到远程库originmaster分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送时就可以不加了,直接使用

git push 

抓取git pull

从远程仓库获取最新的更新用

git pull origin master

如果你收到以下提示,代表远程仓库的版本要先于本地版本,必须先用git pull获取更新

hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

自动追踪分支信息

每一次使用git push或者git pull后面都要跟上origin master显然是比较麻烦的,可以使用以下命令建立自动追踪

git branch --set-upstream-to=origin/master master

显示如下

Branch master set up to track remote branch master from origin.

以后就可以直接使用,会自动去找到建立了追踪信息的分支(例子里是master

git push
git pull

如果你收到的提示是这样的

error: the requested upstream branch 'origin/new_test' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.

说明本地有这个分支了,但是远程库根本不存在,那当然无法建立连接了
再次使用git push -u

git push -u origin new_test

创建远程分支new_test,并且把本地内容上传,并且把两者信息关联起来

注意,每一个分支都可以去分别追踪远程库不同的分支,这就意味着你可以让本地库和远程库的每一个分支一一对应起来。也意味着每一次你创建一个新的分支,可能都要去设置一下追踪信息。

git config -e

该命令可查看已经与远程库建立好信息追踪的所有分支信息

克隆git clone

从远程仓库克隆到本地
找一个合适的目录

git clone git@github.com:blurryssky/gittest.git

相关文章

  • Git远程配置

    Git远程查看  Git查看远程仓库 Git远程参数 Git配置远程仓库

  • 2018-05-14

    删除远程分支: git push 远程仓库 --delete 远程分支 git push 远程仓库:远程分支 本地...

  • Git

    删除远程 Git 仓库 git remote rm origin 添加远程 Git 仓库 git remote a...

  • Git - 5.远程仓库

    已经在本地创建了一个Git仓库后,又想在GitHub创建一个Git仓库,并且让这两个仓库进行远程同步 创建远程仓库...

  • Git 入门到放弃

    简介 git关联远程仓库 本地仓库与远程仓库同步问题 Git 终端命令 git关联远程仓库 基本流程 注册gith...

  • git 仓库

    git 初始化 初始化git init git 新建 git 仓库,关联远程仓库 关联远程仓库git remote...

  • Git常用命令大全

    // 初始化仓库 git init //从远程仓库克隆 git clone // 关联远程仓库 git remot...

  • 通过Git将本地项目和远程仓库做关联

    添加远程代码仓库: git remote add origin 远程仓库地址 提交代码到远程仓库: git pus...

  • git 查看、添加、删除 远程仓库

    查看远程仓库地址git remote -v(去掉-v可查看远程仓库名 ) 添加远程仓库地址git remote a...

  • git 命令行操作笔记

    git中的选项解释 创建本地git仓库 提交代码到git仓库 本地git仓库添加到远程仓库中 克隆远程仓库到本地 ...

网友评论

      本文标题:Git - 5.远程仓库

      本文链接:https://www.haomeiwen.com/subject/oqoylttx.html