连接远端git仓库
与远端的git仓库的连接具体步骤如下:
1.安装git客户端
[root@miner_k ~]# yum -y install git
2.认证
连接远端的git仓库需要认证,而认证的方式有两种,第一种是使用SSH协议的密钥登录,另外的一种是使用HTTPS协议连接。
以github为例:
2.1 使用ssh协议连接验证身份
2.1.1 在本地生成密钥对,此处密钥是以空为例
[root@miner_k ~]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
7a:cb:59:58:ab:5a:e6:6a:1e:ed:a6:52:30:84:ce:ca root@miner_k
The key's randomart image is:
+--[ RSA 2048]----+
| . |
| . . |
| o . |
| o o |
|.. o S . |
|.E .o o . |
| .o * o |
| . .O.= |
| +==B |
+-----------------+
2.1.2 将本地的公钥上传到github上
[root@miner_k ~]# cd .ssh/
[root@miner_k .ssh]# ls
id_rsa id_rsa.pub
[root@miner_k .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA9bHKT8jGqqbetXaoXCF6xP8/Zwtx2ENAvKMtB3oi8PmmftfHX8CFl7x6sssThlVB3mYsDnDUNtR9ptJSJFCff0UNxg35wbRBezh30YveL3XKD3X1Zbw6RgmQaeHHQ39JsFC7rm4Q2I8cW+woX8de78XHNpjvd6mAuSgKs4va+Efadma3/+c3qp8h6h84W10K9UW9ZkMlklPE9NQl3mJ11SxH/jxVRheNooNZVXzVVPgo4vwpLMq0spXRdbRvJz3a6Unt6JFzwgi127GdF81ju50rTQe5ULWcnD8aIflDDLoeVf5sxX9Z/ZSXIP8zdTkzqhI5LE/33HiOjARKdFmiVQ== root@miner_k
2.2 使用HTTPS协议认证(两种方式)
2.2.1生成GPG key(github有详细简介,在此不做介绍)
https://help.github.com/articles/generating-a-new-gpg-key/
2.2.2使用github的账户和密码连接
将仓库克隆到本地
[root@miner_k shell]#git clone https://github.com/github134/shell.git
修改配置文件:
[root@miner_k shell]# cd shell/.git/
[root@miner_k .git]# vi config
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = https://github134@github.com/github134/shell.git
**修改内容:用户名@仓库地址**
在上传文件时可以输入密码:
[root@miner_k shell]#git push
password:
3.在本地连接远端的仓库
3.1 克隆数据到本地
3.1.1 使用SSH协议
从github上获取SSH协议的地址
[root@miner_k git]# git clone git@github.com:github134/git-test2.git
Initialized empty Git repository in /git/git-test2/.git/
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
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.
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 19 (delta 1), reused 15 (delta 0), pack-reused 0
Receiving objects: 100% (19/19), done.
Resolving deltas: 100% (1/1), done.
[root@miner_k git]# ls
git-test2
3.1.2 使用HTTPS协议连接
[root@miner_k git-https]# gpg --list-secret-keys --keyid-format LONG
/root/.gnupg/secring.gpg
------------------------
sec 2048R/79DB69E0BE828D4F 2017-05-12 [expires: 2018-05-12]
uid miner_k
ssb 2048R/1C432EAE99D2145E 2017-05-12
[root@miner_k git-https]# git config --global user.signingkey 79DB69E0BE828D4F
[root@miner_k git-https]# git clone https://github.com/github134/git-test2.git
Initialized empty Git repository in /git/git-https/git-test2/.git/
remote: Counting objects: 19, done.
remote: Compressing objects: 100% (13/13), done.
remote: Total 19 (delta 1), reused 15 (delta 0), pack-reused 0
Unpacking objects: 100% (19/19), done.
[root@miner_k git-https]# ls
git-test2
3.2 与远端仓库的关联
本地没有初始化仓库会报错。错误如下:
[root@miner_k git-remote]# git remote add origin git@github.com:github134/git-test2.git
fatal: Not a git repository (or any of the parent directories): .git
正确操作:
[root@miner_k git-remote]# git init
Initialized empty Git repository in /git/git-remote/.git/
[root@miner_k git-remote]# git remote add origin git@github.com:github134/git-test2.git
3.3 本地仓库中的操作
3.3.1 查看远端仓库的信息
[root@miner_k git-test2]# git remote
origin
[root@miner_k git-test2]# git remote -v
origin git@github.com:github134/git-test2.git (fetch)
origin git@github.com:github134/git-test2.git (push)
3.3.2 本地仓库与远端仓库分支关联
[root@miner_k git-test2]# git checkout -b dev origin/dev
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
[root@miner_k git-test2]# git branch
* dev
master
[root@miner_k git-test2]# ls
dev1.txt new.txt README.md
注:本地仓库的分支可以和远端仓库的分支不同,但是建议创建的分支名称相同
3.3.3 将分支推送到远端服务器
[root@miner_k git-test2]# git push origin dev
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 329 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:github134/git-test2.git
a6e3ed5..f51b3b0 dev -> dev
注:将本地的dev分支推送到远端的服务器上,如果远端服务器上没有dev分支会自动创建。
不同开发人员同时修改代码之后上传会发送冲突
解决思路:
1. 首先,可以试图用git push origin branch-name推送自己的修改;
2. 如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;
3. 如果合并有冲突,则解决冲突,并在本地提交;
4. 没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成功!
1.推送时的报错信息:
[root@miner_k git-test2]# git push origin dev
To git@github.com:github134/git-test2.git
! [rejected] dev -> dev (non-fast-forward)
error: failed to push some refs to 'git@github.com:github134/git-test2.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.
2.将远端仓库的代码重新拉取一遍
[root@miner_k git-test2]# git pull
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (1/1), done.
remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0
Unpacking objects: 100% (3/3), done.
From github.com:github134/git-test2
f51b3b0..dbf021f dev -> origin/dev
Auto-merging dev3.txt
CONFLICT (add/add): Merge conflict in dev3.txt
Automatic merge failed; fix conflicts and then commit the result.
3.修改代码,解决冲突。重新提交到本地仓库
[root@miner_k git-test2]# ls
dev1.txt dev2.txt dev3.txt new.txt README.md
[root@miner_k git-test2]# cat dev3.txt
<<<<<<< HEAD
test2
=======
test1
>>>>>>> dbf021fc4028b4b440638eb85434e84c112ab3d4
[root@miner_k git-test2]# vim dev3.txt
[root@miner_k git-test2]# git add -A
[root@miner_k git-test2]# git commit -m "modify dev3.txt"
[dev 67cc63f] modify dev3.txt
4.重新推送到远端的git仓库中
[root@miner_k git-test2]# git push origin dev
Counting objects: 9, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 537 bytes, done.
Total 6 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To git@github.com:github134/git-test2.git
dbf021f..67cc63f dev -> dev