GITHUB之GIT BASH使用教程

来源:互联网 发布:mac pdf reader pro 编辑:程序博客网 时间:2024/04/28 23:26
maybe yes 发表于2014-10-25 12:12
原文链接 : http://blog.lmlphp.com/archives/7/The_use_tutorial_of_git_bash_and_how_to_start_with_github  来自 : LMLPHP后院
写在前面
这篇文章写完后,感觉不是很满意,漏掉了一些常用的命令忘记写,如“git tag”,“git diff”,“git show”,“git log”,“git remote”等。但是作为教程,应该是越简单越好,文章太长,反而惹人讨厌,这样一想,也就没有继续补充了。
GITHUB 是全球最出名的基于 GIT 的代码托管平台之一,可以免费的托管开源代码。作为一名软件工程师,对代码的管理养成一个良好的习惯是非常重要的。本人将讲解如何使用 GIT BASH 管理 GITHUB 中的代码库。
Git 是基于 Linux 内核开发的版本控制工具。与常用的版本控制工具 CVS,Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持,使源代码的发布和交流极其方便。 Git 的速度很快,这对于诸如 Linux kernel 这样的大项目来说自然很重要。 Git 最为出色的是它的分支、合并、跟踪的能力。
申请 GITHUB 账号
关于 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 your_email@example.com
将生成的新秘钥加入 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 账号
登录 GITHUB 在 Settings->SSH keys 菜单下添加,将剪切板的内容粘贴到 Key 文本框中,名称可以随意填写。到现在为止,配置工作已经完成。
GIT BASH 命令详解
git init
初始化 GIT,只有初始化了以后才可以使用 GIT 相关命令。在初始化之前,可以先创建一个文件夹。如下示例
$ mkdir lmlphp$ cd lmlphp$ git init
git clone
获取远程项目,并下载到本地。远程库的地址在 GITHUB 项目中会有提供。下面是我测试时显示的内容,若执行成功,则将显示同下面类似的内容。
C:\Users\May\Documents\GitHub\test> git clone git@github.com:leiminglin/LMLPHP.gitCloning into 'LMLPHP'...Warning: Permanently added 'github.com,192.30.252.128' (RSA) to the list of known 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 .\LMLPHPC:\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 occuredC:\Users\May\Documents\GitHub\test\LMLPHP [master]> git branch developC:\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 occuredC:\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 developSwitched 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 newFeatureSwitched 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 occuredC:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]>
git status
git status 命令用来查看当前分支状态。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git statusOn branch newFeaturenothing 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 pullWarning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of known 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/ newFeatureC:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git pull origin developWarning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of known hosts.From github.com:leiminglin/LMLPHP * branch            develop    -> FETCH_HEADAlready up-to-date.C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]>
git add
git add 命令用来增加更新的内容,后面的参数为目录名或者文件名,一般在 git commit 命令之前使用。通过 git diff 可以查看有哪些不同之处,只有被增加的更新才会被提交到版本库。
git commit
git commit 命令用来提交更新。更新时需要提交注释,使用 -m 参数。GITHUB 提供的 GIT 客户端做的非常好,如果您忘记添加注释,它会弹出文本框让你填写。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git commit -m "test"On branch newFeaturenothing to commit, working directory clean
git push
git push 命令用来推送更新到远程库,此命令一般在 commit 命令之后使用。如果远程没有对应的分支名,则需要通过设置参数“--set-upstream”指定提交到哪个分支。如下示例:
C:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git pushfatal: 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 newFeatureC:\Users\May\Documents\GitHub\test\LMLPHP [newFeature]> git push --set-upstreamorigin developWarning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of known hosts.Branch develop set up to track remote branch develop from origin.Everything up-to-date

GIT BASH 的功能非常强大,使用也非常方便,特别适合大型项目多人同时开发。图形界面工具虽然方便简单,但是效率远远不能跟命令行相比。本文简单的介绍了 GIT BASH 的使用,更多的功能请使用 --help 命令查看,系统会使用浏览器打开 HTML 版本文档,描述的非常详细。

参考链接

http://blog.lmlphp.com/archives/7/The_use_tutorial_of_git_bash_and_how_to_start_with_github

0 0
原创粉丝点击