极简 Github 上手教程
随着你写的代码越来越多,就需要去管理这些代码,可能要在不同的电脑上编写同一份代码,可能要和别人合作开发,也可能想把项目分享给别人。更进一步地,你可能还会需要管理代码的不同历史版本。于是你就得和代码管理工具以及代码托管平台打交道。
Github 是现在广泛流行的代码托管平台,很多开源项目都把代码放在这里。之前我们有过关于 git 使用的入门教程(从公众号底部菜单栏可进入)。今天,再来科普一下如何把代码放在 github 上。
1. 去 github.com 注册一个账号。

依次输入用户名、邮箱、密码,就可注册成功。需要去邮箱里验证一下,否则不能创建项目。
2. 登录后,点击 New repository 创建一个新项目。

此页面上的 Let’s get started! 是一个简要的 github 使用教程,不妨也浏览一下。

输入项目名称,然后把创建 README 勾选上,这样可以省去你初始化项目的步骤,方便之后直接在本地获取项目。
点击 Create repository 完成创建。

在项目页面上,从 Clone or download 里找到项目的地址,后面会用到它。
3. 安装 git 客户端,获取项目到本地。
Windows 下推荐使用 git for windows。官网下载有可能会很慢,不行的话就直接从搜索引擎找个安装文件下载。Github 本身也提供了一个客户端,但同样从国内下载也很慢。
安装好 git for windows 后,可以通过 git 命令行获取项目并进行后续的更新。具体命令可参考之前的 git 入门教程。这里我说一下图形化界面的使用。

从开始菜单中选择 Git GUI,打开界面,选择 Clone Existing Repository,

Source Location 输入刚才 Github 上创建的项目地址,TargetDirectory 设置一个本地目录。这里注意,本地目录必须是个还不存在的目录,否则无法创建成功。

创建成功后,就会将项目代码复制到本地。
4. 修改,上传代码。
在项目目录新增一个文件,test.py。

在文件夹里点击右键,选择 Git GUI Here,打开 Git GUI 并显示当前项目。看到 Unstaged Changes 里有新增的文件。点击文件前面的图标,将其移动至 Staged Changes 里,就可以准备提交了。

在 Commit Message 里输入提交注释,点击 Commit,完成提交。
然后再点击 Push,就可以将提交更新至 Github 上。此时会提示你输入 Github 的用户名和密码。

完成之后,可以通过点击 Git GUI 菜单栏上的 Repository -> Visualize All Branch History 查看提交历史。

此时到 Github 的项目页面上,就可以看到,test.py 已经被添加到项目中。

关于 git 中文件的状态转换,以及 git 命令行的使用,请参考 git 入门教程。更详细的说明,可查阅 pro git 教程。