Git-远程仓库

一、添加远程仓库

第一步,先在Github上添加一个远程仓库,仓库名:learngit

第二步,在本地的learngit仓库下运行命令:

git remote add origin [email protected]:testGroup/test-marketing-testcase.git

添加后,远程库的名字就是origin,这是Git默认的叫法,也可以改成别的,但是origin这个名字一看就知道是远程库

第三步,把本地库的所有内容推送到远程库上:

git push -u origin master

在执行第三步的命令时,遇到如下问题:

git push -u origin master
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:n29q+bmAVXjmN3nfxXEgCEpVEMltPqhZR0Jeehrj2ug.
Please contact your system administrator.
Add correct host key in /Users/chichi/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/chichi/.ssh/known_hosts:11
ECDSA host key for git.xxxxxx-inc.com has changed and you have requested strict checking.
Host key verification failed.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

解决方法:

以编辑器的方式进入以下文件:

vi /Users/chichi/.ssh/known_hosts

删除  git.xxxxxx-inc.com  相关的部分。

第四步,删除成功后, 再次把本地库的所有内容推送到远程库上:

  testcase git:(master) git push -u origin master        

Counting objects: 23, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (18/18), done.

Writing objects: 100% (23/23), 1.85 KiB | 0 bytes/s, done.

Total 23 (delta 6), reused 0 (delta 0)

To [email protected]:testGroup/test-marketing-testcase.git

 * [new branch]      master -> master

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

把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。

由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

推送成功后,可以立刻在GitHub页面中看到远程库的内容已经和本地一模一样。

第五步,从现在起,只要本地作了提交,就可以通过命令把本地master分支的最新修改推送至GitHub:

git push origin master

小结

要关联一个远程库,使用命令git remote add origin git@server-name:path/repo-name.git

关联后,使用命令git push -u origin master第一次推送master分支的所有内容;

此后,每次本地提交后,只要有必要,就可以使用命令git push origin master推送最新修改;

二、从远程库克隆

上文介绍了先有本地库,后有远程库的时候,如何关联远程库。

现在,假设我们从零开发,那么最好的方式是先创建远程库,然后,从远程库克隆。

第一步,创建一个项目,如test-marketing-testcase

第二步,在本地添加一个文件夹,然后进入这个目录,用命令git clone克隆一个本地库:

➜  testcase git:(master) cd /Users/chichi/Documents/gittest 
➜  gittest git clone [email protected]-inc.com:testGroup/test-marketing-testcase.git 
Cloning into 'test-marketing-testcase'...
remote: Enumerating objects: 45, done.
remote: Counting objects: 100% (45/45), done.
remote: Compressing objects: 100% (37/37), done.
remote: Total 45 (delta 15), reused 0 (delta 0)
Receiving objects: 100% (45/45), 485.71 KiB | 0 bytes/s, done.
Resolving deltas: 100% (15/15), done.
Checking connectivity... done.

可以在本地看到gittest文件夹里的内容已经和远程库里的一模一样了。

GitHub给出的地址不止一个,还可以用https://git.xxxx-inc.com/testGroup/test-marketing-testcase.git这样的地址。实际上,Git支持多种协议,默认的git://使用ssh,但也可以使用https等其他协议。

使用https除了速度慢以外,还有个最大的麻烦是每次推送都必须输入口令,但是在某些只开放http端口的公司内部就无法使用ssh协议而只能用https

小结

要克隆一个仓库,首先必须知道仓库的地址,然后使用git clone命令克隆。

Git支持多种协议,包括https,但通过ssh支持的原生git协议速度最快。

猜你喜欢

转载自www.cnblogs.com/chengchengla1990/p/10095824.html