git和github在ubuntu下的使用

近期使用ubuntu系统越来越频繁,也越来越对ubuntu系统爱不释手,虽然说上手比较麻烦,但是习惯了各种命令之后,感觉在ubuntu系统上开发、应用等确实比较方便。

git

git是一款免费、开源的分布式版本控制系统,相比CVSSVN等传统版本控制工具来说,其不需要服务器端软件支持(当然,进行pull和push的时候,与服务端还是有交互的),更适合分布式开发,公共服务器的压力和数据量都不会太大,可以进行离线工作,任意两个开发者之间很容易解决冲突。
由于git是基于linux内核开发的,所以其主要是应用在linux系统上。当然,现在git也有for windows版本,或者在Cygwin环境下使用。

git安装

我使用的是ubuntu 14.04版本,系统默认没有安装git
首先可以尝试输入git命令,来检查是否安装了git

$ git
The program 'git' is currently not installed. You can install it by typing:
sudo apt-get install git

可以使用sudo apt-get install git进行安装。
或者如果是想使用最新版本的git,也可以用源码编译安装。从Git官网下载最新的源代码,然后解压,依次输入:./configmakesudo make install这几个命令安装就好了。
输入git,结果如下:
git

github账号的申请

如果只是需要将github上感兴趣的代码拷贝到本地,自己进行修改使用,而不打算共享发布的话,其实不申请帐号也没有关系,只需要 git clone 代码到本地就可以了。本文对这种方法不做讨论,毕竟使用 github 就是为了开源的目的。
首先去 github.com 上注册一个帐号,具体的注册流程就不赘述了。

git设置

github账号注册了之后,我们可以在git上对用户和邮箱进行设置,以便git对使用人的识别。

git config --global user.name = "用户名或者用户ID"
git config --global user.email = "邮箱"

这两个选项会在以后的使用过程中自动添加到代码中。

创建验证用的公钥

这个是比较复杂和困扰大多数人的地方,因为 git 是通过 ssh 的方式访问资源库的,所以需要在本地创建验证用的文件。
使用命令:ssh-keygen -C 'you email address@xxx.com' -t rsa
会在用户目录 ~/.ssh/ 下建立相应的密钥文件。
可以使用ssh -v git@github.com命令来测试链接是否畅通。

上传公钥

github.com的界面中 选择右上角的Account Settings,然后选择SSH Public Keys,选择新加。

Title可以随便命名,Key的内容拷贝自 ~/.ssh/id_rsa.pub 中的内容(ssh模式下可以通过vi ~/.ssh/id_rsa.pub查看),完成后,可以再使用ssh -v git@github.com进行测试。看到下面的信息表示验证成功。
github
这个时候,本机git就可以对我们自己的github远程仓库进行pullpush了。
比如提交到github之前先要进行关联git remote add origin https://github.com/kelvinsoft/webpackstu.git,如果仓库已有数据,就需要先进行pull同步,然后再进行git push origin master推送到github远程仓库。

root@iZ94or2br5nZ:/home/windsoft/gitrepo/webpackstu# git remote add origin https://github.com/kelvinsoft/webpackstu.git
root@iZ94or2br5nZ:/home/windsoft/gitrepo/webpackstu# git push origin master
Username for 'https://github.com': kelvinsoft
Password for 'https://kelvinsoft@github.com': 
To https://github.com/kelvinsoft/webpackstu.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/kelvinsoft/webpackstu.git'
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.
root@iZ94or2br5nZ:/home/windsoft/gitrepo/webpackstu# git pull origin master
From https://github.com/kelvinsoft/webpackstu
 * branch            master     -> FETCH_HEAD
Merge made by the 'recursive' strategy.
 README.md | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 README.md
root@iZ94or2br5nZ:/home/windsoft/gitrepo/webpackstu# ls -l
total 8
-rw-r--r-- 1 root root 509 Jun 21 08:34 package.json
-rw-r--r-- 1 root root  24 Jun 21 10:02 README.md
root@iZ94or2br5nZ:/home/windsoft/gitrepo/webpackstu# ll
total 20
drwxr-xr-x 3 root root 4096 Jun 21 10:02 ./
drwxr-xr-x 3 root root 4096 Jun 21 08:30 ../
drwxr-xr-x 8 root root 4096 Jun 21 10:03 .git/
-rw-r--r-- 1 root root  509 Jun 21 08:34 package.json
-rw-r--r-- 1 root root   24 Jun 21 10:02 README.md
root@iZ94or2br5nZ:/home/windsoft/gitrepo/webpackstu# git push origin master
Username for 'https://github.com': kelvinsoft
Password for 'https://kelvinsoft@github.com': 
Counting objects: 6, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 781 bytes | 0 bytes/s, done.
Total 5 (delta 0), reused 0 (delta 0)
To https://github.com/kelvinsoft/webpackstu.git
   bd1212e..61d45d3  master -> master

下面是一些比较有用的命令的介绍:

# 创建一个版本库
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
posted on 2016-06-21 10:12  宇文风  阅读(773)  评论(0编辑  收藏  举报