4 Github的使用
--------------------------------------------------------------------------------
GitHub是一个托管Git项目的网站,对于闭源项目收费,开源项目则免费。使用Github进行代码发布和托管的步骤如下:
1. 登录Github官网https://github.com/ ,申请Github账户,并创建名为github-test的Repository
2. 安装Git客户端(Linux)
#yum install git git-gui
3. 生成密钥对,并拷贝到Github网站
#ssh-keygen -t rsa -C “xxx@gmail.com”
xxx@gmail.com为你注册Github时的邮箱账户
登录Github点击Edit your profile->SSH keys,添加./.ssh/id_rsa.pub中的内容
4. 设置ssh不输入口令
#eval `ssh-agent`
#ssh-add
5. 测试是否能连接上GIthub
#ssh git@github.com
PTY allocation request failed on channel 0
Hi rangochan! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.
连接成功
6. 配置Git全局用户配置
# git config --global user.name xxx
# git config --global user.email xxx@gmail.com
xxx及xxx@gmail.com分别为Github账户名和邮箱
7. 创建本地新项目
#mkdir github-test
#cd github-test/
#git init
#touch README
#git add README
#git commit -m 'my first commit'
定义远程服务器别名origin
#git remote add origin git@github.com:xxx/github-test.git
本地和远程实行合并,本地默认为master
#git push origin master
当通过Github以xxx对github-test作出修改时,由于本地快照与Github远程服务器上的不一致,会引起以下错误:
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:xxx/puppet'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
解决:
通过pull子命令更新Github项目中作出的更改
#git pull origin master
之后再执行git push origin master
Counting objects: 8, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 714 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
登录https://github.com/xxx/github-test ,可查看到github-test项目
8. 更新文件
#vim README
just for test
自动commit更改文件
#git commit -a
更新到远程
#git push origin master
9. 创建和合并分支
#git branch
* master
显示当前分支是master
#git branch new-branch
创建分支
# git checkout new-branch
切换到新分支
# vi check.py
创建新文件
# git add check.py
# git commit -a -m "added a Python script"
Commit 到本地Git
# git push origin new-feature
合并到远程服务器
如果new-branch分支成熟了,则可以合并进master
#git checkout master
#git merge new-branch
#git branch
* master
new-banch
#git push
执行合并,master中也合并了new-branch中的更新
登录到GitHub,点击"Switch Branches"可以更改分支来查看不同分支下代码情况。