【Git】连接远程仓库

如何使用Git 连接远程仓库呢?远程仓库->一般指的是代码托管平台。那就先来瞅瞅三个较熟悉的版本(代码)托管服务平台。、

版本(代码)托管服务平台:

码云(gitee.com):是开源中国社区团队推出的基于Git的快速的、免费的、稳定的在线代码托管平台,不限制私有库和公有库数量.

Coding(coding.net): 是CODING 旗下的一站式开发平台,提供 git/svn 代码托管,免费支持私有库(限定)和公有库

github(github.com):是全球最大的开源社区,基于git的版本托管平台。私有库需要付费,访问速度慢。

前提准备:

1.git工具的下载和安装(一直next就行了)。 下载 >>> 

2.github/码云/Coding上进行注册。  码云>>> coding>>>   github>>>

前提准备好了就可以开始进行Git与远程仓库的连接,这里以github为例。

一、Git的配置

1.设置用户名和邮箱(--global 为全局参数,表明本地所有Git仓库都会使用这个配置)

git config --global user.name "yourname"

git config --global user.email "[email protected]"

2.生成密钥(SSH key)

ssh-keygen -t rsa -C "[email protected]"

3.添加密钥(SSH key),并验证是否成功

添加密钥:将上一步骤生成的密钥即.ssh/id_rsa.pub中内容全部复制。在github的 Settings-->SSH and GPG keys-->New SSH key,key中粘贴复制的内容(Title自定义)。

验证:github输入第一条的命令,码云输入第二条

a.ssh -T [email protected]

b.ssh -T [email protected]

二、创建项目工程

1.远程仓库:在github中New repository 输入Repository name。[例如:TestDemo]

2.项目工程:在自己本地电脑上新建一个与github新项目工程同名的文件夹。[例如:TestDemo]

三、创建版本库

 进入步骤四中的文件夹下,输入以下命令初始化仓库,若出现:Initialized empty Git repository in E:/** /**/.git/ 则表示创建成功[注意:此时会生成一个.git目录(隐藏目录)]

git init

四、连接远程仓库(下面两种方式都可以)

git remote add origin [email protected]:yourName/repositoryname.git

git remote add origin https://github.com/yourName/repositoryname.git

五、从远程仓库pull文件(若远程仓库没有文件,直接执行步骤六)

git pull origin master

六、将本地文件push到远程仓库(若没有文件则手动创建)

git status          查看工作目录的状态

git add <file>        将文件添加到暂存区

git commit -m "commnet"   提交更改,添加备注信息(此时将暂存区的信息提交到本地仓库)

git push origin master    将本地仓库的文件push到远程仓库

 七、生成多个密钥(多个账户)配置不同的远程仓库【账号配置为局部变量】

a.添加新的ssh-key

如果报错:Could not open a connection to your authentication agent.无法连接到ssh agent;可执行ssh-agent bash命令后再执行ssh-add命令

  ssh-add ./id_rsa_github

  ssh-add ./id_rsa_gitee

b.配置config文件

在./ssh目录下若没有 config文件则创建

# 配置 github
Host github.com
HostName github.com

IdentityFile C:\\Users\\zzw\\.ssh\\id_rsa_github
PreferredAuthentications publickey
User ZeroBound

# 配置 gitee
Host gitee.com

HostName gitee.com
IdentityFile C:\\Users\\zzw\\.ssh\\id_rsa_gitee
PreferredAuthentications publickey
User zhzw

c.到github或码云上添加 密钥,之后验证是否成功

  1.ssh -T [email protected]
  2.ssh -T [email protected]

d.进入仓库目录配置用户名和邮箱

  git config user.name "yourname"

  git config user.email "[email protected]"

八、相关问题

Q1git pull origin master 无法进行pull,出现如下提示

git pull origin master
fatal: unable to access 'https://github.com/yourName/Demo.git': error setting certificate verify locations:
  CAfile: G:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
  CApath: none

分析:ca-bundle.crt文件是证书文件。根据提示CApath:none 没有该文件,所以无法访问远程仓库

解决:修改为正确路径 或者 将证书验证设置false

git config --system http.sslcainfo E:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt

git config --system http.sslverify false

 Q2.git pull origin master 出现如下提示:

fatal: refusing to merge unrelated histories

解决:如下操作即可解决

git pull origin master --allow-unrelated-histories

Q3.每次git push origin master 时都需要输入用户名和密码:

 因为配置的时候使用的是https协议,所以每次都需要输入

git remote -v  查看远程连接

git remote rm origin  删除远程连接

git remote add origin [email protected]:yourName/repositoryname.git
 

分类: Git

标签: git

猜你喜欢

转载自blog.csdn.net/zzddada/article/details/94646439