本地仓库与远程仓库的交互

1. 添加远程仓库

1)在 github 上新建一个项目

2)在本地仓库中将新建远程仓库做关联

在本地仓库下运行命令

$ git remote add origin [email protected]:michaelliao/learngit.git

注意:

  1. 远程库的名字就是 origin,这是 Git 默认的叫法,也可以改成别的
  2. 一个 git 项目可以关联多个远程库,关联的远程库的其他常用叫法也有:upstream 表示上游代码库名, 该名字也可以任意。
  3. 由于一个 git 项目可能关联多个远程库,所以墙裂建议根据当前关联的远程库的特点起一个有意义的名字,方便辨认

3)查看关联的远程库

有两种查看「关联的远程库」的方式:

  1. 输入 git remote -v
     ```python
     # 下面显示了可以 fetch 和 push 的远程库的地址。如果没有推送权限,就看不到push的地址。
     origin    [email protected]:hfy/zhihu-web.git (fetch)
     origin    [email protected]:hfy/zhihu-web.git (push)
     zhihu    [email protected]:zhihu/zhihu-web.git (fetch)
    
  1. 在项目的 .git/config 文件中可以查看 其中每一个 [remote "xxx"] 就是关联的一个远程库
     [core]
         repositoryformatversion = 0
         filemode = true
         bare = false
         logallrefupdates = true
         ignorecase = true
         precomposeunicode = true
    [remote "zhihu"]
         url = git@git.in.zhihu.com:zhihu/zhihu-web.git
         fetch = +refs/heads/*:refs/remotes/origin/*
    [remote "origin"]
         url = git@git.in.zhihu.com:hfy/zhihu-web.git
         fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
         remote = origin
         merge = refs/heads/master
    

4)修改 / 删除关联的远程库

【删除】:在 .git/config 文件中删除相应的 [remote "xxx"] 记录即可删除关联的远程库

【修改】:删除旧的远程关联库,并重新增加新的关联库即可

4. 从已有的远程仓库中克隆项目

参见 获取 Git 仓库

4. SSH警告

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

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)?
  • 这是因为 Git 使用 SSH 连接,而 SSH 连接在第一次验证 GitHub 服务器的 Key 时,需要确认GitHub 的 Key 的指纹信息是否真的来自 GitHub 的服务器,输入 yes 回车即可。

  • Git 会输出一个警告,告诉你已经把 GitHub 的 Key 添加到本机的一个信任列表里了:

Warning: Permanently added 'github.com' (RSA) to the list of known hosts.

这个警告只会出现一次,后面的操作就不会有任何警告了。

如果你实在担心有人冒充GitHub服务器,输入 yes 前可以对照 GitHub 的 RSA Key 的指纹信息是否与 SSH 连接给出的一致。

results matching ""

    No results matching ""