Git- 链接远程仓库

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

 

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

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

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

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

 

前提准备:工具

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

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

前提准备好了就能够开始进行Git与远程仓库的链接,这里以github为例。.net

 

1、Git的配置3d

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

git config --global user.name "yourname"

git config --global user.email "your_email@youremail.com"

2.生成密钥(SSH key)

ssh-keygen -t rsa -C "your_email@youremail.com"

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

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

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

a.ssh -T git@github.com 
b.ssh -T git@gitee.com

2、建立项目工程

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

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

3、建立版本库

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

git init

4、链接远程仓库(下面两种方式均可以)

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

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

git pull origin master

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

git status          查看工做目录的状态

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

git push origin master    将本地仓库的文件push到远程仓库(若 push 不成功,可加 -f 进行强推操做)

 注: 至此已经完成了 远程与本地仓库的配置,若须要单独配置可见如下操做

7、生成多个密钥(多个帐户)配置不一样的远程仓库【帐号配置为局部变量】

 

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 git@github.com
  2.ssh -T git@gitee.com
 
d.进入仓库目录配置用户名和邮箱
  git config user.name "yourname"
  git config user.email "your_email@youremail.com"

 

8、相关问题

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 git@github.com:yourName/repositoryname.git
相关文章
相关标签/搜索