GitHub社会化编程---基础篇

一、社会化编程

GitHub创造了社会化编程的概念。随着GitHub的出现,软件开发者们才真正意义上拥有了源代码。世界上任何人都可以比从前更容易获得源代码,所有人都平等的拥有了更改源代码的权利,并在自由更改后加以公开。这在软件开发领域是一场巨大的革命,而革命领导者GitHub的口号便是“社会化编程”。如今,世界众多程序员都在通过GitHub公开源代码,同时利用GitHub支持着自己日常的软件开发。接下来,我们将深入理解引发这场革命的社会化编程,带你一窥GitHub的概要。

二、GitHub的使用环境

1.GitHub注册

已经有写的不错的博客,这里偷点懒不再重复了,附上链接供参考:GitHub注册图解

2.Git工具安装

Git仓库管理功能是GitHub的核心,Git属于分散性版本管理系统,是为版本管理而设计的软件。Git工具安装可参考下面的官方教程:安装Git

推荐一款更好用的工具Cmder (= Cmd + PowerShell + Git for windows + ''bash"),下载和配置可以参考博客:Cmder简介

3.Git环境初始化配置

3.1设置姓名和邮箱地址

$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "[email protected]"

3.2提高命令输出的可读性

$ git config --global color.ui auto

3.3设置SSH Key

Secure Shell (SSH) 是一个允许两台电脑之间通过安全的连接进行数据交换的网络协议。通过加密保证了数据的保密性和完整性。SSH采用公钥加密技术来验证远程主机,以及(必要时)允许远程主机验证用户。SSH Key(密钥对:公钥和私钥)可以让你方便的登录到SSH服务器,而无需输入密码。

为什么GitHub需要SSH Key呢?因为GitHub需要识别出你推送的提交确实是你推送的,而不是别人冒充的,而Git支持SSH协议(你的本地Git仓库和远程GitHub仓库之间的传输是通过SSH加密的),所以,GitHub只要知道了你的公钥(与公钥唯一配对的私钥保存在本地),就可以确认只有你自己才能推送。

在上面《GitHub注册图解》的后半部分有给出设置SSH Key的图解。添加SSH Keys成功后(GitHub允许你添加多个Key,从多个主机访问GitHub),就可以用手中的私人密钥与GitHub进行认证和通信了。输入本地Git连接GitHub的命令,获得如下所示的返回值,就说明已经连接成功了。

$ ssh -T [email protected]
Hi user-name! You've successfully authenticated, but GitHub does not provide shell access.

4.GitHub访问太慢解决方案

国内访问GitHub可能因为网络代理商的问题,导致访问时间很长甚至访问失败。可以使用VPN翻墙解决,也可以绕过DNS解析,在本地直接绑定host (C:\Windows\System32\drivers\etc\hosts)。具体可参考这篇博客GitHub绑定IP

该方法也可以本地主机直接绑定其他IP,主要通过https://www.ipaddress.com/网站查询访问网址对应的IP地址,也可以再使用ping github.com或ping 192.30.253.112命令验证下访问时间TTL是否够小,如果访问失败或者TTL太大可以更换一个TTL更小的IP绑定。

四、GitHub菜单操作

1.GitHub界面常用快捷键

开源领域为了提高效率,常有很多快捷键操作,比如VIM,GitHub也不例外,在登陆后的GitHub各个界面按 shift + / 可以打开适用于该页面的快捷键一览表,从不同页面打开快捷键一览表显示可用的快捷键可能不同。

2.GitHub页面常用菜单

GitHub页面常用的操作可分为以下几部分,可参考博客玩转GitHub

  • 顶部的工具栏
  • 控制面板(URL:https://github.com)
  • 个人信息(URL:https://github.com/user-name)
  • 仓库页面(URL:https://github.com/user-name/repository-name)

最常用的是管理Issue的Bug Tracking System和修改代码后用于分支合并请求的Pull requests。如果给Issue添加源代码,它就会变成Pull requests,二者的编号也相互通用。Issue在跟踪探讨并解决Bug方面很方便,Pull requests主要用于申请将自己的代码合并到主分支或别人的仓库中,当然你的申请需要通过别人的审核与测试,确保没有Bug后才允许合并,两者都支持对每一行代码进行评注。Issue/Pull requests与协作编写文档的WiKi共同构成了多人协作开发的核心工具,三者都支持**GFM语法(github-flavored-markdown)**,可以轻松创建表现力丰富的文档。推荐一个支持MarkDown语法的本地开发文档编辑工具:Typora极简教程和一个支持MarkDown语法的开发文档或手册管理工具GitBook使用教程

3.支持GitHub的GUI客户端

GitHub使用CLI(Command Line Interface)操作自然比较高效,但对习惯了菜单操作的新手不够友好,GitHub公司提供了GUI客户端辅助使用,教程可参考博客GitHub Desktop图文教程

五、GitHub常用命令

1.GitHub常用命令简介

GitHub日常使用频率最高的命令见下图,分别由Remote远程仓库、Repository本地仓库、Workspace工作分支、Index暂存区四部分构成。文件或代码通过下图所示的路径进行传递,每一步都有命令查询状态日志和变更细节,详见博客:常见Git命令清单
最常用的GitHub操作

2.本地仓库到远程仓库的实例

下面用一个例子展示从Repository本地仓库初始化到上传Remote远程仓库的过程,下面使用更好用的Cmder工具了(支持Git命令,默认以λ作为命令提示符):

λ mkdir git-tutorial					#新建本地仓库文件夹

λ cd git-tutorial						#切换到本地仓库路径内

λ git init								#初始化仓库
Initialized empty Git repository in D:/GitHub/git-tutorial/.git/

λ git checkout -b master				#新建master工作分支并切换到该分支
Switched to a new branch 'master'

λ touch README.md						#新建文件

λ git add README.md						#将默认的master工作分支内的文件添加到index暂存区

λ git status							#查看仓库的状态

λ git commit -m "First commit"			#将当前暂存区内的文件实际保存到本地仓库的历史记录中
[master (root-commit) beff257] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md
 
 λ git log								#查看提交日志
commit beff257f859f6214d3788a34682b88a0831ebd69 (HEAD -> master)

 #往README.md文件内写点内容,比如“Add First commit”
 
 λ git diff								#查看工作分支与暂存区的差别
diff --git a/README.md b/README.md
index e69de29..bca39bf 100644
--- a/README.md
+++ b/README.md
@@ -0,0 +1,2 @@
+# Add First commit

λ git commit -am "Second commit"		#向暂存区添加并向本地仓库提交现有文件更新
[master 58253cb] Second commit
 1 file changed, 2 insertions(+)	
 			
 #将已新建好的远程仓库添加为本地仓库的远程仓库,并将该远程仓库在本地的标识符设为git-tutorial
 λ git remote add git-tutorial [email protected]:StreamAI/git-tutorial.git
 
 λ git push -u git-tutorial master		#将当前分支下本地仓库的内容推送给远程仓库的master分支
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (6/6), 452 bytes | 226.00 KiB/s, done.
Total 6 (delta 0), reused 0 (delta 0)
To github.com:StreamAI/git-tutorial.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'git-tutorial'.

3.远程仓库到本地仓库的实例

还拿刚才的例子继续,先把本地git-tutorial文件夹删除,展示从远程仓库获取到本地并修改后上传的过程:

λ git clone [email protected]:StreamAI/git-tutorial.git	#从远程仓库克隆到本地
Cloning into 'git-tutorial'...
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Receiving objects: 100% (6/6), done.

λ cd git-tutorial						#切换到目标仓库目录

λ git checkout -b feature-A				#新建并切换到feature-A工作分支
Switched to a new branch 'feature-A'

#更新README.md文件内容

λ git diff								#查看工作分支与暂存区的差别

λ git add README.md						#将默认的master工作分支内的文件添加到index暂存区

λ git commit -m "Add Feature-A"			#将当前暂存区内的文件实际保存到本地仓库的历史记录中
[feature-A a7e3806] Add Feature-A
 1 file changed, 3 insertions(+), 1 deletion(-)
 
λ git checkout master					#从feature-A工作分支切换到master工作分支
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

λ git pull origin master				#获取最新的远程仓库源码,让本地仓库维持最新状态
From github.com:StreamAI/git-tutorial
 * branch            master     -> FETCH_HEAD
Already up to date.

λ git merge --no-ff feature-A			#将已实现的feature-A工作分支合并到master工作分支
Merge made by the 'recursive' strategy.
 README.md | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
 
 λ git reflog							#查看当前仓库执行过的操作日志
64bd41b (HEAD -> master) HEAD@{0}: merge feature-A: Merge made by the 'recursive' strategy.
e49acb5 (origin/master, origin/HEAD) HEAD@{1}: checkout: moving from feature-A to master
a7e3806 (feature-A) HEAD@{2}: commit: Add Feature-A
e49acb5 (origin/master, origin/HEAD) HEAD@{3}: checkout: moving from master to feature-A
e49acb5 (origin/master, origin/HEAD) HEAD@{4}: clone: from [email protected]:StreamAI/git-tutorial.git

λ git push					#从远程仓库克隆在HEAD内有记录,所以推送更新可不用跟远程仓库标识符
Enumerating objects: 6, done.
Counting objects: 100% (6/6), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 374 bytes | 374.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), done.
To github.com:StreamAI/git-tutorial.git
   e49acb5..64bd41b  master -> master
发布了65 篇原创文章 · 获赞 35 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/m0_37621078/article/details/88083715