Github与Git安装使用

本文详细介绍了如何在Windows XP系统上安装Git版本控制系统,并通过实例演示了如何使用Git进行基本操作,包括设置SSH远程连接、创建本地及远程仓库、提交更改、处理常见错误等。

操作系统:windows xp

1. 下载Git 版本控制系统

地址:http://code.google.com/p/msysgit/downloads/list 下载完成后点默认到安装完成

2. 运行桌面上的Git bash,出现命令框:

设置SSH远程连接服务器端Github库:

ssh-keygen -t rsa -C “your_email@youremail.com”

一直选择默认,中间出现两次输入密码,不用管,直接Enter,知道发生Key为止,Key在C:\Documents and Settings\Administrator\.ssh目录下的id_rsa.pub文件中。

可能出现的问题:

ssh: connect to host  port 22: Bad file number
fatal: The remote end hung up unexpectedly

解决方法:

在.ssh目录下添加一个config文件,无后缀名

Host github.com
User txledut(自己的用户名)
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

用IceSword查看了一下端口,发现22端口是被禁掉的。这个22端口是默认的ssh连接端口,新的端口不能和现在程序使用的服务端口重复,而且要确保开放,443端口是用来支持https服务的,这个端口默认开放。


3. 登录Github主页,按照用户邮箱登入,依次点击 Account Settings–SSH Public Keys–Add another public key 其中的“Title”随便填,“Key”填写刚刚产生的id_rsa.pub文件中的全部内容


本地客户端输入命令:

ssh -T git@github.com 测试连接
出现“Are you sure you want to continue connecting(yes/no)?”输入yes按一下“Enter”,You’ve successfully authenticated, but GitHub does not provide shell access 这就表示已成功连上github服务器端。

4.建好自己的工作目录, 在把项目上传到Github之前,还需要设置传到哪个库中,所以设置用户名跟邮箱,每次github在commit时候都会记录他们,右键工作目录文件夹选Git bash:

$ git config --global user.name "your name"
$ git config --global user.email "your_email@youremail.com"


Github页面,服务器端creat new res 建立一个Hello-World的库,与客户端同步用

5. 在本地工作目录建立客户端的库:比如建立 Hello world文件夹并添加README文件如下:

mkdir ~/Hello-World

cd ~/Hello-World

git init

touch README

git add README

git commit -m 'first commit'

git remote add origin git@github.com:txledut/Hello-World.git //连接远程的github项目,origin指向远程自己的库

git push -u origin master  //把本地项目的文件或更新加到github项目上


可能出现的错误:

1.在执行

$ git remote add origin git@github.com:txledut/Hello-World.git 

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

解决办法:

$ git remote rm origin

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

2. 在执行

$ git push origin master

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

解决办法:

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

执行完毕后,在服务器端可以看到README文件已经同步到Hello-World.git中。

 

Fork A Repo:

fork的意思是,当把Github上的项目搬到自己的空间上修改后再上传到自己空间库或者与原作者的库merge,对代码做出自己的贡献:

把服务器上的库克隆到本地:

在工作目录下:

git clone https://github.com/username/gitname.git

When a repository is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repository it was forked from. To keep track of the original repository, you need to add another remote named upstream:也就是说,克隆后,origin指向的还是自己的服务器端的库,而如果涉及原作者的库,需要再添加另外一个远程服务器端upstream,另一个分支,不管是原库有更新或者把更新的库同步到原库:

按照下面执行:

工作目录下克隆得到gitname文件夹,进入gitname,更新文件后:

1. 如果上传到自己的空间:

  更新文件

git add 已更新的文件

git commit -m '描述' //添加描述

git push origin master  //把本地项目的文件或更新加到github项目上

2. 如果同步到原作者的库或者从原库有更新,需要先配置远端库 再merge:

配置远端库:

git remote add upstream https://github.com/username/gitname.git

git fetch upstream

# Pulls in changes not present in your local repository, without modifying your files 

合并:

git fetch upstream
# Fetches any new changes from the original repo
git merge upstream/master
# Merges any changes fetched into your working files

git fetch与git pull的区别:两者都是从服务器获取代码更新,git pull会把服务器端任何有关的改动自动更新到本地,git fetch 会把目标远程服务器端的改动并且本地没有的commits更新到本地,但是不与当前分支合并,若想再合并到本地,再用git merge即可。

Create branches:

创建分支:

$git branch mybranch# Creates a new branch called "mybranch"
$git checkout mybranch# Makes "mybranch" the active branch
或者:

$git checkout -b mybranch# Creates a new branch called "mybranch" and makes it the active branch
在不同分支之间切换:

$git checkout master# Makes "master" the active branch
git checkout mybranch# Makes "mybranch" the active branch
更新完分支之后,合并到master分支:

$git checkout master# Makes "master" the active branch
git merge mybranch# Merges the commits from "mybranch" into "master"
git branch -d mybranch# Deletes the "mybranch" branch

待续。。。






评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值