引用網(wǎng)址
http://zhwen.org/?p=articles/git
Git 介紹
最近的小項(xiàng)目想使用git來管理,應(yīng)為git可以不需要服務(wù)器而在任意的Linux機(jī)器上管理代碼,相對(duì)svn和cvs還是有它的優(yōu)勢(shì)的,所以我選用了git來管理我的小項(xiàng)目,以后在提供svn的管理。 在使用了一段時(shí)間后想寫一點(diǎn)總結(jié),可能也是和網(wǎng)絡(luò)上其其它的git的文章差不多。但是作為我的使用總結(jié)還是很有必要的。
git安lixnus的解釋是--The stupid content tracker, 傻瓜內(nèi)容跟蹤器。呵呵!其實(shí)一點(diǎn)也不傻了,相當(dāng)?shù)闹悄芑苍S應(yīng)該這樣說是”content tracker for stupid guy”,呵呵!
git的管理是在本地建立存儲(chǔ)倉庫,代碼的所有變化的記錄都在本地存儲(chǔ)。也就是代碼和管理倉庫是形影不理的。不想svn分為客戶端和服務(wù)器端。客戶端只有 一些簡(jiǎn)單的倉庫信息,而真正的代碼和代碼的變化信息全都在服務(wù)器上保存。客戶端一般只能得到代碼文件(只是一般情況,如果非要得到當(dāng)然也還是可以的)。所 以git的這種方式可以減輕服務(wù)器的負(fù)擔(dān)--不用擔(dān)心服務(wù)器壞了或是連接不到怎么辦。
git的配置
所以首先我應(yīng)當(dāng)先說git的配置:
Git命令的使用,一般有兩種兩種形式,一種是git后面帶參數(shù)(如:git add),另一種是直接減號(hào)連接的一條命令(如:git-add),后面講解全部使用后者,這樣可以避免空格的使用帶來的問題。
helight@helight:~/mywork/zhwen.org$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/helight/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/helight/.ssh/id_rsa. Your public key has been saved in /home/helight/.ssh/id_rsa.pub. The key fingerprint is: e0:4a:ba:d9:ba:b9:7a:0a:e4:aa:86:6c:a7:d8:85:c0 helight@helight The key's randomart image is: +--[ RSA 2048]----+ | | | | | . | |. . . | |.E . . S | |o. + . | |+.o o | |==.B | |X=@+. | +-----------------+ helight@helight:~/mywork/zhwen.org$ vim /home/helight/.ssh/ id_rsa id_rsa.pub known_hosts
生成密鑰,用戶通信加解密。如果接受默認(rèn)設(shè)置,那么生成2048bits RAS的密鑰,私鑰和公鑰文件分別位于:~/.ssh/id_rsa和~/.ssh/id_rsa.pub。用戶需要向服務(wù)器管理員提供公鑰 (id_rsa.pub),在用戶同步版本庫時(shí)對(duì)用戶進(jìn)行身份認(rèn)證。用戶必須妥善保管私鑰。
helight@helight:~/kernel-mod/hello$ git-config user.name Zhwen Xu helight@helight:~/kernel-mod/hello$ git-config user.email Helight.Xu@gmail.com
配置用戶名,在生成補(bǔ)丁、日志時(shí)使用。git-config命令帶--global選項(xiàng)是對(duì)所有用戶信息進(jìn)行配置,默認(rèn)只針對(duì)對(duì)當(dāng)前用戶。 git-config中寫入配置信息。 如果git-config加了--global選項(xiàng),配置信息就會(huì)寫入到~/.gitconfig文件中。 因?yàn)槟憧赡苡貌煌纳矸輩⑴c不同的項(xiàng)目,而多個(gè)項(xiàng)目都用git管理,所以建議不用global配置。
用戶可以通過git-config的其他選項(xiàng)來對(duì)git做其他配置,--list可以查看用戶已經(jīng)選項(xiàng)。如:
helight@helight:~/kernel-mod/hello$ git-config --list user.name=Zhwen Xu user.email=Helight.Xu@gmail.com core.repositoryformatversion=0 core.filemode=true core.bare=false core.logallrefupdates=true helight@helight:~/kernel-mod/hello$
git的使用
命令:git-init-db 在我們要管理的目錄中執(zhí)行這條命令即可建立相關(guān)的git數(shù)據(jù)庫文件。 我們首先以一個(gè)小例子來說,如下在hello這個(gè)文件夾中有兩個(gè)文件,一個(gè)c程序和一個(gè)Makefile文件。
helight@helight:~/kernel-mod/hello$ ls -a . .. hello.c Makefile helight@helight:~/kernel-mod/hello$
接著執(zhí)行上面所說的命令:
helight@helight:~/kernel-mod/hello$ git-init-db Initialized empty Git repository in /home/helight/kernel-mod/hello/.git/ helight@helight:~/kernel-mod/hello$ ls -a . .. .git hello.c Makefile helight@helight:~/kernel-mod/hello$
執(zhí)行完上面的命令之后用“ls -a”來看,就會(huì)發(fā)現(xiàn)多了一個(gè)“.git”的文件,這個(gè)文件就是hello個(gè)小項(xiàng)目的git倉庫。關(guān)于這個(gè)倉庫有興趣的讀者可以進(jìn)入該文件夾繼續(xù)分析。 接下來介紹如何將項(xiàng)目中的文件添加到倉庫中,讓git來作版本管理。將文件添加到git倉庫中的命令是git-add,當(dāng)然這并不是真的將文件copy到git的倉庫文件夾中。只是在倉庫中標(biāo)識(shí)它,以示要用它git來管理它了。具體操作如下:
helight@helight:~/kernel-mod/hello$ git-add hello.c helight@helight:~/kernel-mod/hello$ git-add * helight@helight:~/kernel-mod/hello$
可以看到添加可以是單個(gè)文件添加,也可以是多個(gè)文件一起添加。接下來是提交和項(xiàng)目狀態(tài)的查看。相信有了前面的一點(diǎn)說明大家對(duì)git的管理也多少有點(diǎn)感覺了吧!
項(xiàng)目的提交和狀態(tài)查看:
命令:git-commit, git-status
helight@helight:~/kernel-mod/hello$ git-status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached ..." to unstage) # # new file: Makefile # new file: hello.c # helight@helight:~/kernel-mod/hello$ git-commit -m "init the project" ./* Created initial commit f4808f0: init the project 2 files changed, 27 insertions(+), 0 deletions(-) create mode 100644 Makefile create mode 100644 hello.c helight@helight:~/kernel-mod/hello$ git-status # On branch master nothing to commit (working directory clean) helight@helight:~/kernel-mod/hello$
第一個(gè)“git-status”的提示信息告訴我們版本庫中加入了兩個(gè)新的文件(這是和上一個(gè)版本的變化),并且 git 提示我們提交這些文件,我們可以通過 git-commit 命令來提交。提交后再次使用就會(huì)提示沒有變化需要提交了。
分支管理:
git主要提倡的一種管理方式就是分支管理,所以這應(yīng)該是每一個(gè)學(xué)習(xí)git的人應(yīng)該掌握的。
分支查看,分支建立和分支切換:
helight@helight:~/kernel-mod/hello$ git-branch * master helight@helight:~/kernel-mod/hello$ git-branch helight helight@helight:~/kernel-mod/hello$ git-branch helight * master helight@helight:~/kernel-mod/hello$ git-checkout helight Switched to branch "helight" helight@helight:~/kernel-mod/hello$ git-branch * helight master helight@helight:~/kernel-mod/hello$
可以看出“git-branch”是查看分支情況和創(chuàng)建分支,”git-checkout”個(gè)用來切換分支。其中“*”表示當(dāng)前工作的分支。
刪除分支:git-branch -D xxx
helight@helight:~/kernel-mod/hello$ git-branch xux helight@helight:~/kernel-mod/hello$ git-branch * helight master xux helight@helight:~/kernel-mod/hello$ git-branch -D xux Deleted branch xux. helight@helight:~/kernel-mod/hello$ git-branch * helight master helight@helight:~/kernel-mod/hello$
分支差異查看:git-show-branch,git-diff,git-whatchanged
我們對(duì)文件修改一下后在查看。
helight@helight:~/kernel-mod/hello$ git-show-branch * [helight] init the project ! [master] init the project -- *+ [helight] init the project
git-diff:上次提交到現(xiàn)在的變化差異
helight@helight:~/kernel-mod/hello$ git-diff diff --git a/hello.c b/hello.c index 843a6b8..c762de7 100644 --- a/hello.c +++ b/hello.c @@ -14,6 +14,7 @@ static int __init hello_init(void) static void __exit hello_exit(void) { printk("kernel: %s\n","Bey world!"); + printk("kernel: %s\n","Bey world!"); } module_init(hello_init);
git-commit:提交
helight@helight:~/kernel-mod/hello$ git-commit -m "some change" ./* Created commit 2d900d9: some change 1 files changed, 1 insertions(+), 0 deletions(-)
git-whatchanged:查看本分支的修改情況
helight@helight:~/kernel-mod/hello$ git-whatchanged commit 2d900d918d24943b32f3d41b1974e0375be02c9e Author: Zhenwen Xu Date: Wed Nov 12 22:09:45 2008 +0800 some change :100644 100644 843a6b8... c762de7... M hello.c commit f4808f013f44e815831a3830a19925472be83424 Author: Zhenwen Xu Date: Wed Nov 12 21:53:52 2008 +0800 init the project :000000 100644 0000000... c151955... A Makefile :000000 100644 0000000... 843a6b8... A hello.c helight@helight:~/kernel-mod/hello$
譬如我們要查看標(biāo)號(hào)為 master和helight的版本的差異情況, 我們可以使用這樣的命令:
helight@helight:~/kernel-mod/hello$ git-diff helight master diff --git a/hello.c b/hello.c index c762de7..843a6b8 100644 --- a/hello.c +++ b/hello.c @@ -14,7 +14,6 @@ static int __init hello_init(void) static void __exit hello_exit(void) { printk("kernel: %s\n","Bey world!"); - printk("kernel: %s\n","Bey world!"); } module_init(hello_init); helight@helight:~/kernel-mod/hello$ 補(bǔ)丁制作: git-format-patch helight@helight:~/kernel-mod/hello$ vim hello.c helight@helight:~/kernel-mod/hello$ git-commit -m "change by helight" hello.c Created commit 4772773: change by helight 1 files changed, 1 insertions(+), 0 deletions(-)
制作用于郵件發(fā)送的補(bǔ)丁:
helight@helight:~/kernel-mod/hello$ git-format-patch -s master 0001-change-by-helight.patch helight@helight:~/kernel-mod/hello$ cat 0001-change-by-helight.patch From 4772773ecbbde66b8febc1d8aed0da67d480f1e4 Mon Sep 17 00:00:00 2001 From: Zhenwen Xu Date: Thu, 13 Nov 2008 10:30:10 +0800 Subject: [PATCH] change by helight Signed-off-by: Zhenwen Xu --- hello.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/hello.c b/hello.c index 89795d2..2cbf9ee 100644 --- a/hello.c +++ b/hello.c @@ -6,6 +6,7 @@ MODULE_LICENSE("GPL"); static int __init hello_init(void) { printk("kernel: %s\n","Hello world!"); + printk("kernel: %s\n","This is Helight.Xu!"); return 0; } -- 1.5.6.5 helight@helight:~/kernel-mod/hello$
分支合并:git-merge
現(xiàn)在我們看看怎么將helight分支上的工作合并到master分支中。現(xiàn)在轉(zhuǎn)移我們當(dāng)前的工作分支到 master,并且將helight分支上的工作合并進(jìn)來。
helight@helight:~/kernel-mod/hello$ git-checkout master Switched to branch "master" helight@helight:~/kernel-mod/hello$ git-merge "merge helight" HEAD helight Updating f4808f0..2d900d9 Fast forward hello.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) helight@helight:~/kernel-mod/hello$
但是更多的是將現(xiàn)在的工作pull到主分支上去,如下命令:
helight@helight:~/kernel-mod/hello$ vim hello.c helight@helight:~/kernel-mod/hello$ git-commit -m "another change" ./* Created commit 1d6b878: another change 1 files changed, 0 insertions(+), 3 deletions(-)
git-pull:將工作更新到分支上
helight@helight:~/kernel-mod/hello$ git-checkout master Switched to branch "master" helight@helight:~/kernel-mod/hello$ git-pull . helight From . * branch helight -> FETCH_HEAD Updating 2d900d9..1d6b878 Fast forward hello.c | 3 --- 1 files changed, 0 insertions(+), 3 deletions(-)
現(xiàn)在來看看如何退回到上一個(gè)版本:git-reset
命令形式:
git-reset [ --soft | --hard] []
命令的選項(xiàng):
--soft
恢復(fù)到 git-commit命令之前,但是所作的修改是不會(huì)發(fā)生變化的。
--hard
將工作樹中的內(nèi)容和頭索引都切換至指定的版本位置中,也就是說自上上一個(gè)git-commit命令之后的所有的跟蹤內(nèi)容和工作樹中的內(nèi)容都會(huì)全部丟失。 因此,這個(gè)選項(xiàng)要慎用,除非你已經(jīng)非常確定你的確不想再看到那些東西了。
git信息查看和日志查看:
git-log
git-show
git-show-branch
git-show-index