git 学习

每次下载国外源代码,总会把我带到github网站去,可以看出国外程序员还是很喜欢用git来管理代码的,呵呵,看来自己也该学习下这个据说会是svn subversion 的替代品的git。
   于是又为了了解GIT到底是什么东西,google了好长时间,直看得我云里雾里,最终也没有搞清楚git相对于svn有哪些优点。最后还是来看一下教程:http://www.zeuux.org/science/learning-git.cn.html ,
                   http://www.linuxsir.org/main/doc/git/gittutorcn.htm
                   http://gitref.org/  : github提供的git学习文档-灰常给力的文档
   首先,学习下基本的命令,目标是能从github下载到自己想要的代码。
一、本机安装:
  1  在自己电脑上安装了 git-1.7.3.2-intel-leopard.dmg,  安装到目录: /usr/local/git.
   下载地址: http://code.google.com/p/git-osx-installer/downloads/list

     2 .  记录配置信息,用户名与Email:由于GIt是分布式版本控制系统,在本地上有一个版本库,我们可以设置自己的用户名与联系方式:
    xujia-27156:~ xujia$ git config --global user.name "xujia"  //设置信息
    xujia-27156:~ xujia$ git config --global user.email "rocketxj@gmail.com"
    xujia-27156:~ xujia$ git config --global --list    //查看信息
    user.name=xujia
    user.email=rocketxj@gmail.com

//其中,--global指明user.name, user.email是全局变量。所谓全局变量,就是在你的PC上任何版本库这些变量都是有效的。
user.name, user.email分别表示用户名与邮箱。我们可以通过--list列举出我们已经设置过的内容。需要说明的是,user.name,
user.email是必须设置的,以后才知道是谁修改了项目。实际上git可设置的选项超过130个,只是大部分我们是不常用的。

    3. 操作练习:
为了简明起见,我们创建两个文件作为练习:
xujia-27156:bd xujia$ echo 'hello word'>hello
xujia-27156:bd xujia$ echo 'xujia example'>example
查看状态:
xujia-27156:bd xujia$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#    example
#    hello
nothing added to commit but untracked files present (use "git add" to track)

添加内容跟踪信息
xujia-27156:bd xujia$ git add example hello
继而提交
xujia-27156:bd xujia$ git commit -m "xujia"
[master (root-commit) 97a07ed] xujia


 2 files changed, 2 insertions(+), 0 deletions(-)

create mode 100644 example
create mode 100644 hello
修改hello文件:
xujia-27156:bd xujia$ echo "it's a new day for git" > hello
查看hello文件内容
xujia-27156:bd xujia$ cat hello
it's a new day for git

git查看文件diff:
xujia-27156:bd xujia$ git diff
diff --git a/hello b/hello
index 7fd5222..23c679b 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-hello word
+it's a new day for git

添加内容跟踪信息并一起提交(简化命令)
xujia-27156:bd xujia$ git commit -a -m "a new day"
[master 44570aa] a new day
 1 files changed, 1 insertions(+), 1 deletions(-)

创建新分支:
xujia-27156:bd xujia$ git branch new-branch1011201856

切换到新分支内:
xujia-27156:bd xujia$ git checkout new-branch1011201856
Switched to branch 'new-branch1011201856'
查看自己当前所在分支:
xujia-27156:bd xujia$ git branch
  master
* new-branch1011201856
为了认识分支间的比较命令,进行操作如下:
修改分支中的hello文件:
xujia-27156:bd xujia$ echo "work work and worked" > hello
xujia-27156:bd xujia$ git diff
diff --git a/hello b/hello
index 23c679b..8a327ee 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-it's a new day for git
+work work and worked
添加并提交
xujia-27156:bd xujia$ git commit -a -m "0716"
[new-branch1011201856 d7374a4] 0716
 1 files changed, 1 insertions(+), 1 deletions(-)
切换到master分支:
xujia-27156:bd xujia$ git checkout master
M    hello
Switched to branch 'master'
修改两个文件:
xujia-27156:bd xujia$ echo "hard hard and harded" >example
xujia-27156:bd xujia$ echo "play play and played" > hello
添加并提交:
xujia-27156:bd xujia$ git commit -a -m "0714"
[master 150ed79] 0714
 2 files changed, 2 insertions(+), 2 deletions(-)

git show branch 命令可以使我们看到版本库中每个分支的世系发展状态, 并且可以看到每次提交的内容是否已进入每个分支
查看master分支:
xujia-27156:bd xujia$ git show master
commit 150ed79308fba11238134a760723b6a475a41263
Author: xujia <rocketxj@gmail.com>
Date:   Sat Nov 20 19:14:28 2010 +0800

    0714

