[Git]Git-Github入门

基于 Linux ,Windows用户可以使用 cygwin

一、前往 http://www.cygwin.com/ 下载 setup.exe

运行 setup.exe ,一路向下(安装位置最好选择一个比较空的盘,路径不要包含中文和空格,装完后1G左右),在软件包选择界面选择要安装的软件包

我们这里要用到的有:

git-completion

openssh

vim

Git

已安装的软件包显示 keep ,跳过的显示 Skip ,要安装的则显示为版本号

安装完成后运行桌面的 Cygwin Terminal

输入 cygcheck 命令可以查看软件的版本

输入 mount 命令可以查看盘符的映射情况, cygpath 可以实现 Windows 和Cygwin 路径的转换

?

  $ cygpath -u d:\

/cygdrive/d

$ cygpath -w ~/

d:programfilescygwinhomeuser1

二、设置本地 Git

  # 配置用户

$ git config --global user.name "your_username"

$ git config --global user.email your_email_address

# 设置 git 颜色显示

$ git config --global color.ui true

# 设置 alias

$ git config --global alias.st status

$ git config --global alias.ci "commit -s"

$ git config --global alias.co checkout

# 查看git配置信息

$ git config –l

三、连接到 Github 需要验证本地的 SSH keygen ,这里我们通过 openssh 来生成本地秘钥(只需要生成一次)

Output: $ cd ~/.ssh

$ ssh-keygen -t rsa -C "your_email_address"

Genarating public/private esa key pair.

Enter file in which to save the key(/home/user1/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

一路回车就行了,然后

  $ cd ~/.ssh

$ ls -aFl

会见到新生成的 id_rsa 和 id_rsa.pub 文件

四、将秘钥保存在 Github

登陆 https://github.com/ ,点击右上角的 Account Settings ,然后点击左边的 SSH Keys ,点击 Add SSH Key ,将 id_rsa.pub 文件的内容粘贴进去(title可以随便输入),然后点击 Add Key。

五、新建 Github 项目

点击右上角的 Create a new repo 创建新项目(选择开源的话就是免费的)。

创建成功后,进入项目页,注意这个 SSH / Git Read-Only 两项地址,之后会用到

比如我这里就是

SSH git@github.com:leegf17/webapplee.git

Git Read-Only git://github.com/leegf17/webapplee.git

测试:

输入 $ ssh git@github.com回车:

  1. 如果出现以下结果,那么请重新生成ssh-keygen;

The authenticity of host ‘github.com (207.97.227.239)’ can’t be established.

RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts.

  1. 检验几次后,如果依然有以上问题,

设置git的user name和email:

$ git config --global user.name "leegf17"
$ git config --global user.email "leef17@gmail.com"

设置github的参数:

$ git config --global github.user leegf17
$ git config --global github.token 0123456789abcdef01234567xxxxxxx

github.user和github.token(在Accounting Settings->Applications->Create new token->创建name->生成key)都可以从github上得到。

  1. 那么请更换github账号,重新生成keygen后即可显示以下内容,
Hi leegf17! You've successfully authenticated

说明你连接成功了。  

六、在本地建立项目

首先添加 github 服务器,并下载你的项目文件

  # lee-server 是你给自己的 github 服务器取的名字

$ git remote add lee-server git@github.com:leegf17/webapplee.git

$ git clone git://github.com/leegf17/webapplee.git #这里改成你自己的Git Read-Only地址

$ cd webapplee

七:使用

Github 的本地文件分为三层,工作区——index——版本库 [——服务器]

我们可以通过 add 命令将工作区的文件提交到 index ,用 commit 命令将index提交到版本库,用 push 命令将本地文件提交到服务器。

1.获取源码:

$ git clone git@github.com:billyanyteen/github-services.git

2.这样你的机器上就有一个repo了。
3.git于svn所不同的是git是分布式的,没有服务器概念。所有的人的机器上都有一个repo,每次提交都是给自己机器的repo

仓库初始化:

git init

生成快照并存入项目索引:

git add

文件,还有git rm,git mv等等…
项目索引提交:

git commit

  1. 协作编程:
    将本地repo于远程的origin的repo合并,
    推送本地更新到远程:

git push origin master

更新远程更新到本地:

git pull origin master

补充:

添加远端repo:

$ git remote add upstream git://github.com/leegf17/webapplee.git

重命名远端repo:

$ git://github.com/leegf17/webapplee.git为“upstream”

============================================

  # 生成index.txt文件

$ touch index.txt

$ echo "test file" > index.txt

# 查看内容

$ cat index.txt

test file

$ git add index.txt

# 提交到服务端-上传

$ git commit -m "first commit" #commit要求必须输入说明

$ git push -u lee-server master #将本地版本库 master 提交到 lee-server 也就是之前定义的 github

Branch master set up to track remote branch master from github.

在github网页上就能看到这个文件了。

============创建项目事例=============

$ makdir ~/hello-world    //创建一个项目hello-world

$ cd ~/hello-world    //打开这个项目

$ git init    //初始化

$ touch README

$ git add README   //更新README文件

$ git commit -m 'first commit'//提交更新,并注释信息“first commit”

$ git remote add origin git@github.com:defnngj/hello-world.git   //连接远程github项目

$ git push -u origin master   //将本地项目更新到github项目上去

--------------------关于可能出现的错误-------------------

1.在执行

$ git remote addorigin git@github.com:defnngj/hello-world.git

错误提示:fatal: remote origin already exists.

解决办法:

$ git remote rm origin

然后在执行:$ git remote add origin git@github.com:defnngj/hello-world.git 就不会报错误了

2. 在执行

$ git push origin master

错误提示:error:failed to push som refs to.......

解决办法:

$ git pull origin master //先把远程服务器github上面的文件拉先来,再push 上去。

---------------------------后记-----------------------------

本文是参考官方帮助进行的:http://help.github.com/win-set-up-git/ 基本与官方步骤相同,我在此属于翻译了一下!

关于更过的学习:请登陆: http://progit.org/book/zh/进行学习。

本来关于此类知识应该属于开发的,本人从事测试工作,因为老大现在在推行git的使用,所以,就花了时间,初步的学习了一下,为了更好的测试嘛。呵呵。

第二个原因,看到有个乐师用版本管理系统(SVN)来更新和管理自己的乐谱,这个很有意思。版本管理系统并不局限于代码的管理。而且版本管理系统的思想也很有意思。

关于理论上的东西,请参考其它文档。

这里只是一些最最基本的操作,要领会Git的博大精深一定还是要去读更多的深入的资料(下面这本书就不错)

参考资料:

<Git权威指南>- http://ishare.iask.sina.com.cn/download/explain.php?fileid=34123964

原文地址:https://www.cnblogs.com/webapplee/p/3767720.html