GITHUB之GIT BASH使用教程技术
写在前面
这篇文章写完后,感觉不是很满意,漏掉了一些常用的命令忘记写,如“git tag”,“git diff”,“git show”,“git log”,“git remote”等。但是作为教程,应该是越简单越好,文章太长,反而惹人讨厌,这样一想,也就没有继续补充了。
GITHUB 是全球最出名的基于 GIT 的代码托管平台之一,可以免费的托管开源代码。作为一名软件工程师,对代码的管理养成一个良好的习惯是非常重要的。本人将讲解如何使用 GIT BASH 管理 GITHUB 中的代码库。
Git 是基于 Linux 内核开发的版本控制工具。与常用的版本控制工具 CVS,Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持,使源代码的发布和交流极其方便。 Git 的速度很快,这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git 最为出色的是它的分支、合并、跟踪的能力。
申请 GITHUB 账号
安装 GIT 客户端
GITHUB 官网有上提供 GITHUB for Windows 应用程序,关于 GITHUB for Windows 的使用非常的方便,图形界面同时也提供了 GIT Bash。要求必须是 Windows7 及以上的操作系统,如果您使用的 Windows XP 的系统,就只能安装其他 GIT 客户端了。
配置 SSH KEYS
要使用SSH协议连接 GITHUB,首先需要生成 SSH KEYS。生成的密钥是两个文件,一个公钥一个私钥。公钥需要提交给GITHUB 官网您的账号中。关于如何生成 SSH Keys,请看如下步骤示例:
检查 SSH keys
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
生成 keys
$ ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
# Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter]
然后按照提示输入密码,最后出现如下提示则说明生成成功。
Your identification has been saved in /c/Users/you/.ssh/id_rsa.
# Your public key has been saved in /c/Users/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]
将生成的新秘钥加入 SSH 客户端
# start the ssh-agent in the background
$ ssh-agent -s
# Agent pid 59566
$ ssh-add ~/.ssh/id_rsa
复制公钥
$ clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
将公钥加入到 GITHUB 账号
GIT BASH 命令详解
git init
初始化 GIT,只有初始化了以后才可以使用 GIT 相关命令。在初始化之前,可以先创建一个文件夹。如下示例
$ mkdir lmlphp
$ cd lmlphp
$ git init
git clone
C:\Users\May\Documents\GitHub\test> git clone [email protected]:leiminglin/LMLPHP.g
it
Cloning into 'LMLPHP'...
Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of know
n hosts.
remote: Counting objects: 210, done.
remote: Total 210 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (210/210), 66.48 KiB | 15.00 KiB/s, done.
Resolving deltas: 100% (102/102), done.
Checking connectivity... done.
C:\Users\May\Documents\GitHub\test>
git branch
git branch 命令用于创建分支,查看分支。查看分支可以使用参数-a,-v,-r等,a代表所有,v代表版本信息,r 代表显示远程分支。下面的例子使用“git branch develop”创建了一个新的分支。
C:\Users\May\Documents\GitHub\test> cd .\LMLPHP
C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git branch -av
* master 405960a session_write_close() when fatal error occured
remotes/origin/HEAD -> origin/master
remotes/origin/develop 405960a session_write_close() when fatal error occured
remotes/origin/master 405960a session_write_close() when fatal error occured
C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git branch develop
C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git branch -av
develop 405960a session_write_close() when fatal error occured
* master 405960a session_write_close() when fatal error occured
remotes/origin/HEAD -> origin/master
remotes/origin/develop 405960a session_write_close() when fatal error occured
remotes/origin/master 405960a session_write_close() when fatal error occured
C:\Users\May\Documents\GitHub\test\LMLPHP [master]>
git checkout
git checkout 命令用于创建分支和切换分支。*号代表当前分支,下面通过 checkout 命令切换到 develop 分支。"checkout"在英文中的意思是检出,但是也不难理解,GIT 中分支其实就是一个指向,速度很快;现在我的本地有两个分支,但是只有一份代码,当使用 checkout 命令切换分支并且两个分支的内容不同时,你会发现磁盘上的文件内容即刻发生了变化。checkout 命令还可以用来创建分支并切换到这个分支,使用 checkout -b 参数即可,下面的例子使用此命令创建了 newFeature 分支并切换到了这个分支。
C:\Users\May\Documents\GitHub\test\LMLPHP [master]> git checkout develop
Switched to branch 'develop'
C:\Users\May\Documents\GitHub\test\LMLPHP [develop]> git branch -av
* develop 405960a session_write_close() when fatal error occured
master 405960a session_write_close() when fatal error occured
remotes/origin/HEAD -> origin/master
remotes/origin/develop 405960a session_write_close() when fatal error occured
remotes/origin/master 405960a session_write_close() when fatal error occured
C:\Users\May\Documents\GitHub\test\LMLPHP [develop]> git checkout -b newFeature
Switched to a new branch 'newFeature'
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git branch -av
develop 405960a session_write_close() when fatal error occured
master 405960a session_write_close() when fatal error occured
* newFeature 405960a session_write_close() when fatal error occured
remotes/origin/HEAD -> origin/master
remotes/origin/develop 405960a session_write_close() when fatal error occured
remotes/origin/master 405960a session_write_close() when fatal error occured
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]>
git status
git status 命令用来查看当前分支状态。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git status
On branch newFeature
nothing to commit, working directory clean
git pull
git pull 命令用来更新代码,该命令相当于 git fetch 和 git merge 的组合。需要注意的是,如果来源是远程分支 develop,则必须这样写“origin develop”,origin 后面有个空格。如果远程分支存在有和当前分支一样的名字,则可以不指定分支。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git pull
Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know
n hosts.
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details
git pull
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/newFeature
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git pull origin develop
Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know
n hosts.
From github.com:leiminglin/LMLPHP
* branch develop -> FETCH_HEAD
Already up-to-date.
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]>
git add
git commit
git commit 命令用来提交更新。更新时需要提交注释,使用 -m 参数。GITHUB 提供的 GIT 客户端做的非常好,如果您忘记添加注释,它会弹出文本框让你填写。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git commit -m "test"
On branch newFeature
nothing to commit, working directory clean
git push
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git push
fatal: The current branch newFeature has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin newFeature
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git push --set-upstream
origin develop
Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know
n hosts.
Branch develop set up to track remote branch develop from origin.
Everything up-to-date
GIT BASH 的功能非常强大,使用也非常方便,特别适合大型项目多人同时开发。图形界面工具虽然方便简单,但是效率远远不能跟命令行相比。本文简单的介绍了 GIT BASH 的使用,更多的功能请使用 --help 命令查看,系统会使用浏览器打开 HTML 版本文档,描述的非常详细。
如何使用 GIT BASH 同时管理多个远程库和推送更新到多个远程 GIT 服务端,请看我的另一篇文章《使用GIT BASH管理多个远程代码库》。
相关文章
暂无