通过apt-get安装了git,但是却无法执行,也找不到执行文件
$ git
-sh: git: not found
$ sudo find / -name git
/usr/share/doc/git
/usr/share/git
只好找其他安装方法
转自:http://blog.chinaunix.net/uid-25685402-id-3030181.html
第一:git的介绍
Git是Linux之父创建的一个轻型的文件系统,目前最多的被用来作为版本控制,版本控制的基本原理是以牺牲磁盘空间(保存所有的历史版本,而不是保存差量的部分)来提高性能,同时可以很容易的创建分支(有利于合作开发)。git中存在三个区域(1、Repository 2、Working directory 3、Staging area / Index), Repository(缺省的是以.git为名字的目录,意思就是可以自己自定义名字)可以比喻为一个仓库或者说数据库,因为以后所有的代码都要存放其中,Working directory很好理解,就是开发的环境(比如用于书写代码的那个目录),Staging area
/ Index是一个中间(代码从Working directory提交到Repository中)的状态。具体命令过程是:git add .代表把代码从Working directory提交到Staging area / Index 另外一个命令:git commit –m “注释”代表从Staging area / Index提交到Repository 综合这两个步骤的快捷命令是:git commit –a。另外git包含四个对象,分别是blob,tree和tag、commit(blob和tree对应,tree和commit对应,tag和commint对应),其实git的结构就是这几个对象所组成的一个树形结构,每个项目的版本在git中其实以commit的形式来保存,commit又指向tree,blob就好比tree的叶子,其实是保存数据部分的实体。另外git还包含指针HEAD,brance,HEAD唯一的指向commit.
想详尽的了解git请去网络搜索了解。
第二:git的下载和安装
下载的部分在官网下载即可,这里下载的是git-1.7.7.4.tar.gz (下载http://download.csdn.net/download/feilly/4980391)
[root@localhost ~]# tar zxvf git-1.7.7.4.tar.gz
[root@localhost ~]# cd git-1.7.7.4
[root@localhost git-1.7.7.4]# ./configure
期间出现问题:
/usr/bin/ld: crt1.o: No such file: No such file or directory
linux 下gcc编译时报错,是因为缺少libc6-dev
sudo apt-get install libc6-dev
[root@localhost git-1.7.7.4]# make && make install
如果出现错误 ”zlib.h No such file or directory”
zlib.h 包含在 zlibg1-dev中;
openssl/rand.h 包含在 libssl-dev中,系统没装。OK, 安装~,执行
# apt-get install zlib1g-dev libssl-dev
然后再跑# make
又看见错误 ”tclsh failed; using unoptimized loading”。 还要安装 tcl…….执行
# apt-get install build-essential tcl8.4 tk8.4 gettext
安装好之后查看以上没加入.configure时的默认安装目录
[root@localhost ~]# which git //这个路径在PATH内,故不需要再添加
/usr/local/bin/git
这样git就安装成功了
如果没有,则手动添加
设置PATH(除非你喜欢用绝对路径执行git)
vi /etc/profile 添加:export PATH=/usr/local/git/bin:/usr/local/git/libexec/git-core:$PATH
或
vi ~/.bashrc 添加:export PATH=/usr/local/git/bin:/usr/local/git/libexec/git-core:$PATH
第三:git的基本配置(这里root登录)
[root@localhost /]# mkdir -p /git/project
[root@localhost /]# cd /git/project/
[root@localhost project]# git config --global user.name 'ethnicitybeta'
[root@localhost project]# git config --global user.email 'ethnicitybeta@126.com'
Global的配置其实是写入用户的家目录中的git配置文件中
[root@localhost project]# cat ~/.gitconfig
[user]
name = ethnicitybeta
emal = ethnicitybeta@126.com
email = ethnicitybeta@126.com
[root@localhost project]# vim main.c //创建测试文件
Hello ethnicitybeta!!!
[root@localhost project]# git init //git的初始化
Initialized empty Git repository in /git/project/.git/
[root@localhost project]# ls -al
total 32
drwxr-xr-x 3 root root 4096 Nov 27 03:38 .
drwxr-xr-x 3 root root 4096 Nov 27 03:11 ..
drwxr-xr-x 7 root root 4096 Nov 27 03:38 .git
-rw-r--r-- 1 root root 23 Nov 27 03:37 main.c
接下来这段是相对于全局设置的局部设置
[root@localhost project]# git config user.name 'ethniciy'
[root@localhost project]# git config user.email 'ethnicity@126.com'
[root@localhost project]# cat .git/config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[user]
email = ethnicity@126.com
name = ethnicity
第四:一个git实例
[root@localhost project]# vim main.c //创建测试文件
Hello ethnicitybeta!!!
[root@localhost project]# git init //git的初始化
[root@localhost project]# git add . //加入到git
[root@localhost project]# ll .git/ //下边出现的index就是Staging area
total 72
-rw-r--r-- 1 root root 23 Nov 27 03:38 HEAD
drwxr-xr-x 2 root root 4096 Nov 27 03:38 branches
-rw-r--r-- 1 root root 143 Nov 27 03:41 config
-rw-r--r-- 1 root root 73 Nov 27 03:38 description
drwxr-xr-x 2 root root 4096 Nov 27 03:38 hooks
-rw-r--r-- 1 root root 104 Nov 27 03:43 index
drwxr-xr-x 2 root root 4096 Nov 27 03:38 info
drwxr-xr-x 5 root root 4096 Nov 27 03:43 objects
[root@localhost project]# git commit -m '1st commit' //提交生成第一个commit
[master (root-commit) 96f025b] 1st commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 main.c
[root@localhost project]# rm -rf main.c //模拟一个文件丢失
[root@localhost project]# git checkout -f HEAD //找回丢失的文件
[root@localhost project]# ls
main.c
第五:对于git对象的理解和实例分析
[root@localhost ~]# cd /git/
[root@localhost git]# mkdir object
[root@localhost object]# vim main.c
Hello wnayan!
[root@localhost object]# git hash-object main.c //计算main.c的哈希值
d32fe487dc38cbfc7fe051a5d6dbae01551d8dbd
[root@localhost object]# git init
Initialized empty Git repository in /git/object/.git/
[root@localhost object]# git add .
以下这个文件(blob)就是保存mia.c内容的文件
[root@localhost object]# ll .git/objects/d3/
total 8
-r--r--r-- 1 root root 30 Nov 27 04:02 2fe487dc38cbfc7fe051a5d6dbae01551d8dbd
[root@localhost object]# git commit -m "1st commint"
[master (root-commit) da97249] 1st commint
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 main.c
[root@localhost object]# find .git/objects/ -type f //列出文件夹下的普通文件
.git/objects/ae/eedc3b20507db9600f7f72431557fcf9303252
.git/objects/d3/2fe487dc38cbfc7fe051a5d6dbae01551d8dbd
.git/objects/da/97249d3649b1770b5cec8fa515833ea596f26f
[root@localhost object]# git show d32f //查看blob的内容,就是main.c的一样的内容
Hello wnayan!
[root@localhost object]# git cat-file -t aeeedc //这三句是查看object的类型
tree
[root@localhost object]# git cat-file -t da97
commit
[root@localhost object]# git cat-file -t d32f
blob
以上三种对象中tree保存文件的名字,blob保存文件的内容,这种结构有利于两个内容相同但是名字不同的文件的保存(tree保存两个名字,blob保存一份内容就可以了)
[root@localhost object]# git ls-tree aeee //列出tree和blob的对应关系
100644 blob d32fe487dc38cbfc7fe051a5d6dbae01551d8dbd main.c
[root@localhost object]# git show -s --pretty=raw da97 //查看commit的内容,关系很明确
commit da97249d3649b1770b5cec8fa515833ea596f26f
tree aeeedc3b20507db9600f7f72431557fcf9303252
author ethnicitybeta <ethnicitybeta@126.com> 1322337880 +0800
committer ethnicitybeta <ethnicitybeta@126.com> 1322337880 +0800
1st commint
第六:多层目录的实例
[root@localhost /]# mkdir -p /git/wanyan
[root@localhost /]# cd /git/wanyan/
[root@localhost wanyan]# cat README
just test!
[root@localhost wanyan]# cat lib/comment
include
[root@localhost wanyan]# cat lib/include/main.c
main
[root@localhost wanyan]# git init
Initialized empty Git repository in /git/wanyan/.git/
[root@localhost wanyan]# git add .
[root@localhost wanyan]# git commit -m "1st version"
[master (root-commit) 206ecf2] 1st version
3 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 README
create mode 100644 lib/comment
create mode 100644 lib/include/main.c
[root@localhost wanyan]# find .git/objects/ -type f //查看生成的对象
.git/objects/03/e86fb4437c4153cec24aa4021dca582abc0558
.git/objects/56/2f7710be64f9309a9fe6aa529753953dbe7e47
.git/objects/20/6ecf269d0f540995d88d6be825fecb09b5d3eb
.git/objects/8f/006adb88a619f20442a9eb3148cff31691eaf0
.git/objects/ba/2906d0666cf726c7eaadd2cd3db615dedfdf3a
.git/objects/90/48a2fd8c21a70a2dffcf80c819eee2fb491a31
.git/objects/a1/a869c8c840478164594a788ae5ce38dc0ee6da
[root@localhost wanyan]# git cat-file -t 03e8 //可以通过这个命令依次查看对象的类型
tree
[root@localhost wanyan]# ll .git/HEAD
-rw-r--r-- 1 root root 23 Nov 27 04:23 .git/HEAD //当前使用的commit是master
[root@localhost wanyan]# cat .git/HEAD
ref: refs/heads/master
[root@localhost wanyan]# cat .git/refs/heads/master
206ecf269d0f540995d88d6be825fecb09b5d3eb
[root@localhost wanyan]# git cat-file -t 206e
commit
接下来就是修改任意的文件,来生成第多个版本的测试
[root@localhost wanyan]# cd lib/
[root@localhost lib]# cat comment
include
change
[root@localhost wanyan]# git commit -a -m "2nd commit" //这个命令是那两个命令的综合
[master a7772df] 2nd commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost wanyan]# find .git/objects/ -type f //可以发现对象明显的增加
.git/objects/5f/eaaa33f5f3b3bc5fae0b750b2a5ebdc74b0a27
.git/objects/03/e86fb4437c4153cec24aa4021dca582abc0558
.git/objects/56/2f7710be64f9309a9fe6aa529753953dbe7e47
.git/objects/57/440fcc9bc816076d418a2da304a2498b928bae
.git/objects/20/6ecf269d0f540995d88d6be825fecb09b5d3eb
.git/objects/8f/006adb88a619f20442a9eb3148cff31691eaf0
.git/objects/ba/2906d0666cf726c7eaadd2cd3db615dedfdf3a
.git/objects/90/48a2fd8c21a70a2dffcf80c819eee2fb491a31
.git/objects/a1/a869c8c840478164594a788ae5ce38dc0ee6da
.git/objects/a7/772df6deb98005cf5ee21ae3ea863296677923
.git/objects/2c/f3d62adf771c30fa8062ce2c491d14486ea5ee
接下来这个步骤就是来打tag,tag分为轻量级的和全面包含的
首先是轻量级的tag这个tag虽然是对象,但是并不在对象目录里显示(object目录)
[root@localhost wanyan]# git tag v1.0
[root@localhost wanyan]# ll .git/refs/tags/
total 8
-rw-r--r-- 1 root root 41 Nov 27 04:50 v1.0
[root@localhost wanyan]# cat .git/refs/tags/v1.0 //可以发现内容是commit呀
a7772df6deb98005cf5ee21ae3ea863296677923
[root@localhost wanyan]# git cat-file -t a777
commit
然后是全面包含的tag,目录里显示(object目录)
[root@localhost wanyan]# git tag -a stable1.0 -m "1st stable"
[root@localhost wanyan]# find .git/objects/ -type f
.git/objects/5f/eaaa33f5f3b3bc5fae0b750b2a5ebdc74b0a27
.git/objects/03/e86fb4437c4153cec24aa4021dca582abc0558
.git/objects/56/2f7710be64f9309a9fe6aa529753953dbe7e47
.git/objects/57/440fcc9bc816076d418a2da304a2498b928bae
.git/objects/20/6ecf269d0f540995d88d6be825fecb09b5d3eb
.git/objects/8f/006adb88a619f20442a9eb3148cff31691eaf0
.git/objects/ba/2906d0666cf726c7eaadd2cd3db615dedfdf3a
.git/objects/90/48a2fd8c21a70a2dffcf80c819eee2fb491a31
.git/objects/a1/a869c8c840478164594a788ae5ce38dc0ee6da
.git/objects/a7/772df6deb98005cf5ee21ae3ea863296677923
.git/objects/8a/26be084ddce5e8178ca91def21e0c8f7a0945e
.git/objects/2c/f3d62adf771c30fa8062ce2c491d14486ea5ee
[root@localhost wanyan]# git cat-file -t 8a26
Tag
接下来演示tag的使用
[root@localhost wanyan]# vi README //修改一下
just test!
another hang!
[root@localhost wanyan]# git commit -a -m "3rd commit"
[master a08fc01] 3rd commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost wanyan]# cat README
just test!
another hang!
下边的操作演示的是把tag那个状态(修改之前),那个打包出去
[root@localhost wanyan]# git archive --format=tar --prefix=wanyan/ v1.0 | gzip > /tmp/wanyan.tar.gz
[root@localhost wanyan]# cd /tmp/
[root@localhost tmp]# tar zxvf wanyan.tar.gz
wanyan/
wanyan/README
wanyan/lib/
wanyan/lib/comment
wanyan/lib/include/
wanyan/lib/include/main.c
[root@localhost tmp]# cd wanyan
[root@localhost wanyan]# cat README //是不是发现又是tag那个状态的文件
just test!
第七:包含多个分支和合并实现的实例
1、出现合并冲突的实例
[root@localhost ~]# mkdir -p /git/branche
[root@localhost branche]# cp -rv ../wanyan/* . //偷懒一下
`../wanyan/README' -> `./README'
`../wanyan/lib' -> `./lib'
`../wanyan/lib/comment' -> `./lib/comment'
`../wanyan/lib/include' -> `./lib/include'
`../wanyan/lib/include/comment' -> `./lib/include/comment'
`../wanyan/lib/include/main.c' -> `./lib/include/main.c'
`../wanyan/lib/README' -> `./lib/README'
[root@localhost branche]# git init
Initialized empty Git repository in /git/branche/.git/
[root@localhost branche]# git add .
[root@localhost branche]# git commit -m "1st commit"
[master (root-commit) e9f37b6] 1st commit
5 files changed, 9 insertions(+), 0 deletions(-)
create mode 100644 README
create mode 100644 lib/README
create mode 100644 lib/comment
create mode 100644 lib/include/comment
create mode 100644 lib/include/main.c
[root@localhost branche]# cat .git/HEAD
ref: refs/heads/master
[root@localhost branche]# cat .git/refs/heads/master
e9f37b62445a7c855108cb00455c9922ea356c29
[root@localhost branche]# git cat-file -t e9f3
Commit
第一次改变:
[root@localhost branche]# vi lib/comment
include
change
the last change
[root@localhost branche]# git commit -a -m "2rd commit"
[master c2a876e] 2rd commit
1 files changed, 1 insertions(+), 0 deletions(-)
第二次改变:
[root@localhost branche]# vi README
just test!
another hang!
last hang
[root@localhost branche]# git commit -a -m "3rd commit"
[master f5febf9] 3rd commit
1 files changed, 1 insertions(+), 0 deletions(-)
创建分支
[root@localhost branche]# git branch laji
[root@localhost branche]# ll .git/refs/heads/
total 16
-rw-r--r-- 1 root root 41 Nov 27 05:19 laji
-rw-r--r-- 1 root root 41 Nov 27 05:18 master
[root@localhost branche]# cat .git/refs/heads/laji //以下可以看出指向同一个commit
f5febf9e98c5dc2a1279a56c47642677fdea79ec
[root@localhost branche]# cat .git/refs/heads/master
f5febf9e98c5dc2a1279a56c47642677fdea79ec
[root@localhost branche]# git branch //查看当前使用的分支
laji
* master
[root@localhost branche]# git checkout laji //分支的切换
Switched to branch 'laji'
[root@localhost branche]# git branch
* laji
master
[root@localhost branche]# cat .git/HEAD
ref: refs/heads/laji
接下来演示的是分支之间的关系(互不影响),在分支laji下的修改对master分支没任何影响。
首先是在laji分支的下的修改
[root@localhost branche]# vi README
just test!
another hang!
last hang
change for branch
[root@localhost branche]# git commit -a -m "laji 4th commit"
[laji b72a123] laji 4th commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost branche]# cat README
just test!
another hang!
last hang
change for branch
[root@localhost branche]# cat .git/refs/heads/laji //commit不相同了,可见出现了分支
b72a1238f9962dd103c5839077026e7c342595ce
[root@localhost branche]# cat .git/refs/heads/master
f5febf9e98c5dc2a1279a56c47642677fdea79ec
然后切换到master分支观察下
[root@localhost branche]# git checkout master
Switched to branch 'master'
[root@localhost branche]# git branch
laji
* master
[root@localhost branche]# cat README
just test!
another hang!
last hang
接着创造分叉(就是对mater下做出进一步的git)
[root@localhost branche]# git branch
laji
* master
[root@localhost branche]# vi README
just test!
another hang!
last hang
The master change
[root@localhost branche]# git commit -a -m "master 4th commit"
[master bf7bf97] master 4th commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost branche]# cat README //列出和laji分支做对比
just test!
another hang!
last hang
The master change
[root@localhost branche]# git checkout laji
Switched to branch 'laji'
[root@localhost branche]# cat README
just test!
another hang!
last hang
change for branch
最后就是分支的合并(把laji 合并到master),这个合并可以是不同的文件之间的合并(因为合作开发项目时,所做的工作基本是很难相同的)
[root@localhost branche]# git branch
laji
* master
[root@localhost branche]# git merge laji
Auto-merging README
CONFLICT (content): Merge conflict in README
Automatic merge failed; fix conflicts and then commit the result.
[root@localhost branche]# cat README //因为是同一个文件的合并出现了冲突
just test!
another hang!
last hang
<<<<<<< HEAD
The master change
=======
change for branch
>>>>>>> laji
[root@localhost branche]# git branch
laji
*master
[root@localhost branche]# vi README
just test!
another hang!
last hang
The master change
change for branch
~
[root@localhost branche]# git add .
[root@localhost branche]# git commit "last commit"
这样就可以了,解决了冲突,提交成功。
[root@localhost branche]# git branch -D laji //删除没用的分支
Deleted branch laji (was b72a123).
2、不出现冲突的实例
[root@localhost other]# cd ..
[root@localhost git]# mkdir another
[root@localhost git]# cd another/
[root@localhost another]# vi a1
wanyan
~
[root@localhost another]# vi a2
ethnicity
[root@localhost another]# git init
Initialized empty Git repository in /git/another/.git/
[root@localhost another]# git add .
[root@localhost another]# git commit -m "1st commit"
[master (root-commit) f723f47] 1st commit
2 files changed, 2 insertions(+), 0 deletions(-)
create mode 100644 a1
create mode 100644 a2
[root@localhost another]# git branch laji
[root@localhost another]# git branch
laji
* master
[root@localhost another]# git checkout laji
Switched to branch 'laji'
[root@localhost another]# vi a1
wanyan
zhengjing
[root@localhost another]# git commit -a -m "laji 2nd commit"
[laji 05cda63] laji 2nd commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost another]# git checkout master
[root@localhost another]# vi a2
ethnicity
beta
[root@localhost another]# git commit -a -m "mater 3rd commit"
[master 1239b8e] mater 3rd commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost another]# cat a1
wanyan
[root@localhost another]# cat a2
ethnicity
beta
[root@localhost another]# git checkout laji
Switched to branch 'laji'
[root@localhost another]# cat a1
wanyan
zhengjing
[root@localhost another]# cat a2
ethnicity
[root@localhost another]# git checkout master
Switched to branch 'master'
[root@localhost another]# git merge laji
Merge made by the 'recursive' strategy.
a1 | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost another]# cat a1
wanyan
zhengjing
[root@localhost another]# cat a2
ethnicity
beta
[root@localhost another]# git branch -D laji //删除分支
Deleted branch laji (was 05cda63).
[root@localhost another]# git branch
* master
第八:仅有一个分支的合并实例
[root@localhost git]# mkdir other
[root@localhost git]# cd other/
[root@localhost other]# vim main
hello ethnicitybeta
[root@localhost other]# git init
Initialized empty Git repository in /git/other/.git/
[root@localhost other]# git add .
[root@localhost other]# git commit -m '1st commit'
[master (root-commit) 9ef10c3] 1st commit
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 main
[root@localhost other]# git branch wanyan
[root@localhost other]# git checkout wanyan
Switched to branch 'wanyan'
[root@localhost other]# git branch
master
*wanyan
[root@localhost other]# vi main
hello ethnicitybeta
wanyanzhenjiang
~
[root@localhost other]# git commit -a -m "wanyan 2nd commit"
[wanyan 96aa677] wanyan 2nd commit
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost other]# cat main
hello ethnicitybeta
wanyanzhenjiang
[root@localhost other]# git checkout master
Switched to branch 'master'
[root@localhost other]# git branch
* master
wanyan
[root@localhost other]# cat main
hello ethnicitybeta
[root@localhost other]# git checkout master
Switched to branch 'master'
[root@localhost other]# git merge wanyan
Updating 9ef10c3..96aa677
Fast-forward //表示被合并的分支并没有出现分叉
main | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost other]# cat main
hello ethnicitybeta
wanyanzhenjiang
实验结束