关于git:git远程仓库

8次阅读

共计 3157 个字符,预计需要花费 8 分钟才能阅读完成。

备注:

本文参考于廖雪峰老师的博客 Git 教程。按照其博客进行学习和记录,感激其自私分享,也欢送各位查看原文。

知识点

  • ssh-keygen -t rsa -C "youremail@example.com"生成 SSh 密钥对。私钥保留好,公钥能够增加到 Git 服务器(如 GitHub 近程仓库)
  • 增加或关联一个近程仓库,git remote add origin git@server-name:path/repo-name.git
  • 第一次将本地仓库推送到近程 master 分支,git push -u origin master
  • 本地提交后,将本地最新仓库推送到近程,git push origin master
  • 克隆近程仓库,git clone git@server-name:path/repo-name.git

近程仓库

Git 是分布式版本控制系统,同一个 Git 仓库能够散布到不同机器上。一台机器上的原始版本库,能够被其余机器“克隆”,且每台机器的版本库都一样,没有主次之分。(一台电脑不同目录也可也组成近程仓库)

理论,个别是有一台服务器,每个人从这个服务器仓库克隆一份到本人电脑,并且能够提交本人的代码,或者从服务器仓库拉取他人的提交。

Github 就是一个提供近程仓库托管服务的网站,通过 github 账号,就能够收费取得 Git 近程仓库。

本地和近程仓库

本地 Git 仓库和近程 Github 仓库之间的传输是通过 SSH 加密的

  • 创立SSH Key

创立 SSH Key 时,生成的公钥和私钥默认寄存在用户主目录下,在用户主目录下(windows 为 c /Users/ 用户名 /,linux 为 /home/ 用户目录 /)会有一个.ssh 目录,并且外面蕴含有 id_rsaid_rsa.pub两个文件。

  • 关上终端(shell,windows 下关上 Git Bash),创立SSH Key
$ ssh-keygen -t rsa -C "youremail@example.com"

设置及明码等,间接回车放弃默认即可,临时不必对 key 设置明码(除非窃密级别特地高)

此时,在用户主目录下找到 .ssh 目录,外面有 id_rsa(私钥,不能泄露) 和id_rsa.pub(公钥),这两个就是 SSH Key 的机密对。

github 近程仓库增加密钥

  • 登陆 Github,点击头像,抉择setting,关上SSH and GPG keys>SSH keys 列表
  • 而后点击“New SSH Key”, 填上 Title,在 Key 文本框里粘贴公钥 id_rsa.pub 的内容

  • 点“Add key”,将公钥增加到 Github
  • GitHub 反对 SSH 协定,通过你增加的公钥,就能够确定代码的提交、近程连贯、推送等不会被他人顶替
  • Github 容许增加多个 Key,这样能够在多台电脑上近程连贯 github 仓库、提交和推送代码等。

Github 上托管的 Git 仓库,目前公开库能够被所有人查看,如果设置为公有库则只能本人查看批改

通过有条件,能够本人搭建 Git 服务器

增加近程仓库

本地曾经存在一个 Git 仓库,通过将其连贯到近程 GitHub 仓库,能够实现备份和近程同步(和写作)

  • 登陆 GItHub,点击“New repository”,新建一个仓库。填上仓库名、形容、public,点击“create repository”。

  • 当初一个空的 GItHub 仓库曾经创立实现。

如下所示,GitHub 疾速装置,有三个向导提醒,能够在命令行创立一个新仓库、或者从本地推送一个已有的仓库、或者导入代码从一个仓库

  • 要把本地已有的仓库推送到近程,依据 GitHub 提醒,在本地的 GitTest 仓库目录下运行如下命令
$ git remote add origin git@github.com:findmoon/newrepo.git

本地仓库曾经和近程仓库关联,实际上能够和任何近程仓库关联,然而 如果 SSH Key 公钥不在 GitHub 的账户列表中,是无奈推送的

近程仓库的名字是origin,是 Git 默认的叫法。

  • 把本地仓库的内容推送到近程仓库
$ git push -u origin master
The authenticity of host 'github.com (...)' can't be established.
RSA key fingerprint is ...
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
对象计数中: 10, 实现.
Delta compression using up to 4 threads.
压缩对象中: 100% (7/7), 实现.
写入对象中: 100% (10/10), 845 bytes | 0 bytes/s, 实现.
Total 10 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To git@github.com:findmoon/newrepo.git
 * [new branch]      master -> master
分支 master 设置为跟踪来自 origin 的近程分支 master。

git push命令实际上是把以后分支 master 推送到近程。

因为近程库是空的,咱们第一次推送 master 分支时,加上了 - u 参数,Git 岂但会把本地的 master 分支内容推送到近程新的 master 分支,还会把本地的 master 分支和近程的 master 分支关联起来,在当前的推送或者拉取时就能够简化命令

  • 查看近程仓库曾经和本地一样

  • 当初,只有本地仓库提交后,就能够间接推送到近程
$ git push origin master

这样,一个分布式版本库就实现了。

SSH 正告

在第一次连贯 GitHub(clonepush), 会有一个 SSH 的正告

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

这是因为 SSH 连贯在第一次验证 GitHub 服务器的 key 时,须要你确认 GitHub 的 key 的指纹信息是否真的来自 GitHub 服务器。
输出 yes 后,主机(你)就会把 GitHub 的 key 增加到本机的信赖列表外面。这个正告只会呈现一次。

从 GitHub 近程仓库克隆

开发时,可能会先创立近程库,而后从近程库克隆到本地

  • 登陆 GitHub,创立一个新仓库remoteRepo(能够勾选Initialize this repository with a README,初始化 readme 文件)

  • 创立实现后,新仓库蕴含 readme 文件

  • 近程库曾经创立实现,下一步能够用 git clone 克隆到本地库。在仓库的两头左边,有一个 Clone or download 按钮,能够看到 clone 的地址
$ git clone git@github.com:findmoon/remoteRepo.git
正克隆到 'remoteRepo'...
Warning: Permanently added the RSA host key for IP address 'XXX' to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
接管对象中: 100% (3/3), 实现.
查看连贯... 实现。

clone 实现后,近程仓库就被下载下来。能够进入目录查看

  • git clone 克隆也反对 https 协定,然而 https 每次都须要输出口令,且速度较慢
正文完
 0