Git详解二

本文详细介绍如何使用Git进行远程仓库的添加与克隆,包括关联远程仓库、推送本地分支到远程以及从远程仓库拉取更新等内容。
前言:这一篇文章还是继续Git,在这篇文章中我们会提到远程仓库、分支以及一些别名的配置,关于Git的一些基础操作命令,请参照  Git详解一    好了 下面我们开始

本文摘自廖雪峰git教程,连接地址:http://www.liaoxuefeng.com/

一、远程仓库

对与远程仓库首先大家要有一个概念,我们使用git 在本地创建的仓库是一个本地仓库,而远程仓库是放在网络上的,是任何人访问的(前提要有权限)
对远程仓库的操作一般分为两种:添加到远程仓库(也就是我们通常的push)和 从远程克隆仓库

添加到远程仓库:
首先要想添加仓库我们必须要有一个远程仓库,而这里呢?我们暂时利用一个第三方的网站  github  来作为我们的远程仓库,首先我们需要登录自己的github,然后找到 New Repository   创建一个仓库 输入我们要创建的仓库名,然后直接点击 Create repository

现在我们已经创建了一个空的 FristRepository  仓库,现在我们就可以将我们本地的仓库提交到这个远程仓库,又或者从这个远程仓库克隆一个到本地

首先不管是添加还是克隆,我们都需要先关联一下这个远程仓库,怎么关联呢? 看代码:
$ git remote add origin https://github.com/RookieKk/FirstRepository.git
 来我们先分别介绍一下$ git remote add origin https://github.com/RookieKk/FirstRepository.git  这个字符串 的组成部分, git remote add origin  其实做的操作其实 就相当于  起别名   而https://github.com/RookieKk/FirstRepository.git其实就是我们要关联的仓库的地址,也就是ssh秘钥,在这里大家需要注意的是https://github.com/RookieKk/FirstRepository.git 这个位置一定要是你自己的噢!

上面的完成了之后,我们就可以试着将我们本地的仓库给添加到远程仓库了,不过因为是首次添加,所以我们如果直接push可能会出现这个问题:
$ git push -u origin master
Username for 'https://github.com': RookieKk
Password for 'https://RookieKk@github.com':
To https://github.com/RookieKk/FirstRepository.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/RookieKk/FirstRepository.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解释一下:因为是首次添加,所以我们后面最好是跟上一个 -u 参数,这个参数不仅会把我们本地的master分内容推送到远程master分支上,而且还会把本地的master分支与远程的master分支关联起来,而 origin 是我们取得别名,是用来替代 https://github.com/RookieKk/FirstRepository.git,这样写比较方便,不然每次都这么写想想都觉得累
解决上面的问题其实很简单,我们只需要先 pull 一下之后再 push 就可以了
$ git pull origin master
warning: no common commits
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/RookieKk/FirstRepository
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
Merge made by the 'recursive' strategy.
 README.md | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
然后在 push:
$ git push -u origin master
Username for 'https://github.com': RookieKk
Password for 'https://RookieKk@github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/RookieKk/FirstRepository.git/'
在然后去我们的github 仓库中查看我们提交的内容:



克隆仓库:

上次我们讲了先有本地库,后有远程库的时候,如何关联远程库。

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

首先,登陆GitHub,创建一个新的仓库,名字叫 gitskills_clone

仓库的创建在这里就不在做介绍了,我们直接看命令:

$ git clone https://github.com/RookieKk/gitskills_clone.git
Cloning into 'gitskills_clone'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.

这时候你就会发现在你的工作空间的目录下多了一个名为 gitskills_clone 的文件夹 这个文件就是我们 clone 下来的本地仓库
















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值