Git 基础 – 远程仓库的使用
Github简介:
Github基本功能:
GitHub可以托管各种git库,并提供一个web界面,但它与外国的SourceForge、Google Code或中国的coding的服务不同,GitHub的独特卖点在于从另外一个项目进行分支的简易性。为一个项目贡献代码非常简单:首先点击项目站点的“fork”的按钮,然后将代码检出并将修改加入到刚才分出的代码库中,最后通过内建的“pull request”机制向项目负责人申请代码合并。已经有人将GitHub称为代码玩家的MySpace。在GitHub进行分支就像在Myspace(或Facebook)进行交友一样,在社会关系图的节点中不断的连线。
作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户。随着越来越多的应用程序转移到了云上,Github已经成为了管理软件开发以及发现已有代码的首选方法。如前所述,作为一个分布式的版本控制系统,在Git中并不存在主库这样的概念,每一份复制出的库都可以独立使用,任何两个库之间的不一致之处都可以进行合并。
远程仓库:注册github帐号,上传密钥,并新建一个仓库
1. 注册账号
2. 建立密钥
[root@git ~]# ssh-keygen -t rsa -b 4096 -C "xxxxxxx@163.com" 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: SHA256:TI7gUMIVOmeSAwPICamxGrnpnmfOkpzaRgajSIYS7tI yins1205@163.com The key's randomart image is: +---[RSA 4096]----+ |Xoo.+. | |==.= | |+=B + . | |X+ O . = | |OB . . S | |B.E | |+oo | | B+o | |++*o | +----[SHA256]-----+ [root@git ~]# cd .ssh/ [root@git .ssh]# cat id_rsa.pub ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC3IvVrvSQal1HHhTckD90S7VVEJIf1GptdejhjTk3F8N6szVbKnqGCeuyQYQW1s4oK1NSTdZCJjNto/3dqf72lMbZExLUJgloqEYoPS4I93lnkQFUKOaGm4CV7lSX5Kj9PQSZK2tWHpie+DOI2KblPU8xr7Ydeq9I6HUHibkXGC0P4b4MaDISWDRhqD8oGJWVYrTlJQ93E1gapgQlG1OaTPYm74HPhkVHSck8KrQ2AAqqHfl7CO/nMfGta0ey/PqXYryCRvNRoI3sD5W9rzvr13P92s/mDZODYStEGBpCDWI2AFcKpARooaLx2/FQgSW+ocFlb6mBRWXn3+gUHuRPzTvRPx6E+fbvMPYKXmxpKfPU+rZ6kBnHSaQxT8OofgSrfrj5sQd55YkeNbnwAEz2nn5WOZBIrjOq+/EwIhaDeaOU56evlWm1JXdo5a2jsd4aB8LcVa6S4nWHZD76G5lB6na0Bw5AhQfeanjpZLYPjSfqWSt4khDnum26hJbu8889fnDLf01F2sh9reGDj5Ag/w84GU5ySRZiixJQCzUGGXqD7/WmGR5FgwZ05ncJ3cOVY8Gl4axseeNckgfFn5RYjFBE6nsKYQR1xPQbErum7kTbnJcQXF0h/p8+nkOYc0+ui3M1w4jsymHX1uvbdZDabib43MaVKWjKmsy3xjqxHiQ== xxxxxxx@163.com
3. 新建仓库
git使用
1. 推送本地仓库内容到github
[root@git demo]# git remote add origin git@github.com:yins1205/demo.git //关联远程仓库 [root@git demo]# git push -u origin master //第一次推送需要加 -u参数 The authenticity of host 'github.com (20.205.243.166)' can't be established. ECDSA key fingerprint is SHA256:p2QAMXNIC1TJYWeIOttrVc98/R1BUFWu3/LiyKgUfQM. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes //确认连接 Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts. Enumerating objects: 21, done. Counting objects: 100% (21/21), done. Delta compression using up to 4 threads. Compressing objects: 100% (12/12), done. Writing objects: 100% (21/21), 1.63 KiB | 1.63 MiB/s, done. Total 21 (delta 0), reused 0 (delta 0) To github.com:yins1205/demo.git * [new branch] master -> master Branch 'master' set up to track remote branch 'master' from 'origin'.
1.1 查看上传的本地内容
2.查看某个远程仓库
如果想要查看某一个远程仓库的更多信息,可以使用
命令。 如果想以一个特定的缩写名运行这个命令,例如
,会得到像下面类似的信息:
[root@git demo]# git remote show origin * remote origin Fetch URL: git@github.com:yins1205/demo.git Push URL: git@github.com:yins1205/demo.git HEAD branch: master Remote branch: master tracked Local branch configured for 'git pull': master merges with remote master Local ref configured for 'git push': master pushes to master (up to date)
它同样会列出远程仓库的 URL 与跟踪分支的信息。 这些信息非常有用,它告诉你正处于
分支,并且如果运行
, 就会抓取所有的远程引用,然后将远程
分支合并到本地
分支。 它也会列出拉取到的所有远程引用。
3.远程仓库的克隆
3.1 假如你把本地保存git的目录删除了,还可以直接通过远程仓库来恢复
[root@git demo]# cd [root@git ~]# rm -rf demo/
3.2 进入到远程仓库,复制远程仓库地址及内容
3.3 进到服务器,进行克隆
[root@git ~]# git clone git@github.com:yins1205/demo.git Cloning into 'demo'... remote: Enumerating objects: 21, done. remote: Counting objects: 100% (21/21), done. remote: Compressing objects: 100% (12/12), done. remote: Total 21 (delta 0), reused 21 (delta 0), pack-reused 0 Receiving objects: 100% (21/21), done. [root@git ~]# cd demo/ [root@git demo]# ls CONTRIBUTING.md PROJECTS.md README
这样原本的目录及目录中的内容就全部回来了。
4. 重新创建一个首页来进行上传
[root@git demo]# echo www.yunyeblog.com > index.html [root@git demo]# git add index.html [root@git demo]# git commit -m "add index.html" [master da8fae2] add index.html 1 file changed, 1 insertion(+) create mode 100644 index.html [root@git demo]# git push origin master Enumerating objects: 4, done. Counting objects: 100% (4/4), done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 281 bytes | 281.00 KiB/s, done. Total 3 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), completed with 1 local object. To github.com:yins1205/demo.git 2ba06a7..da8fae2 master -> master
4.1 在前端查看
已经上传成功了。