diff --git a/example b/example
index c90caa7..db6debf 100644
--- a/example
+++ b/example
@@ -1 +1 @@
-xujia example
+hard hard and harded
diff --git a/hello b/hello
index 23c679b..ebafb0f 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-it's a new day for git
+play play and played

查看new-branch1011201856分支版本变化内容:
xujia-27156:bd xujia$ git show new-branch1011201856
commit d7374a4f399dd9ba46f4e8480f54aa1f39b6dd3c
Author: xujia <rocketxj@gmail.com>
Date:   Sat Nov 20 19:16:08 2010 +0800

    0716

diff --git a/hello b/hello
index 23c679b..8a327ee 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-it's a new day for git
+work work and worked
查看当前两分支的不同:
xujia-27156:bd xujia$ git diff master new-branch1011201856
diff --git a/example b/example
index db6debf..c90caa7 100644
--- a/example
+++ b/example
@@ -1 +1 @@
-hard hard and harded
+xujia example
diff --git a/hello b/hello
index ebafb0f..8a327ee 100644
--- a/hello
+++ b/hello
@@ -1 +1 @@
-play play and played
+work work and worked

查看分支的所有修改(默认遍历到最初版本0)
xujia-27156:bd xujia$ git whatchanged
commit d7374a4f399dd9ba46f4e8480f54aa1f39b6dd3c
Author: xujia <rocketxj@gmail.com>
Date:   Sat Nov 20 19:16:08 2010 +0800

    0716

:100644 100644 23c679b... 8a327ee... M  hello

commit 44570aa515b4b0e02e5465c69314075e027e653f
Author: xujia <rocketxj@gmail.com>
Date:   Sat Nov 20 18:53:25 2010 +0800

    a new day

:100644 100644 7fd5222... 23c679b... M  hello

commit 97a07ed0ba4415bbed1ee455c5409002d8ae3d59
Author: xujia <rocketxj@gmail.com>
Date:   Sat Nov 20 18:46:34 2010 +0800

    xujia

:000000 100644 0000000... c90caa7... A  example
:000000 100644 0000000... 7fd5222... A  hello

合并两分支,把new-branch1011201856合并到master:
先切换到master:
xujia-27156:bd xujia$ git checkout master
Switched to branch 'master'
再合并:
xujia-27156:bd xujia$ git merge "merge new-branch1011201856 to master" head new-branch1011201856
Auto-merging hello
CONFLICT (content): Merge conflict in hello
Automatic merge failed; fix conflicts and then commit the result.
出现冲突,需要手动解决:
xujia-27156:bd xujia$ cat hello
...出现类似svn冲突提示...去掉大于小于号后,提交版本,解决冲突。

 4. 下载github 上的代码
如果搜索到自己喜欢的代码,可以先fork下,github会把本是私有的代码生成在我自己的一份分支,这样就有写的权限了。

操作命令:
git clone
xujia-27156:http-openjson.wiki xujia$ git clone https://github.com/openjsan/openjsan.wiki.git
Cloning into openjsan.wiki...
remote: Counting objects: 14, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 14 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (14/14), done.


 5. 上传代码到github网站服务器上

注册github上帐号, 并在自己的mac机上的ssh生成rsa的keypair密钥,再把keypair中的公钥填写到网站的自己帐号中()。

这样才能建立new Repository,接着就可以在本机上已有的repository上传到github上。

命令操作如下:
xujia-27156:FD xujia$ git init
Initialized empty Git repository in /Users/xujia/develop/git learning/FD/.git/
xujia-27156:FD xujia$ touch README   //进入文件编辑了内容
xujia-27156:FD xujia$ git add README     //第一次提交不能用 git commit -a 
xujia-27156:FD xujia$ git commit -m "first commit on 2010-10-19 22:05"
[master (root-commit) cc503d0] first commit on 2010-10-19 22:05
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 README
xujia-27156:FD xujia$ git remote  //第一次并没有建立的remote链接信息别名
xujia-27156:FD xujia$ git remote add FDbackup git@github.com:rocketxujia/FD.git  //建立github中FD.git的连接(取了别名)
xujia-27156:FD xujia$ git remote  //查看已有的remote连接信息别名
FDbackup
xujia-27156:FD xujia$ git push FDbackup master   //利用remote连接别名,上传指定的分支。
Counting objects: 3, done.
Writing objects: 100% (3/3), 231 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:rocketxujia/FD.git
 * [new branch]      master -> master



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值