使用Github

首先安装Git,

在ubuntu下:

1
sudo apt-get install git

安装与设置ssh-key

1
2
3
4
5
6
7
8
9
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/ray/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): <输入key的密碼,或直接按下enter使用空密码>
Enter same passphrase again: <再输入一次密码>
Your identification has been saved in /home/ray/.ssh/id_rsa.
Your public key has been saved in /home/ray/.ssh/id_rsa.pub.
The key fingerprint is:
50:43:77:c6:97:af:61:82:dc:ea:9b:6b:67:d4:1b:61 rayhome1987@gmail

其中id_rsa.pub是公钥,而id_rsa则是私钥,请妥善保存以免遺失,它们都存放于~/.ssh目录中。将公钥粘贴到你github帐号中的SSH Public Keys的位置。注意小心不要复制到空格。
然后在github上面注册一个账号
以下是新建一个repo的例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Global setup:
 
# Download and install Git
git config --global user.email <your email="">
git config --global user.name <your name="">
 
# Next steps:
mkdir test # cd test
git init
# github会自动读取你的README内容并显示在项目简介中,因此先创建README
touch README
# 将README加到index中
git add README
 # 提交到版本库中
git commit -m 'first commit'
# 把github的repo加入为远程的repo
git remote add origin git@github.com:<你的ID>/test.git
# 把目前的commit状态push并同步到github上面
git push origin master
 
# Existing Git Repo?
cd existing_git_repo
git remote add origin git@github.com:<你的ID>/test.git
git push origin master
</your></your>

克隆一个repo,通过以下命令:

1
git clone git@github.com:<id>/test.git test</id>

以下是git的一些日常操作:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 创建一个版本库
git init
# 每次修改好了后,可以先将修改存入stage(快照/索引)中
git add <modified files="">
# 修改了大量文件则使用下面这个命令批量存入
git add .
# 使用commit将快照/索引中的内容提交到版本库中
git commit -m "msg"
# 也可以将git add与git commit用一个指令完成
git commit -a -m "msg"
# 将本地的git档案与github(远程)上的同步
git push
# 将github(远程)的git档案与本地的同步(即更新本地端的repo)
git pull
# 例如,pull指令其实包含了fetch(將变更复制回來)以及merge(合并)操作
git pull git://github.com/tom/test.git
 
# 另外版本控制系統的branch功能也很有意思,若同时修改bug,又要加入新功能,可以fork出一个branch:一个专门修bug,一个专门加入新功能,等到稳定后再merge合并
git branch bug_fix # 建立branch,名为bug_fix
git checkout bug_fix # 切换到bug_fix
git checkout master #切换到主要的repo
git merge bug_fix #把bug_fix这个branch和现在的branch合并
 
# 若有remote的branch,想要查看并checkout
git branch -r # 查看远程branch
git checkout -b bug_fix_local bug_fix_remote #把本地端切换为远程的bug_fix_remote branch并命名为bug_fix_local
 
# 还有其它可以查看repo状态的工具
git log #可以查看每次commit的改变
git diff #可以查看最近一次改变的內容,加上参数可以看其它的改变并互相比较
git show #可以看某次的变更
 
# 若想知道目前工作树的状态,可以輸入
git status
</modified>

Enjoy!

posted @ 2012-05-08 10:21  singleboss  阅读(351)  评论(0编辑  收藏  举报